如何实现可变虚成员函数
How to achieve variadic virtual member function
所以我有这个功能...
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++ 还很陌生,所以我进行了一些搜索,并假设我会找到一些 simple/elegant 执行此操作的新方法。我想象的是...
//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 );
}
如果您愿意接受稍微不同的调用语法(例如 std::max
also uses),您可以有一个非常优雅的解决方案:
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 });
当然可以,如果你愿意的话。您只需要将模板化转发器与非模板化虚函数结合起来即可。
我正在使用 gsl::span
(" ") 和自动 std::array
(not 一个本地的,以避免 0) 的特殊情况,以获得最佳的通用性、稳定性和效率。
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);
}
我给了它们不同的名字,因为你可能会覆盖虚函数,并且可能不希望 API-函数被隐藏。
所以我有这个功能...
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++ 还很陌生,所以我进行了一些搜索,并假设我会找到一些 simple/elegant 执行此操作的新方法。我想象的是...
//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 );
}
如果您愿意接受稍微不同的调用语法(例如 std::max
also uses),您可以有一个非常优雅的解决方案:
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 });
当然可以,如果你愿意的话。您只需要将模板化转发器与非模板化虚函数结合起来即可。
我正在使用 gsl::span
("std::array
(not 一个本地的,以避免 0) 的特殊情况,以获得最佳的通用性、稳定性和效率。
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);
}
我给了它们不同的名字,因为你可能会覆盖虚函数,并且可能不希望 API-函数被隐藏。