13

所以我有这个功能......

virtual void CallRemoteFunction( const char* pServerGameObjectId, const char* pFunctionName, OVariant arg1 = OVariant(), OVariant arg2 = OVariant(), OVariant arg3 = OVariant(), OVariant arg4 = OVariant(), OVariant arg5 = OVariant(), OVariant arg6 = OVariant(), OVariant arg7 = OVariant(), OVariant arg8 = OVariant(), OVariant arg9 = OVariant() );

我决定重写该函数,因为坦率地说,我对此感到尴尬。该函数很简单......采用可变数量的未知类型的参数并做一些事情。

我对现代 C++ 相当陌生,所以我进行了一些搜索,并假设我会找到一些简单/优雅的新方法。我想象过类似...

//hypothetical code
virtual void CallRemoteFunction( const char* pServerGameObjectId, const char* pFunctionName, ... args )
{
    std::vector<OVariant> argsArray;
    for (auto& arg : args )
    {
        argsArray.push_back(arg)
    }

    //do other stuff
 }
 //end hypothetical code

但在我的搜索中,我找不到任何接近的东西。那么有人可以给我一些关于如何将我的原始功能重构为更清洁或更简单的想法吗?注意:解决方案必须是 C++ 11 或更早版本。

-更新-该功能不必是虚拟的。
我希望能够像这样调用函数......

CallRemoteFunction("serverID","someFunc",1,2,3);

这是参考的实现

//calls function on client if this peer is a linked server or vice versa
void Peer::CallRemoteFunction( const char* pServerGameObjectId,  const char* 
pFunctionName, OVariant arg1, OVariant arg2, OVariant arg3, OVariant arg4, 
OVariant arg5, OVariant arg6, OVariant arg7, OVariant arg8, OVariant arg9 )
{
    Command command;
    command.SetObjectName( pServerGameObjectId );
    command.SetFunctionName( pFunctionName );
    command.ResetArgs();
    if ( arg1.IsValid() ){ command.PushArg( arg1 ); }
    if ( arg2.IsValid() ){ command.PushArg( arg2 ); }
    if ( arg3.IsValid() ){ command.PushArg( arg3 ); }
    if ( arg4.IsValid() ){ command.PushArg( arg4 ); }
    if ( arg5.IsValid() ){ command.PushArg( arg5 ); }
    if ( arg6.IsValid() ){ command.PushArg( arg6 ); }
    if ( arg7.IsValid() ){ command.PushArg( arg7 ); }
    if ( arg8.IsValid() ){ command.PushArg( arg8 ); }
    if ( arg9.IsValid() ){ command.PushArg( arg9 ); }

    LOG_DEBUG( "Calling Remote Function : " << pServerGameObjectId << "." << 
command.GetFunctionName() );
    ByteArray buffer( command.GetSerializeSize() );
    command.Serialize( buffer );

    ZMQMessage* request = new ZMQMessage();//deleted when sent via zmq
    request->addmem( buffer.GetBytes(), buffer.m_NumBytes );

    PushMessage( MDPW_REQUEST, m_pServiceId, request );
}
4

2 回答 2

7

Sure, you can have that, if you want. You just have to combine a templated forwarder with a non-templated virtual function.

I'm using gsl::span ("What is a "span" and when should I use one?") and an automatic std::array (not a native one to avoid the special case of 0) for best generality, stability and efficiency.

virtual void DoCallRemoteFunction(
    const char* pServerGameObjectId,
    const char* pFunctionName,
    gsl::span<OVariant> args
) {
    ...
}

template<class... ARGS>
void CallRemoteFunction(
    const char* pServerGameObjectId,
    const char* pFunctionName,
    ARGS&&... args
) {
    std::array<OVariant, sizeof...(ARGS)> arr = { OVariant{std::forward<ARGS>(args)} ... };
    DoCallRemoteFunction(pServerGameObjectId, pFunctionName, arr);
}

I have given them different names because you presumably will override the virtual function, and probably don't want the API-function to be shadowed then.

于 2018-05-13T13:10:05.743 回答
6

如果您愿意接受稍有不同的调用语法(例如std::max也使用),您可以有一个非常优雅的解决方案:

virtual void CallRemoteFunction( const char* pServerGameObjectId, const char* pFunctionName, std::initializer_list<QVariant> args )
{
    std::vector<OVariant> argsArray;
    for (auto& arg : args )
    {
        argsArray.push_back(arg)
    }

    //do other stuff
}

像这样调用:

CallRemoteFunction("foo", "bar", { arg1, arg2, arg3 });
于 2018-05-13T12:53:46.117 回答