Member Function Delegate
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Member Function Delegate

  1. #1
    Join Date
    May 2013
    Posts
    18

    Member Function Delegate

    Hi there,

    I'm trying to write a simple Delegate class with a Bind() and Invoke() function. For now it only needs to support a void class function with no parameters. I've searched around and found quite a few exmaples, though, those class are heavily templated and I lose track trying to simplify it.

    So far my code is following:
    Code:
    #include <windows.h>
    
    class Test
    {
    public:
    	void DoSomething()
    	{
    		MessageBox(NULL, L"Test::DoSomething!", NULL, 0);
    	}
    };
    
    template<class tObject, void (tObject::*Func)()>
    void Wrapper(void* Object)
    {
    	return ((tObject *)(Object)->*Func)();
    }
    
    class Delegate
    {
    public:
    	typedef void (*FuncType)(void *);
    
    	template <class tObject>
    	void Bind(tObject *Object, void (tObject::*Callback)())
    	{
    		m_Object = Object;
    
    		m_Callback = &Wrapper<tObject, &tObject::DoSomething>;	// Works, but need to replace DoSomething with Callback variable somehow
    
    		// ((tObject *)m_Object->*Callback)();		// This works, but needs to be in Invoke function, so these variables need saving somehow
    	};
    
    	void Invoke()
    	{
    		(*m_Callback)(m_Object);	// Works if m_Callback is saved with Wrapper helper class
    	};
    
    private:
    	void *m_Object;
    	FuncType m_Callback;
    };
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	Test test;
    	Delegate dg;
    
    	dg.Bind(&test, &Test::DoSomething);
    
    	dg.Invoke();	// Call test.DoSomething();
    
    	return 0;
    };
    The part I am having difficulty with is assigning &Test::DoSomething to the m_Callback variable.

    &tObject::DoSomething works, yet _Callback which I pass &Test::DoSomething to does not work.

    Where am I going wrong?

    Thanks.

    [EDIT] Also, why does the following line work:
    Code:
    m_Callback = &Wrapper<tObject, &tObject::DoSomething>;
    When wrapper is like:
    Code:
    template<class tObject, void (tObject::*Func)()>
    void Wrapper(void* Object)
    Should it not be Wrapper<class-typename, parameter-1>(parameter-2) // This currently creates an error
    Last edited by FrOzeN89; June 22nd, 2013 at 05:29 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,003

    Re: Member Function Delegate

    Quote Originally Posted by FrOzeN89 View Post
    I'm trying to write a simple Delegate class with a Bind() and Invoke() function. For now it only needs to support a void class function with no parameters.
    And why do you want this? Why not just use std::function, boost::function or libsigc?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    May 2013
    Posts
    18

    Re: Member Function Delegate

    Thanks. I went with std::function. I overlooked that somehow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center