Quote:Original post by Zahlman
Quote:Original post by d000hg
I have a macro which is something like this:
#define CREATE_FUNCTOR(name,method) struct name { void method(); };
This isn't exact so don't focus on why I want to do this!
Sorry, I must insist. You are aware of std::mem_fun and std::mem_fun_ref, yes? I'm pretty sure there's *something* that solves your problem more nicely...
I don't think I can use these but I'll try and remember to check. In fact you can see the real macros...
#define CREATE_HANDLER(DLG_NAME,HANDLER_NAME) /void HANDLER_NAME(); /struct HANDLER_NAME##_functor : public GUIEventHandlerBase /{ / DLG_NAME *m_pThis; / inline void On() { m_pThis->HANDLER_NAME(); } / HANDLER_NAME##_functor(DLG_NAME *pThis) : m_pThis(pThis){} /};#define REGISTER_HANDLER(ID,EVENT,HANDLER) RegisterEventHandler(ID,EVENT,new HANDLER##_functor(this));
And the way they are used in header and source file:
class MainMenuDlg : public GUIDialog {protected: const static std::string Template; GameState *m_pGameState;public: MainMenuDlg(GUIManager *pManager,GameState *pGameState); virtual ~MainMenuDlg(); void LoadSaveGame(char *name); CREATE_HANDLER(MainMenuDlg , OnQuit) CREATE_HANDLER(MainMenuDlg , OnLoad) CREATE_HANDLER(MainMenuDlg , OnNewGame)};
const std::string MainMenuDlg::Template = "MainMenu";MainMenuDlg::MainMenuDlg(GUIManager *pManager,GameState *pGameState): GUIDialog(pManager,Template), m_pGameState(pGameState){ SetPos(0,0); SetSize(1,1); m_pGUIManager->LockWindow(this,true); REGISTER_HANDLER("BTN_EXIT",BUTTON_LCLICK,OnQuit) REGISTER_HANDLER("BTN_NEW",BUTTON_LCLICK,OnNewGame) REGISTER_HANDLER("BTN_LOAD",BUTTON_LCLICK,OnLoad)}void MainMenuDlg::OnQuit(){} void MainMenuDlg::OnLoad(){}void MainMenuDlg::OnNewGame(){}
The macros are ugly but the end result is quite nice - not safe or proper C++ I'm sure - I want the user to have it this easy to use but not have to resort to nasty macros (as much) if possible.