简体   繁体   English

唯一标识成员函数指针的方法?

[英]Way to uniquely identify a pointer-to-member-function?

I'm trying to set up a "coroutine-like" system for a game engine, and am running into trouble finding any way to uniquely identify pointers-to-member-functions in a template function. 我正在尝试为游戏引擎设置“类似协程”的系统,并且在寻找任何方法来唯一标识模板函数中指向成员函数的指针时遇到麻烦。

I'd like to be able to start & stop coroutines in "Behavior" derived classes by calling CoStart and CoStop methods in the base class that take a pointer to a member function: 我希望能够通过调用基类中指向成员函数指针的CoStart和CoStop方法来启动和停止“行为”派生类中的协程。

CoStart( c_BlinkCycle );
CoStop( c_BlinkCycle );

Where the coroutine methods have a standard signature, eg: 协程方法具有标准签名的地方,例如:

CoCommand MyBehavior::c_BlinkCycle( int step ) {
    // ...
}

I can use a template in the base class to handle these nicely: 我可以在基类中使用模板来很好地处理这些问题:

template<typename T>
void CoStart( CoCommand (T::* coMethod)(int) ) {
    // ...
}

However, I want to be able to store some metadata for the coroutine methods on their first use (in CoStart()) and have no idea of any kind of unique way of identifying them. 但是,我希望能够在首次使用协程时为Cooutine方法存储一些元数据(在CoStart()中),并且不希望有任何独特的方式来识别它们。 Ie: 即:

if ( !metadataVector.contains( coMethod ) ) {
     // ... set up metadata
}

If I could just somehow get the address, or a type ID, or the name, or any kind of unique identifier to the pointer-to-member-function, I'd be set. 如果我能以某种方式获取地址,类型ID或名称,或指向成员函数指针的任何唯一标识符,那么我将被设置。 But with the template, I don't seem to have have any shared pointer type I could cast those to, so I'm kinda at a loss. 但是有了模板,我似乎没有任何可以将它们转换为的共享指针类型,所以我有点不知所措。 (FYI, I'm using boost::function and boost::bind later on, but doesn't look like they allow comparisons either). (仅供参考,稍后我将使用boost :: function和boost :: bind,但看起来它们也不支持比较)。

You can use an object with some members to store unique data identifying them instead of function pointers: 您可以使用带有某些成员的对象来存储标识它们的唯一数据,而不是函数指针:

template <typename T>
class CycleMod { 
   public:
      CycleMod(Meta data): meta(data) { }
      CoCommand start(int){
      };
      CoCommand stop(int){
      };        
   private:
      MetaData meta;
};

You can even wrap pointers with a similar class instead of having member functions. 您甚至可以使用类似的类包装指针,而不使用成员函数。

OK, think I'm going to give up on trying to uniquely identify the c_BlinkCycle() type methods intrinsically (unless someone knows a C++ trick to do it), and instead have them return a unqiue ID value when they get a certain special "step" parameter: 好的,我想我会放弃尝试固有地唯一标识c_BlinkCycle()类型的方法(除非有人知道使用C ++的技巧来做到这一点),而是让它们在得到特定的特殊值时返回unueue ID值。步骤”参数:

CoCommand MyBehavior::c_BlinkCycle( int step ) {
    if ( step == -1 ) {
        return CoCommand_Identify(1);
    }

    // ... otherwise do the particular steps...
}

A little more boilerplate than I wanted, but not the end of the world. 比我想要的多一些,但不是世界末日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM