GUI2Data

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

GUI2Data

Post by phlox81 »

The Problem:

You have some control, holding a value, which actually should transfer to a Dataclass, or a layer between it.

I found now this solution for it:

Code: Select all

template<class functor, class event>
class GUI2Data : public wxEvtHandler
{
	functor f;
public:
	GUI2Data() {}
	GUI2Data(functor f):f(f)
	{
	}
	void Setfunctor(functor func)
	{
		f = func;
	}
	void OnSendData(event& e)
	{
		wxControl* control = dynamic_cast<wxControl*>(e.GetEventObject());
		if(control)
			f(control->GetLabel().fn_str());
	}
};
The Functor can be used a normal functor (boost::bind f.e.) or boost::function + bind.
Or something else.
The Event is the Event, which should trigger this operation.
f.e. OnText, or a FocusEvent.

I am not sure if control->GetLabel() works for all controls probably, but I think it should work pretty good.

The Signature of the functor needs to be some string type. f.e. std:: (w)string, or wxString, const (w)char*

Usage:

Code: Select all

// in a panelclass f.e.
GUI2Data<boost::function<void (std::string)>,wxFocusEvent> gui2data(boost::bind(&foo::SetName,classpointer,_1));

Connect(wxEVT_KILL_FOCUS,wxFocusEventHandler((GUI2Data<void (std::string),wxFocusEvent>::OnSendData)),0,gui2data);

Now the control is connected to the instance of the class.

phlox
Sickboy
Experienced Solver
Experienced Solver
Posts: 91
Joined: Wed Mar 16, 2005 10:30 pm
Location: Germany

Post by Sickboy »

Exactly what i was lookin for. But do you know to do the same thing just with STL instead of Boost ?
I mean instead of boost::function, boost::bind.

Thanks,
Stefan
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

look in the documentation of boost::bind.

ALso this is part of tr1, so if your compiler has a tr1 implementation, you can use std::tr1::function and std::tr1::bind
Sickboy
Experienced Solver
Experienced Solver
Posts: 91
Joined: Wed Mar 16, 2005 10:30 pm
Location: Germany

Post by Sickboy »

There is no other way, without the tr1 extension ?

Sorry that i'm askin but i'm quit new to this functor stuff and still have some problems to understand it :oops:
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

Sickboy wrote:There is no other way, without the tr1 extension ?

Sorry that i'm askin but i'm quit new to this functor stuff and still have some problems to understand it :oops:
Offcourse you can use normal functors and STL to.
Just take a look at google ;)
I use boost::function as it is really easy.(and i am a bit lazy ;))
Sickboy
Experienced Solver
Experienced Solver
Posts: 91
Joined: Wed Mar 16, 2005 10:30 pm
Location: Germany

Post by Sickboy »

sounds good. just a last question ;-) is it possible to compare functor, to achieve something like that ?

Code: Select all

template<class functor, class event> 
class GUI2Data : public wxEvtHandler 
{ 
        functor f; 
public: 
        GUI2Data() {} 
        GUI2Data(functor f):f(f) 
        { 
        } 
        void Setfunctor(functor func) 
        { 
                f = func; 
        } 
        void OnSendData(event& e) 
        { 
                wxControl* control = dynamic_cast<wxControl*>(e.GetEventObject()); 
                if(control) 
                {
                        if (f == boost::function<void (std::string)>)
                            f(control->GetLabel().fn_str()); 
                        else if (f == boost::function<void (double)>)
                        {
                              double var;
                              if (control->GetLabel().ToDouble(var))
                                  f(var);
                        }
                        else if (f == boost::function<void (char)>)
                        // ...
                }
        } 
};
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

I am not to sure in this, I know I can compare boost::function, but in this way, you probably would need a more templatebased approach, as you try to find a type info, which is probably best viewed in Compiletime.

Maybe boost::FunctionTypes provides what you need.

Otherwise you can offcourse make different versions, with template-specialisation.
Post Reply