简体   繁体   中英

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( 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. 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. 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).

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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