accessing derived methods via base class (design woes)
Consider
# include <iostream>
# include <vector>
# include <typeinfo>
# include <string>
class BaseMsg {
friend std::ostream& operator<<( std::ostream&, const BaseMsg& );
std::string name ;
public :
std::string get_name() const { return name ; }
BaseMsg( std::string const & str_ )
: name ( str_ )
{}
virtual std::ostream& print( std::ostream& os ) const { return os <<
"Base"; }
};
inline std::ostream& operator<<(std::ostream& os, const BaseMsg& b) {
return b.print(os);
}
class msg41K : public BaseMsg {
int const ac ;
public :
int ac_type () const { return ac; }
std::ostream& print(std::ostream& 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::ostream& print(std::ostream& 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::ostream& print(std::ostream& 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