我试图为我正在进行的项目反序列化事件处理程序。但是,我试图对事件处理程序非常聪明,因为我没有考虑到序列化问题,所以我担心我已将自己编入角落。我提出的事件处理程序风格我实际上非常满意。但是现在我想反序列化它们,我试图找出一种简单的方法让事件处理程序在一个映射中注册它们的类型,其中处理程序类型作为键,并且反序列化函数指针作为值。但是,我在使函数指针的语法正确时遇到了问题。我已经尝试了许多不同的方法,编译器每次都会发出不同的错误。我已经把它归结为一个简单的错误信息,但我无法克服这个问题。谁能告诉我我做错了什么?
这里是事件处理程序的头文件:
#ifndef MESSAGE_JOINT_H
#define MESSAGE_JOINT_H
#include "Events.h"
#include "MessageReceiver.h"
namespace Events
{
class MessageReceiver;
class MessageJoint
{
public:
MessageJoint( Ogre::String id, MessageReceiver* receiver );
MessageJoint( const MessageJoint & rhs );
void FireEvent( MessageReceiver* from );
void SetData( boost::any d ){ data = d; }
void SetIdentifier( Ogre::String id ){ identifier = id; }
virtual void HandleEvent( boost::any ) = 0;
void TuneIn( MessageReceiver* r )
{
listeners.push_back( r );
}
Ogre::String GetIdentifier(){ return identifier; }
MessageReceiver* owner;
std::vector< MessageReceiver* > listeners;
boost::any data;
private:
typedef void (*Deserialize)(Events::MessageReceiver*);
static std::unordered_map<Ogre::String,Deserialize> mRegisteredAddresses;
static void Register( Ogre::String name, Deserialize f){ mRegisteredAddresses[name] = f;}
Ogre::String identifier;
};
}
#define MESSAGE_JOINT_CLASS( A ) class Joint ## A : public Events::MessageJoint \
{ \
public: \
static const Ogre::String c ## A ## Id; \
void HandleEvent( boost::any ); \
MessageJoint* _Deserialize( Events::MessageReceiver* r){ \
return new Joint ## A( r ); } \
Joint ## A ( Events::MessageReceiver* receiver ) \
: Events::MessageJoint( Ogre::String( "Joint ## A"), receiver ) \
{Register(GetIdentifier(),&Joint ## A::_Deserialize);} \
};
#endif
以下是某些事件的一些示例声明:
#define JOINTOPENMENUOBJECTMANAGER "OpenMenuObjectManager"
MESSAGE_JOINT_CLASS( OpenMenuObjectManager )
#define JOINTCLOSEMENUOBJECTMANAGER "CloseMenuObjectManager"
MESSAGE_JOINT_CLASS( CloseMenuObjectManager )
然后是那些声明的实现:
const Ogre::String SwissArmyKnife::JointOpenMenuObjectManager::cOpenMenuObjectManagerId = JOINTOPENMENUOBJECTMANAGER;
void SwissArmyKnife::JointOpenMenuObjectManager::HandleEvent( boost::any data )
{
SwissArmyKnife* sak = static_cast< SwissArmyKnife* >( owner );
sak->SetInputState( SwissArmyKnife::IS_MN_MENU );
}
const Ogre::String SwissArmyKnife::JointCloseMenuObjectManager::cCloseMenuObjectManagerId = JOINTCLOSEMENUOBJECTMANAGER;
void SwissArmyKnife::JointCloseMenuObjectManager::HandleEvent( boost::any data )
{
SwissArmyKnife* sak = static_cast< SwissArmyKnife* >( owner );
sak->SetInputState( SwissArmyKnife::IS_MAIN );
}
当我尝试使用最近的反序列化更改进行编译时,我收到如下错误消息:
1>c:\source\swissarmyknife\swissarmyknife\actors.h(214): error C2664: 'Events::MessageJoint::Register' : cannot convert parameter 2 from 'Events::MessageJoint *(__thiscall Actors::JointChangeState::* )(Events::MessageReceiver *)' to 'Events::MessageJoint::Deserialize'
1> There is no context in which this conversion is possible
1>c:\source\swissarmyknife\swissarmyknife\actors.h(214): error C2664: 'Events::MessageJoint::Register' : cannot convert parameter 2 from 'Events::MessageJoint *(__thiscall Actors::JointChangeState::* )(Events::MessageReceiver *)' to 'Events::MessageJoint::Deserialize'
1> There is no context in which this conversion is possible
1>c:\source\swissarmyknife\swissarmyknife\guicontrols.h(115): error C2664: 'Events::MessageJoint::Register' : cannot convert parameter 2 from 'Events::MessageJoint *(__thiscall GuiControls::JointGuiPropertyChanged::* )(Events::MessageReceiver *)' to 'Events::MessageJoint::Deserialize'
1> There is no context in which this conversion is possible
1>c:\source\swissarmyknife\swissarmyknife\menuobject.h(84): error C2664: 'Events::MessageJoint::Register' : cannot convert parameter 2 from 'Events::MessageJoint *(__thiscall GuiControls::JointCloseMenu::* )(Events::MessageReceiver *)' to 'Events::MessageJoint::Deserialize'
1> There is no context in which this conversion is possible