47 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
48 #if defined(SMC_USES_IOSTREAMS)
50 #endif // SMC_USES_IOSTREAMS
51 #if defined(SMC_NO_EXCEPTIONS)
53 #endif // SMC_NO_EXCEPTIONS
56 #if defined(SMC_USES_IOSTREAMS)
58 #endif // SMC_USES_IOSTREAMS
59 #if defined(SMC_NO_EXCEPTIONS)
61 #endif // SMC_NO_EXCEPTIONS
64 #if defined(SMC_USES_IOSTREAMS)
66 #endif // SMC_USES_IOSTREAMS
67 #if defined(SMC_NO_EXCEPTIONS)
69 #endif // SMC_NO_EXCEPTIONS
72 #if !defined(SMC_NO_EXCEPTIONS)
79 #define MAX_NAME_LEN 100
86 #ifdef SMC_FIXED_STACK
88 inline char* copyString(
const char* s)
91 return (const_cast<char*>(s));
93 #else // ! SMC_FIXED_STACK
94 inline char *copyString(
const char *s)
100 retval =
new char[MAX_NAME_LEN + 1];
101 retval[MAX_NAME_LEN] =
'\0';
102 (void)std::strncpy(retval, s, MAX_NAME_LEN);
107 #endif // ! SMC_FIXED_STACK
113 #ifndef SMC_NO_EXCEPTIONS
127 : std::runtime_error(reason){};
141 #ifdef SMC_FIXED_STACK
142 class PushOnFullStateStackException :
public SmcException
149 PushOnFullStateStackException()
153 virtual ~PushOnFullStateStackException() throw(){};
203 :
SmcException(
"transition invoked while in transition"){};
228 :
SmcException(
"no such transition in current state"), _state(NULL), _transition(NULL){};
233 const char* transition)
234 :
SmcException(
"no such transition in current state"), _state(copyString(state)), _transition(copyString(transition)){};
239 :
SmcException(
"no such transition in current state"), _state(copyString(ex._state)), _transition(copyString(ex._transition)){};
250 if (_transition != NULL)
252 delete[] _transition;
270 if (_transition != NULL)
272 delete[] _transition;
276 _state = copyString(ex._state);
277 _transition = copyString(ex._transition);
284 const char* getState()
const
290 const char* getTransition()
const
292 return (_transition);
317 :
SmcException(
"index out of bounds"), _index(0), _minIndex(0), _maxIndex(0){};
324 :
SmcException(
"index out of bounds"), _index(index), _minIndex(minIndex), _maxIndex(maxIndex){};
329 :
SmcException(
"index out of bounds"), _index(ex._index), _minIndex(ex._minIndex), _maxIndex(ex._maxIndex){};
342 _minIndex = ex._minIndex;
343 _maxIndex = ex._maxIndex;
356 int getMinIndex()
const
362 int getMaxIndex()
const
379 #endif // !SMC_NO_EXCEPTIONS
391 const char* getName()
const
402 State(
const char* name,
int stateId)
403 : _name(NULL), _stateId(stateId)
407 _name = copyString(name);
411 _name = copyString(
"NAME NOT SET");
417 #ifndef SMC_FIXED_STACK
463 StateEntry(
State* state, StateEntry* next)
464 : _state(state), _next(next){};
477 StateEntry* getNext()
506 #ifdef SMC_FIXED_STACK
508 #else // ! SMC_FIXED_STACK
511 if (_transition != NULL)
513 delete[] _transition;
518 while (_state_stack != NULL)
520 state = _state_stack;
521 _state_stack = _state_stack->_next;
524 #endif // ! SMC_FIXED_STACK
543 virtual void enterStartState() = 0;
548 return (
this == &fsm);
554 return (_debug_flag);
559 void setDebugFlag(
bool flag)
565 #ifdef SMC_USES_IOSTREAMS
567 std::ostream& getDebugStream()
569 return (*_debug_stream);
573 void setDebugStream(std::ostream& debug_stream)
575 _debug_stream = &debug_stream;
578 #endif // SMC_USES_IOSTREAMS
582 bool isInTransition()
const
584 return (_state == NULL ?
true :
false);
589 char* getTransition()
591 return (_transition);
596 void setTransition(
const char* transition)
598 #ifndef SMC_FIXED_STACK
599 if (_transition != NULL)
601 delete[] _transition;
604 #endif // ! SMC_FIXED_STACK
606 _transition = copyString(transition);
614 _previous_state = _state;
620 State* getPreviousState()
622 return (_previous_state);
626 void setState(
const State& state)
634 _previous_state = _state;
637 _state =
const_cast<State*
>(&state);
639 if (_debug_flag ==
true)
641 #ifdef SMC_USES_IOSTREAMS
642 *_debug_stream <<
"ENTER STATE : "
646 TRACE(
"ENTER STATE : %s\n",
648 #endif // SMC_USES_IOSTREAMS
652 #ifdef SMC_FIXED_STACK
655 bool isStateStackEmpty()
const
657 return (_state_stack_depth == 0);
661 int getStateStackDepth()
const
663 return (_state_stack_depth);
668 void pushState(
const State& state)
670 #ifdef SMC_NO_EXCEPTIONS
671 assert(_state_stack_depth < SMC_STATE_STACK_SIZE);
673 if (_state_stack_depth == SMC_STATE_STACK_SIZE)
675 throw PushOnFullStateStackException();
683 _state_stack[_state_stack_depth] = _state;
684 ++_state_stack_depth;
687 _previous_state = _state;
688 _state =
const_cast<State*
>(&state);
690 if (_debug_flag ==
true)
692 #ifdef SMC_USES_IOSTREAMS
693 *_debug_stream <<
"PUSH TO STATE : "
697 TRACE(
"PUSH TO STATE : %s\n",
699 #endif // SMC_USES_IOSTREAMS
708 #ifdef SMC_NO_EXCEPTIONS
709 assert(_state_stack_depth > 0);
711 if (_state_stack_depth == 0)
717 _previous_state = _state;
718 --_state_stack_depth;
719 _state = _state_stack[_state_stack_depth];
721 if (_debug_flag ==
true)
723 #ifdef SMC_USES_IOSTREAMS
724 *_debug_stream <<
"POP TO STATE : "
728 TRACE(
"POP TO STATE : %s\n",
730 #endif // SMC_USES_IOSTREAMS
735 void emptyStateStack()
737 _state_stack_depth = 0;
739 #else // ! SMC_FIXED_STACK
742 bool isStateStackEmpty()
const
744 return (_state_stack == NULL);
748 int getStateStackDepth()
const
750 StateEntry *state_ptr;
753 for (state_ptr = _state_stack, retval = 0;
755 state_ptr = state_ptr->getNext(), ++retval)
763 void pushState(
const State &state)
765 StateEntry *new_entry;
771 new_entry =
new StateEntry(_state, _state_stack);
772 _state_stack = new_entry;
775 _previous_state = _state;
776 _state =
const_cast<State *
>(&state);
778 if (_debug_flag ==
true)
780 #ifdef SMC_USES_IOSTREAMS
781 *_debug_stream <<
"PUSH TO STATE : "
785 TRACE(
"PUSH TO STATE : %s\n",
787 #endif // SMC_USES_IOSTREAMS
798 #ifdef SMC_NO_EXCEPTIONS
799 assert(_state_stack != NULL);
801 if (_state_stack == NULL)
805 #endif // SMC_NO_EXCEPTIONS
807 _previous_state = _state;
808 _state = _state_stack->getState();
809 entry = _state_stack;
810 _state_stack = _state_stack->getNext();
813 if (_debug_flag ==
true)
815 #ifdef SMC_USES_IOSTREAMS
816 *_debug_stream <<
"POP TO STATE : "
820 TRACE(
"POP TO STATE : %s\n",
822 #endif // SMC_USES_IOSTREAMS
827 void emptyStateStack()
829 StateEntry *state_ptr,
832 for (state_ptr = _state_stack;
834 state_ptr = next_ptr)
836 next_ptr = state_ptr->getNext();
842 #endif // ! SMC_FIXED_STACK
847 : _state(const_cast<State*>(&state)), _previous_state(NULL),
848 #ifdef SMC_FIXED_STACK
849 _state_stack_depth(0)
857 #ifdef SMC_USES_IOSTREAMS
859 , _debug_stream(&std::cerr)
862 #endif // SMC_USES_IOSTREAMS
884 State* _previous_state;
887 #ifdef SMC_FIXED_STACK
888 State* _state_stack[SMC_STATE_STACK_SIZE];
889 int _state_stack_depth;
891 StateEntry *_state_stack;
904 #ifdef SMC_USES_IOSTREAMS
908 std::ostream* _debug_stream;
909 #endif // SMC_USES_IOSTREAMS