I would suggest that FooProcessor can only process Foo, and BarProcessor can only process Bar, but other types can have more than one processor class. So you can do this obsessively:
class FooProcessor { public: typedef Foo value_type; }; class BarProcessor { public: typedef Bar value_type; };
You can use polymorphism:
template< typename T > class Processor { public: typedef T value_type; virtual ~Processor() {} virtual void process( value_type * ) = 0; }; class FooProcessor : public Processor<Foo> {
You can use the adapter class, for example Matt Phillips, but in the reverse order, so the process class is required as a template parameter:
template<typename T> class Spec { }; template<> class Spec<FooProcessor> { typedef Foo type; }; template<> class Spec<Bar> { typedef BarProcessor type; };
With an intrusive type and a Spec adapter, typing your ProcessEvent template will take the processor type as a parameter and select another one using value_type or type.
With polymorphism, your ProcessEvent will take the type of the object as a parameter (Foo or Bar) and a processor will be transferred that comes from the Processor or Processor for processing events.
If there are a huge number of events to process and it always processes them with the same object, the latter method, of course, will be somewhat less efficient, since it processes the v-table. Partly depends on how much time they spend processing, and whether the function that does this can be built-in.
source share