Changing which methods a class has by deriving from a template argument

A

Asfand Yar Qazi

Hi,

Is is possible to set the 'policy' of a class by allowing the user to specify
a template argument that defines what some functions will be based on the
template argument given?

Basically, it's the old 'make the user derive from a virtual base class' vs.
'use attachable callbacks (or in this case signals/slots)' problem - and I'm
thinking of solving it by allowing the user to choose.

So, I need to define an interface that allows users to attach 'callbacks' to
an object.

struct StateBody
{
virtual ~StateBody() {}
virtual void atentry() {}
virtula void atexit() {}
};

template<class PolicyT>
class StateMachine : public PolicyT
{
};

class MyStateBody : public StateBody
{
public:
void atentry() {...}
};

strict MyEntrySlot{ void operator()() {...} };

void my_exit_slot() {}

StateMachine<VObjectHandler<DerefCall> > sm1;

State<VObjectHandler<DerefCall> >& state1 = sm1.add_state("state1",
boost::shared_ptr<MyStateBody>(new MyStateBody()) );

StateMachine<VObjectHandler<DirectCall> > sm2;

CompositeState<VObjectHandler<DirectCall> >& state2 =
sm2.add_composite_state("state2", MyStateBody());

StateMachine<SlotHandler> sm3;

State<SlotHandler>& state3 =
sm3.add_state("state3", MyEntrySlot(), &my_exit_slot);

Would this be possible? What about feasibility? The 'add_state()' method in
each case has a different prototype - the template argument type has an
'add_state' method of the needed definition, and StateMachine derives from it
thereby obtaining it as well.

I'm also thinking of allowing the type used for the names "state1", "state2",
etc. to be configurable - i.e. using basic_string<CharT>, where CharT is
supplied as another template parameter.

Thoughts? Spot anything of concern?

Thanks,
Asfand Yar
 

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,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top