Boost logo

Boost :

From: Kevin Wan (wanjunfeng_at_[hidden])
Date: 2006-02-10 09:09:10


I've added a delegate implementation in C++.

If anyone interested, please review it.

the usage is like:
#include <stdio.h>
#include "delegate.h"

void print_hello()
{
    printf("print_hello\n");
}

void print_world()
{
    printf("print_world\n");
}

class Object
{
public:
    Object() { printf("Object::Object\n"); }
    ~Object() { printf("Object::~Object\n"); }
    void print() const
    {
        printf("in Object::print\n");
    }
    void test() const
    {
        wan::delegate<void()> d;
        d.add(this, &Object::print);
        d();
    }
};

class Functor
{
public:
    void operator()() const
    {
        printf("in Functor::operator() const\n");
    }
};

int main()
{
    {
        wan::delegate<void()> d;
        d.add(&print_hello);
        d.add(&print_world);
        printf("\n");
        d();
        wan::delegate<void()> d1(d);
        printf("%u\n", sizeof(d));
        d.add(d);
        d();
        printf("\n");
        d.remove(d1);
        d();
    }

    {
        wan::delegate<void()> d;
        d.add(new Object(), &Object::print, true);
        d();
        wan::delegate<void()> d1(d);
        wan::delegate<void()>* d2 = d.clone();
        d1();
        (*d2)();
        delete d2;
    }

    {
        Object obj;
        obj.test();
    }

    {
        wan::delegate<void()> d;
        d.add(new Functor(), true);
        d();
    }
}

Thanks,
Kevin

On 2/10/06, Martin Wille <mw8329_at_[hidden]> wrote:
>
> Kevin Wan wrote:
> > as title!
>
> http://boost.org/more/submission_process.htm is linked from
> http://boost.org/ under Contribute/Submissions.
>
>
> HTH,
> m
> Send instant messages to your online friends http://au.messenger.yahoo.com
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>

--
Things don't just happen, you have to make them happen.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk