I'm looking for a good way to pass values from GUI controls to my Resource managment, without including everything everywhere.
The problem with my current solution is that i cannot really debug it, to watch values cause of the #defines.
Is there any better way to do this ?
This is my current solution:
The functors keep the Setter and Getter Methods of the Resource Managment class.
ComponentDefinitions.h
PHP Code:
#define DEFINE_FUNCTOR(type) \
typedef boost::function<void (type)> type##SetFunction; \
typedef boost::function<type (void)> type##GetFunction;
DEFINE_FUNCTOR(double);
#define STRINGIFY(x) EXPAND(x)
#define EXPAND(x) #x
#define DECLARE_SETGET_CALLBACK(type) \
protected: \
type##SetFunction m_pSet##type##Functor; \
type##GetFunction m_pGet##type##Functor; \
public: \
void SetVariableCallback(type##SetFunction s, type##GetFunction g) \
{ \
m_pSet##type##Functor = s; \
m_pGet##type##Functor = g; \
m_strVariableType = wxT(STRINGIFY(type)); \
}
#define IMPLEMENT_SET_CALLBACK(type,val) \
if (m_strVariableType == wxT(STRINGIFY(type))) \
{ \
if ((type)val != m_pGet##type##Functor()) \
m_pSet##type##Functor((type)val); \
}
#define IMPLEMENT_GET_CALLBACK(type,val) \
if (m_strVariableType == wxT(STRINGIFY(type))) \
val = m_pGet##type##Functor();
#define CREATE_SETGET_CALLBACK(type, thisptr, SetMethod, GetMethod) \
(type##SetFunction)boost::bind(&SetMethod,thisptr,_1), \
(type##GetFunction)boost::bind(&GetMethod,thisptr)
This is a minimized version of one control. It can be extended by other data types than double, by DECLARE_SETGET_CALLBACK(...) and parsing in OnTextEntered.
TextCtrlElement.h
PHP Code:
#include "ComponentDefinitions.h"
class TextCtrlElement : public wxTextCtrl
{
public:
TextCtrlElement (wxWindow* parent, wxWindowID id);
~TextCtrlElement();
DECLARE_SETGET_CALLBACK(double)
void OnTextEntered(const wxString &text)
{
double var;
if (ParseToDouble(text, var))
IMPLEMENT_SET_CALLBACK(double, var);
};
}
And finally the usage.
PHP Code:
#include "TextCtrlElement.h"
// ...
TextElem = new TextCtrlElement( this,ID_ENTER_VOLUME );
TextElem->SetVariableCallback(CREATE_SETGET_CALLBACK(double,m_pResourceManager,
ResourceManager::SetVolume,
ResourceManager::GetVolume);
Thanks for any hints !
Stefan