Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templatized state model

Reply
Thread Tools

templatized state model

 
 
skscpp
Guest
Posts: n/a
 
      09-08-2003
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.


 
Reply With Quote
 
 
 
 
Kasper van den Berg
Guest
Posts: n/a
 
      09-08-2003
On Mon, 08 Sep 2003 13:05:30 -0500, skscpp wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Templatized 'Random' member function. ma740988 C++ 3 11-06-2004 07:16 PM
templatized logging Luther Baker C++ 4 05-22-2004 05:55 AM
Templatized operator () overload Paul Escherton C++ 2 11-10-2003 11:18 AM
How do I load data to a templatized class hall C++ 2 10-29-2003 10:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57