?
Solved

c++ callbacks to member functions of any generic object

Posted on 2007-10-13
5
Medium Priority
?
636 Views
Last Modified: 2010-05-18
Hi,

I would like to implments a c++ callback like this:

//pseudocode

//instantiate various user-defined objects:

Person me;
Person jack;
Person dianne;
Dog pooch;

// subscribe the objects to me's Notify() function, so that when me.Notify() is called, each subscribing objcect has its own OnNotify() called;

me.subscribe( jack.OnNotify() )
me.subscribe( dianne.OnNotify() )
me.subscribe( pooch.OnNotify() )

Person::OnNotify() will be defined differently than Dog::OnNotify(), but will have the same signature.

It is critical that I subscribe member functions to the callback rather than static functions.

Person::subscribe( function_pointer ) will insert the function pointer into a vector of function pointers.

Person::Notify() will iterate over the vector of function pointers, calling (*iter)() for each one.



I understand that this is rather difficult to do in c++ and that there are issues regarding type-safety, doing it with different types (Dog and Person), and needing to use member functions.

I have found examples online (and even on EE) that deal with these issues and use templates, passing of a pointer to the instance, templates, functors, etc. But the examples were not explained clearly or slightly different than what I need to do above.

I'm a little overwhelmed and was hoping one of y'all had crossed this bridge before and found a simple, correct solution.

Thanks a ton guys,

Perk
0
Comment
Question by:highqllc
5 Comments
 
LVL 12

Assisted Solution

by:OnegaZhang
OnegaZhang earned 600 total points
ID: 20071746
Have you read this:
http://www.codeproject.com/cpp/FastDelegate.asp
Member Function Pointers and the Fastest Possible C++ Delegates
By Don Clugston.

It can store your member function pointer into a container.
0
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 200 total points
ID: 20071805
Or use portable boost::function
http://www.boost.org/libs/bind/bind.html
0
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 20072050
Why son't you do that like

struct INotifySink {

virtual void OnNotify() = 0;
};

class Person : public INotifySink, public SomeOtherStuff {

public:

  void OnNotify() {

      // provide implementation
  }

};

class Dog : public INotifySink, public SomeDifferentOtherStuff {

public:

  void OnNotify() {

      // provide implementation
  }

};

Person me;
Person jack;
Person dianne;
Dog pooch;


class Notifier {

public:

  void subscribe(INotifySink* p) {

    m_vSubscribers.push_back(p);
  }

protected:

  vector<INotifySink*> m_vSubscribers;

  void NotifyAll () {

    for (vector<INotifySink*>::iterator i = m_vSubscribers.begin(); i != m_vSubscribers.end(); ++i) {

      i->OnNotify();
    }
  }
};


Notifier me;

me.subscribe( &jack);
me.subscribe( &dianne);
me.subscribe( &pooch);
0
 

Author Comment

by:highqllc
ID: 20072219
thanks for the ideas...  I like jkr's suggestion. It is very easy to understand and works well in my implmentation.
0
 

Author Comment

by:highqllc
ID: 20079725
jkr,

How should I handle the destruction of subscribing objects?  I don't want NotifyAll() to attempt to dereference a pointer to an object that no longer exists.  
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question