templatized state model

S

skscpp

Does anyone know of any c++ templates that can be used for state
representation (i.e. state models)?

My question is vague since I am looking for references (websites or books)
where templates can be used to model states.

Thanks.
 
K

Kasper van den Berg

Does anyone know of any c++ templates that can be used for state
representation (i.e. state models)? It can be done. (See below)

My question is vague since I am looking for references (websites or books)
where templates can be used to model states.
Unfortunatly i don't know any books or websites.

Depending on what you exactly want the solutions differ extremely. But
here is one example (although there would be other and probably better
solutions to do only the things i do in the example, but you probably
have some better use for templates representing states):

template <class State> class MyClass{
private:
State myState;

template <class NewState> MyClass<NewState> copyAndChangeState(ns)
{
MyClass<NewState> objectWithChangedState;
// copy all needed data from this to objectWithChangedState;
// initialise the state with information from ns.
}

template <class OldState, class NewState> changeState(OldState o, NewState n)
{
// the default behaviour is that the transition is illegal.
// Specializations of this template method are needed to define
// possible transitions.
throw EInvallidStateTransition();
}

template <> changeState(State1 s1, State2 s2)
// A specialization enabling transitions from Sate1 to State2
{
s1.leave();
s2.enter();
}

public:
template <class NewState> MyClass<NewState> transition(NewState ns)
{
changeState(myState, ns);
// unfortiunatly we have to create an new object an return that.
// But you might be able to use some pointers to avoid this.
return copyAndChangeState(ns);
}
}

Good luck. If you have more questions or i understood you completely wrong
let me know (could you please email me a copy also?)

Greets,
Kasper
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top