accessing derived methods via base class (design woes)

M

ma740988

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
 
M

Michael DOUBEZ

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top