K
Kevin
I'm creating a template to support state machines. In doing so, I
need to pass an enumeration for the number of transitions and a non
type parameter for the range of the enum (to allow me to allocate an
array of pointers big enough to hold one entry for each transition).
My template declaration looks roughly like this:
template <class TRANSITION, int NUMTRANSITIONS>
class StateMachine {
***SNIP***
void RegisterTransition(TRANSITION t, StateMachine *pState);
StateMachine *nextState[NUMTRANSITIONS];
***SNIP***
};
I invoke the template like so:
// Define an enumerated type for transitions
enum ToggleSwitch_t { ON, OFF, TOGGLE };
#define MAX_TRANSITION 3
// Set up a pointer to current state
StateMachine<ToggleSwitch_t, MAX_TRANSITION> *pCurrState;
// Create states
StateMachine<ToggleSwitch_t, MAX_TRANSITION>LightOn("LIGHT ON");
StateMachine<ToggleSwitch_t, MAX_TRANSITION> LightOff("LIGHT OFF");
// Set up transitions
LightOn.RegisterTransition(ON, &LightOn);
LightOn.RegisterTransition(OFF, &LightOff);
LightOn.RegisterTransition(TOGGLE, &LightOff);
LightOff.RegisterTransition(ON, &LightOn);
LightOff.RegisterTransition(OFF, &LightOff);
LightOff.RegisterTransition(TOGGLE, &LightOn);
It works fine as is, but I want to add a static array that will allow
me to index into states as an enum type. I could set up something
like
template <class TRANSITION, int NUMTRANSITIONS, class STATE, int
NUMSTATES>
but this is going to get very hairy very quickly. I would prefer if I
could somehow take the enum that's passed in and simply derive its
maximum from inside the template itself. That would give me something
easier to read like
template <class TRANSITION, class STATE>
I can't find any way to determine the max value of an enum. Most
previous posts are of the form "You can't do that - please try
workaround XYZ..." If I must, I could try using #define macros, but
that just seems to hide the mechanics rather than simplifying them,
which is a sure recipe for confusion for maintenance programming.
Does anyone have any suggestions?
Thanks,
Kevin
need to pass an enumeration for the number of transitions and a non
type parameter for the range of the enum (to allow me to allocate an
array of pointers big enough to hold one entry for each transition).
My template declaration looks roughly like this:
template <class TRANSITION, int NUMTRANSITIONS>
class StateMachine {
***SNIP***
void RegisterTransition(TRANSITION t, StateMachine *pState);
StateMachine *nextState[NUMTRANSITIONS];
***SNIP***
};
I invoke the template like so:
// Define an enumerated type for transitions
enum ToggleSwitch_t { ON, OFF, TOGGLE };
#define MAX_TRANSITION 3
// Set up a pointer to current state
StateMachine<ToggleSwitch_t, MAX_TRANSITION> *pCurrState;
// Create states
StateMachine<ToggleSwitch_t, MAX_TRANSITION>LightOn("LIGHT ON");
StateMachine<ToggleSwitch_t, MAX_TRANSITION> LightOff("LIGHT OFF");
// Set up transitions
LightOn.RegisterTransition(ON, &LightOn);
LightOn.RegisterTransition(OFF, &LightOff);
LightOn.RegisterTransition(TOGGLE, &LightOff);
LightOff.RegisterTransition(ON, &LightOn);
LightOff.RegisterTransition(OFF, &LightOff);
LightOff.RegisterTransition(TOGGLE, &LightOn);
It works fine as is, but I want to add a static array that will allow
me to index into states as an enum type. I could set up something
like
template <class TRANSITION, int NUMTRANSITIONS, class STATE, int
NUMSTATES>
but this is going to get very hairy very quickly. I would prefer if I
could somehow take the enum that's passed in and simply derive its
maximum from inside the template itself. That would give me something
easier to read like
template <class TRANSITION, class STATE>
I can't find any way to determine the max value of an enum. Most
previous posts are of the form "You can't do that - please try
workaround XYZ..." If I must, I could try using #define macros, but
that just seems to hide the mechanics rather than simplifying them,
which is a sure recipe for confusion for maintenance programming.
Does anyone have any suggestions?
Thanks,
Kevin