Boost logo

Boost Users :

From: Saffi (yg-boost-users_at_[hidden])
Date: 2003-03-19 15:41:29


Hi
It is very common in Java when using pattern as EventListener having an
inner class Adapter as the listener.
I wanted that and more, I couldn't use "this" as the source of a shared ptr
since then we will have to ref cnt to the same object ...
Such scenario will cause a problem when one of the ref cnts will reach zero
will delete the object and the other ref will point to a destructed object.

The Problem :
how define an inner class which would delegate functionality of the main
class and break the ever annoying problem of giving ref to the main class.

In Short two things are required :
a. getSelf() functionality instead of "this" as a base for reference
b. a glue to connect inner class to outer (in Java in terms of this example
: ObjectWithAdapter.this)

----------------------------------------------------------------------------
--------------------------------------------------------------
Example :
ObjectWithAdapter : the main class that need to register as listener
IListener : listener interface
ObjectWithAdapter::Adapter is the implementation of the IListener and
delegates the message to the main class

class IListener
{
    virtual void helloListener(char *str)=0;
};

class ObjectWithAdapter : public TMe<ObjectWithAdapter>
{
    typedef RStrong<ObjectWithAdapter> RObjectWithAdapter;

public:
    void helloListener(char *str)
    {
        // called from adapter
    }

public:
    class Adapter : public TSon<ObjectWithAdapter>,
                    IListener
    {public:
        void helloListener(char *str)
        {
            if (getPapa())
            {
                getPapa()->helloListener(str);
            }
        }

        Adapter()
        {
            // register
        }
        virtual ~Adapter(void
            )
        {// unregister
        }

    };

    typedef RStrong<Adapter> RAdapter;

    RAdapter _adapter;

    ObjectWithAdapter()
    {
    }

    void initSons()
    {
        _adapter = RAdapter(new Adapter());
        _adapter->initSon(getSelf());
    }
    ~ObjectWithAdapter()
    {
    }

};
typedef RStrong<ObjectWithAdapter> RObjectWithAdapter;

----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------

Is such functionality is worthy piece of code for boost ?
BTW my implementation works fine for me.

    -- Saffi


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net