Dynamically appending custom data to an object

Discussion in 'C++' started by Luc Tremblay, Jul 5, 2004.

  1. Luc Tremblay

    Luc Tremblay Guest

    Given the typical following code:

    void Listener::HandleEvent(const Event& event)
    {
    // handling code
    }

    In a "clean" fashion, how is it possible to add custom data (to be
    subsequently accessed) to the Event instance? By custom data i mean
    practically anything, from a class to a single int. Particularly to my case,
    it would be assumed that such data would be appropriately destroyed as the
    Event is consumed/destroyed.

    I have considered several options but still can't find one that satisfies
    the "clean" requisite. One implies keeping a void* "userData" in the Event
    class, and using setData<T>/getData<T> template methods to access and
    automagically cast the data to the proper type, with type safety checks in
    debug. Problems arise if I want the Event class to handle the destruction of
    the custom data because internally to Event, the custom data's type is
    unknown. Again particularly to my case, I could use a memory pool for the
    custom data, write the custom data to it using placement new and then clean
    the pool after each frame as all events are considered "consumed" between
    event iterations. Potential problems arise if classes are used as custom
    data, since cleaning the memory pool wouldn't call the destructors of the
    custom data.

    I would guess Boost::any would also be a solution, but we are trying to
    avoid using it in our project.

    Subclassing the Event class and adding the custom data as members, and then
    downcasting to the proper type, is probably also a solution. I am thinking
    it is possibly the only interesting solution (as it is very common), but I
    hope to think someone thought of an elegant alternative. I think it is
    likely that there is a templated solution, yet i can't figure it out.

    It has to be kept in mind that the Event class, in my case, would
    potentially be part of an external library, so using a union member of all
    possible custom data types isn't a viable solution (not to say it is quite
    ugly...)

    Regards,

    Luc Tremblay
     
    Luc Tremblay, Jul 5, 2004
    #1
    1. Advertising

  2. Luc Tremblay wrote:
    >
    > "John Harrison" wrote
    > > On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay
    > > wrote:
    > >
    > > Assuming HandleEvent is not virtual then the following might work

    >
    > Unfortunately, in my case, it indeed needs to be virtual.



    Then you could rearrange it like this:

    class EventBase {
    // standard event stuff
    };

    void Listener::HandleEvent(const EventBase& event)
    {
    // handling code
    }

    template <class T>
    class Event : public EventBase {
    // user data
    T user;
    };

    Since Listener::HandleEvent() seems pretty much fixed in that
    that it receives a reference of some base type of event, the
    real question is how you do the dispatching based on the
    dynamic type of an event. I think the solution may depend
    a lot on the typical kind of the user data T and the way you
    have to handle it.

    For the simplest case, if handling of all possible types of
    the user data could be provided in a uniform way:

    class Action {
    public:
    template <typename T>
    void handle(T const& v);
    };

    then declaring the following function in EventBase
    virtual void handleUserData(Action& pa) const = 0;

    and implementing it in Event<T> as
    void handleUserData(Action& a) const {a.handle(user);}

    would solve the most of the problem.

    Denis
     
    Denis Remezov, Jul 6, 2004
    #2
    1. Advertising

  3. Luc Tremblay

    Luc Tremblay Guest

    "John Harrison" wrote
    > On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay
    > wrote:
    >
    > Assuming HandleEvent is not virtual then the following might work


    Unfortunately, in my case, it indeed needs to be virtual.
     
    Luc Tremblay, Jul 6, 2004
    #3
  4. On Mon, 5 Jul 2004 18:00:03 -0300, Luc Tremblay <>
    wrote:

    > Given the typical following code:
    >
    > void Listener::HandleEvent(const Event& event)
    > {
    > // handling code
    > }
    >
    > In a "clean" fashion, how is it possible to add custom data (to be
    > subsequently accessed) to the Event instance? By custom data i mean
    > practically anything, from a class to a single int. Particularly to my
    > case,
    > it would be assumed that such data would be appropriately destroyed as
    > the
    > Event is consumed/destroyed.
    >


    [snip]

    > I
    > hope to think someone thought of an elegant alternative. I think it is
    > likely that there is a templated solution, yet i can't figure it out.
    >


    Assuming HandleEvent is not virtual then the following might work

    template <class T>
    void Listener::HandleEvent(const Event<T>& event)
    {
    // handling code
    }

    with Event defined like this

    template <class T>
    class Event
    {
    // standard event stuff
    ...
    // user data
    T user;
    };

    Something to think about anyway.

    john
     
    John Harrison, Jul 6, 2004
    #4
  5. Luc Tremblay

    Luc Tremblay Guest

    "Raghupathy" wrote :

    > How about having an interface and call it as ICustData, with a virtual
    > destructor.
    >
    > Any class which wants to be appendable will need to implement this
    > interface.
    > Your host class will then have a member ICustData* m_pCustData, with
    > get/set methods to access the ICustData member. The client can then
    > use dynamic_cast to get the actual pointer that he needs.
    >
    > And when the host class goes out of scope, just call delete on
    > m_pCustData. Since the destructor is virtual, the correct destructor
    > will be called.
    >
    > It's a pretty straight forward solution, but is this what you are
    > looking for? And do you find it intutive?
    >


    Unfortunately your solution rules out base types, which is unacceptable in
    my case.
     
    Luc Tremblay, Jul 6, 2004
    #5
  6. Luc Tremblay

    Raghupathy Guest

    How about having an interface and call it as ICustData, with a virtual
    destructor.

    Any class which wants to be appendable will need to implement this
    interface.
    Your host class will then have a member ICustData* m_pCustData, with
    get/set methods to access the ICustData member. The client can then
    use dynamic_cast to get the actual pointer that he needs.

    And when the host class goes out of scope, just call delete on
    m_pCustData. Since the destructor is virtual, the correct destructor
    will be called.

    It's a pretty straight forward solution, but is this what you are
    looking for? And do you find it intutive?



    "Luc Tremblay" <> wrote in message news:<z9kGc.11525$>...
    > Given the typical following code:
    >
    > void Listener::HandleEvent(const Event& event)
    > {
    > // handling code
    > }
    >
    > In a "clean" fashion, how is it possible to add custom data (to be
    > subsequently accessed) to the Event instance? By custom data i mean
    > practically anything, from a class to a single int. Particularly to my case,
    > it would be assumed that such data would be appropriately destroyed as the
    > Event is consumed/destroyed.
    >
    > I have considered several options but still can't find one that satisfies
    > the "clean" requisite. One implies keeping a void* "userData" in the Event
    > class, and using setData<T>/getData<T> template methods to access and
    > automagically cast the data to the proper type, with type safety checks in
    > debug. Problems arise if I want the Event class to handle the destruction of
    > the custom data because internally to Event, the custom data's type is
    > unknown. Again particularly to my case, I could use a memory pool for the
    > custom data, write the custom data to it using placement new and then clean
    > the pool after each frame as all events are considered "consumed" between
    > event iterations. Potential problems arise if classes are used as custom
    > data, since cleaning the memory pool wouldn't call the destructors of the
    > custom data.
    >
    > I would guess Boost::any would also be a solution, but we are trying to
    > avoid using it in our project.
    >
    > Subclassing the Event class and adding the custom data as members, and then
    > downcasting to the proper type, is probably also a solution. I am thinking
    > it is possibly the only interesting solution (as it is very common), but I
    > hope to think someone thought of an elegant alternative. I think it is
    > likely that there is a templated solution, yet i can't figure it out.
    >
    > It has to be kept in mind that the Event class, in my case, would
    > potentially be part of an external library, so using a union member of all
    > possible custom data types isn't a viable solution (not to say it is quite
    > ugly...)
    >
    > Regards,
    >
    > Luc Tremblay
     
    Raghupathy, Jul 6, 2004
    #6
  7. Luc Tremblay

    Luc Tremblay Guest

    Luc Tremblay wrote :

    > In a "clean" fashion, how is it possible to add custom data (to be
    > subsequently accessed) to the Event instance? By custom data i mean
    > practically anything, from a class to a single int. Particularly to my

    case,
    > it would be assumed that such data would be appropriately destroyed as the
    > Event is consumed/destroyed.


    I should correct myself. I do not mean dynamically as in "during process
    itself"; a pre-definition of the data type to use for each Event is
    possible, considering Events are uniquely defined by their UId. This leads
    me to think that a solution would be a templated factory that maps Event Ids
    to data types.
     
    Luc Tremblay, Jul 6, 2004
    #7
  8. * Luc Tremblay:
    > Given the typical following code:
    >
    > void Listener::HandleEvent(const Event& event)
    > {
    > // handling code
    > }
    >
    > In a "clean" fashion, how is it possible to add custom data (to be
    > subsequently accessed) to the Event instance? By custom data i mean
    > practically anything, from a class to a single int. Particularly to my case,
    > it would be assumed that such data would be appropriately destroyed as the
    > Event is consumed/destroyed.


    You need downcasting, but that downcasting should be centralized, not
    forced upon each event consumer.

    To centralize the downcasting use the visitor pattern.

    // General event definitions.

    class Event;
    class IEventConsumer
    {
    protected:
    virtual onEvent( Event const& anEvent ) {};
    };


    class Event
    {
    public:
    virtual void visit( IEventConsumer& aConsumer )
    {
    aConsumer.onEvent( *this );
    };
    };


    // A particular event with custom data.

    class CustomData;
    class CustomEvent;

    class ICustomEventConsumer: public IEventConsumer
    {
    protected:
    virtual onCustomEvent( CustomEvent const& anEvent ) {};
    };

    class CustomEvent: public Event
    {
    public:
    typedef Event Base;

    virtual void visit( IEventConsumer& aConsumer )
    {
    ICustomEventConsumer* pi = dynamic_cast<ICustomEventConsumer*>( &aConsumer );
    if( pi != NULL )
    {
    pi->onCustomEvent( *this );
    }
    else
    {
    Base::visit( aConsumer );
    }
    }

    CustomData const& data() const { ... }
    };


    // A client

    class Client: protected ICustomEventConsumer
    {
    protected:
    virtual void onCustomEvent( CustomEvent const& anEvent )
    {
    CustomData const& = anEvent.data();¨

    // Whatever.
    }
    };


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 6, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davíð Þórisson

    Appending raw data to placeholders

    Davíð Þórisson, Oct 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    343
    =?Utf-8?B?V2ViTWF0cml4?=
    Oct 24, 2004
  2. Red Ogden
    Replies:
    0
    Views:
    1,120
    Red Ogden
    Jul 16, 2003
  3. Thomas Heller

    appending data to an xml file

    Thomas Heller, Jan 11, 2005, in forum: Python
    Replies:
    1
    Views:
    370
    Peter Hansen
    Jan 12, 2005
  4. Emmanuel

    Appending Data to XML File

    Emmanuel, Oct 13, 2006, in forum: Java
    Replies:
    3
    Views:
    334
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Oct 15, 2006
  5. Emmanuel

    appending data to xml file

    Emmanuel, Oct 13, 2006, in forum: XML
    Replies:
    2
    Views:
    461
    Richard Tobin
    Oct 13, 2006
Loading...

Share This Page