accessing derived methods via base class (design woes)

Discussion in 'C++' started by ma740988, Sep 17, 2008.

  1. ma740988

    ma740988 Guest

    Consider

    # include <iostream>
    # include <vector>
    # include <typeinfo>
    # include <string>

    class BaseMsg {
    friend std::eek:stream& operator<<( std::eek:stream&, const BaseMsg& );
    std::string name ;
    public :
    std::string get_name() const { return name ; }
    BaseMsg( std::string const & str_ )
    : name ( str_ )
    {}
    virtual std::eek:stream& print( std::eek:stream& os ) const { return os <<
    "Base"; }
    };
    inline std::eek:stream& operator<<(std::eek:stream& os, const BaseMsg& b) {
    return b.print(os);
    }

    class msg41K : public BaseMsg {
    int const ac ;
    public :
    int ac_type () const { return ac; }
    std::eek:stream& print(std::eek:stream& os) const {
    return os << "msg41K" << std::endl;
    }
    explicit msg41K ( int const ac_ )
    : BaseMsg ( "msg41K" )
    , ac ( ac_ )
    {}
    };
    class msg42K : public BaseMsg {
    int const cd ;
    public :
    int code_digit () const { return cd; }
    std::eek:stream& print(std::eek:stream& os) const {
    return os << "msg42K" << std::endl;
    }
    explicit msg42K ( int const cd_ )
    : BaseMsg ( "msg42K" )
    , cd ( cd_ )
    {}
    };

    // Event delegate interface class
    struct EvhDelegate {
    typedef void ( EvhDelegate::*handler_fp )( BaseMsg& );
    virtual void handle( BaseMsg& ) = 0;
    virtual ~EvhDelegate(){}
    };

    // ListenerA event handler
    struct ListenerA : EvhDelegate {
    void handle( BaseMsg& e ) {
    //e.print( typeid ( e ).name() ) ;
    //std::cout << typeid ( e ).name() << std::endl; OR
    std::cout << e.get_name() << std::endl;

    }
    };

    // ListenerB event handler
    struct ListenerB : EvhDelegate {
    void handle( BaseMsg& e ) {
    std::cout << typeid ( e ).name() << std::endl;
    }
    };

    // Our functor (modified to handle polymorphic functions)
    template < typename classT, typename memfuncT >
    class functor {
    memfuncT memfunc_;
    classT * class_;
    public:
    functor()
    : class_ ( 0 )
    , memfunc_( 0 )
    {}

    functor(classT * c, memfuncT f)
    : class_( c )
    , memfunc_( f )
    {}

    void operator()( BaseMsg& e ) {
    if( class_ && memfunc_ ) {
    (class_->*memfunc_)( e );
    }
    }
    ~functor() { delete class_; }
    };

    //////
    ///
    //////
    class Subject : public BaseMsg {
    public :
    std::eek:stream& print(std::eek:stream& os) const {
    return os << "Subject" << std::endl;
    }
    };


    // Friendly typedef of our functor for this specific usage
    typedef functor<EvhDelegate, EvhDelegate::handler_fp> EvhFunctor;
    class EventHandler {

    typedef std::vector<EvhFunctor *> handler_t;
    typedef handler_t::const_iterator hciter;
    typedef handler_t::iterator iter;
    handler_t handler_;

    public:
    // Log events and handlers in a map
    void Attach(EvhFunctor * func) {
    handler_.push_back( func );
    }

    // Extract handler from map and call polymorphic handle() function
    // Gets called in the context of the base class but dynamic binding
    ensures
    // the handler for the correct delegate class is called.
    void Notify( BaseMsg& e ) {
    for( hciter itr = handler_.begin() ; itr != handler_.end() ; +
    +itr) {
    EvhFunctor * func = (*itr);
    ( *func )( e );
    }
    }
    // Clear up the handlers
    ~EventHandler() {
    for( hciter itr = handler_.begin() ; itr != handler_.end() ; +
    +itr ) {
    delete *itr;
    }
    }
    };


    int main() {

    BaseMsg* ptr_41 = new ( std::nothrow ) msg41K ( 1 ) ;
    BaseMsg* ptr_42 = new ( std::nothrow ) msg42K ( 2 ) ;

    EventHandler eh;
    eh.Attach(new EvhFunctor( new ListenerA, &EvhDelegate::handle ) );
    eh.Attach(new EvhFunctor( new ListenerB, &EvhDelegate::handle ) );

    eh.Notify( *ptr_41 );
    eh.Notify( *ptr_42 );

    }



    The listeners upon receipt of msg41 or msg42 the Listeners prints the
    appropriate message. It's clear from the code that the messages are
    expressed to the Listeners via BaseMsg. Trouble is, I'd like for
    each Listener to first determine the type (msg41 or msg42 ), then
    print via cout "code_digit" or "ac_type" depending on the appropriate
    message.

    Since each message (msg41, msg42 .. msgNN) has unique methods, it
    doesn't make sense for me to create put every single method - pure
    virtual form - in the BaseMsg.

    The question then becomes. What is an alternate approach?

    Thanks
     
    ma740988, Sep 17, 2008
    #1
    1. Advertising

  2. ma740988 a écrit :
    > Consider
    > [snip]
    > The listeners upon receipt of msg41 or msg42 the Listeners prints the
    > appropriate message. It's clear from the code that the messages are
    > expressed to the Listeners via BaseMsg. Trouble is, I'd like for
    > each Listener to first determine the type (msg41 or msg42 ), then
    > print via cout "code_digit" or "ac_type" depending on the appropriate
    > message.


    In order to identify the underlying object you can:
    - use a unique id returned by the object
    - use dynamic_casts on the types you want to identify
    - add identification function to your base interface
    virtual bool has_ac()const;
    virtual bool has_cd()const;
    - use the visitor pattern

    Each method can have a use sometime, somewhere :)

    > Since each message (msg41, msg42 .. msgNN) has unique methods, it
    > doesn't make sense for me to create put every single method - pure
    > virtual form - in the BaseMsg.
    >
    > The question then becomes. What is an alternate approach?


    Reconsider your design: if you have a listener pattern, it means the
    object should be interested in events generated by an object
    implementing an interface, not underlying data.

    --
    Michael
     
    Michael DOUBEZ, Sep 18, 2008
    #2
    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. Abhijit Deshpande
    Replies:
    7
    Views:
    11,738
    samualmenon
    Jul 16, 2008
  2. Alicia
    Replies:
    3
    Views:
    949
    jjr2004a
    Nov 24, 2004
  3. Taran
    Replies:
    6
    Views:
    382
    Default User
    Apr 20, 2006
  4. Replies:
    1
    Views:
    396
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page