Repost: UnManaged delegates: Do they exist?

  • Thread starter Thread starter Egbert Nierop \(MVP for IIS\)
  • Start date Start date
E

Egbert Nierop \(MVP for IIS\)

Is it possible to create events without using sendmessage?

sample:

class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m looking
for) and passes it to the instance of class a.
 
class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m looking
for) and passes it to the instance of class a.

Hi,

This is possible, but you have to supply both the function pointer and the
object pointer.
This is because in C++ a member function is not explicitly tied to an
instance. Thus the compiler need another way to know which instance the
function should be executed for.

There is a good howto over here:
http://www.newty.de/fpt/fpt.html#chapter2
Take a look at chapter 2.5 which explains how to execute a function with the
member function pointer and the instance pointer

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Egbert said:
Is it possible to create events without using sendmessage?

Events and delegates are completely unrelated ot sendmessage.
sample:

class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m
looking for) and passes it to the instance of class a.

A managed delegate is an instance of a class (System.MulticastDelegate).
That class encapsulates a few things, including a method selector
(equivalent to a pointer to member function) and an object reference.

You can build the same thing in native C++, or you can use an already
written library like boost::signals.

The native equivalent to a delegate is roughly:

template <class T, class RT, class E>
struct function_t
{
typedef RT (T::*PMF)(void*,E*);
};

template <class T, class RT, class E>
class delegate
{
private:
T* m_t;
typename function_t<T,RT,E>::PMF m_f;

public:
delegate(T* t, typename function_t<T,RT,E>::PMF f)
: m_t(t), m_f(f)
{
}

RT operator() (void* o,E* e) const
{
return (m_t->*m_f)(o,e);
}
};

class E
{
};

class A
{
public:
int f(void*, E*);
};


int main()
{
A a;
delegate<A,int,E> d = delegate<A,int,E>(&a,&A::f);
// ...

int i = d(0,new E());
}


Of course, unlike a managed delegate, an instance of this native delegate
won't keep the referenced object alive - you still need to ensure that the
object lifetime exceeds the delegate lifetime yourself.

-cd
 
Egbert said:
Is it possible to create events without using sendmessage?

SendMessage is totally unrelated to events/delegates.
sample:

class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m
looking for) and passes it to the instance of class a.

You'd better use already written libraries for this kind of things. See
boost::signal or libsigc++ for example

Arnaud
MVP - VC
 
Carl Daniel said:
Events and delegates are completely unrelated ot sendmessage.

Sure, but they are both a way to signal...

Thanks for your code!
 
I don't think you need events for what you're trying to do. Function
delegates will do just fine. In the native C++ library space your
closest bet is probably the boost::function library.

Here is an example of how you can achieve this:
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>

class A {
public:
void foo(boost::function<int (bool)> func) {
func_ = func;
}

int foobar(bool b) {
return func_(b);
}

private:
boost::function<int (bool)> func_;
};

class B {
public:
int bar(bool);
};

int main() {
A a;
B b;
a.foo(&b ->* &B::bar);
int result = a.foobar(true); // *** effectively does: result =
b.bar(true);
}
 
Egbert said:
Is it possible to create events without using sendmessage?

sample:

class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m
looking for) and passes it to the instance of class a.

I have two different implementations, both of which are explained in
great detail on my Web site:

http://tweakbits.com/articles/events/index.html

This should give you a good understanding of events.

I recommend that you take a look at boost::function for an
industry-strength solution. Unlike my solution, it's used by 1000s of
programmers on all kinds of platforms.

Tom
 
Back
Top