365 #ifndef STATE_CONTROLLER_H
366 #define STATE_CONTROLLER_H
369 #include <boost/shared_ptr.hpp>
370 #include <boost/make_shared.hpp>
371 #include <boost/type_traits/is_base_of.hpp>
372 #include <boost/static_assert.hpp>
384 #define NewStateInstance boost::make_shared
393 namespace statecontroller
440 , m_wasActivated(false)
441 , m_wasExecuted(false)
442 , m_wasDeactivated(false)
443 , m_firstExecCycle(0)
474 void callActivate(
cycle_t cyc);
476 void callDeactivate(
cycle_t cyc);
481 bool m_wasDeactivated;
495 template <
class SCClass>
496 class GenState :
public State
500 typedef boost::is_base_of<StateController, SCClass> Assert;
501 BOOST_STATIC_ASSERT_MSG(Assert::value,
"SCClass template parameter must be a derived class of statecontroller::StateController");
540 void clear() { QueueVec.resize(0); }
555 void remove(
index_t ind) {
if(ind < QueueVec.size()) QueueVec.erase (QueueVec.begin() + ind); }
559 bool isEmpty()
const {
return QueueVec.empty(); }
582 std::vector<StatePtr> QueueVec;
656 void freeResources();
680 return &(
scref()->Q);
StatePtr tailItem() const
Returns nullStatePtr if queue is empty.
Definition: state_controller.h:566
void reset()
Reinitialise the StateQueue object.
Definition: state_controller.cpp:55
void append(StatePtr state)
Always successful.
Definition: state_controller.h:551
StatePtr getNextState() const
Returns nullStatePtr if queue is empty.
Definition: state_controller.h:550
StatePtr advance()
Returns nullStatePtr if queue is empty.
Definition: state_controller.cpp:62
virtual ~GenState()
Destructor.
Definition: state_controller.h:505
void insertBefore(index_t ind, StatePtr state)
Does nothing if ind is out of range or nullIndex (in this case the allowed range is up to QueueVec...
Definition: state_controller.h:553
bool containsState(StatePtr state) const
Always successful.
Definition: state_controller.h:573
StateController(const std::string &name)
Default constructor.
Definition: state_controller.h:601
StateQueue()
Default constructor.
Definition: state_controller.h:529
bool belongsToMe(StateConstPtr state) const
Check whether a state is owned by the calling state controller.
Definition: state_controller.h:616
virtual void onTerminateCallback()
A callback that is executed in the step() function (before the resources are freed) when a state cont...
Definition: state_controller.h:643
void reserveCapacity(index_t n)
Increase the queue capacity so that it is at least n (does nothing if the capacity is already at leas...
Definition: state_controller.h:542
GenState(SCClass *sc, int id, const std::string &name)
Default constructor that takes a pointer to the owning state controller class (SCClass must be a deri...
Definition: state_controller.h:504
bool validIndex(index_t ind) const
Always successful (function returns false for nullIndex)
Definition: state_controller.h:560
action_t goToState(StatePtr state)
Transition to the specified next state (for callback)
Definition: state_controller.cpp:119
StateQueue * Queue() const
Return pointer to the StateQueue of the owning state controller.
Definition: state_controller.h:677
SCClass *const sc
Pointer to the owning state controller (required in order to be able to access the StateQueue and man...
Definition: state_controller.h:512
index_t indexOf(int id, index_t start=0) const
Returns nullIndex if the given ID is not found.
Definition: state_controller.cpp:87
boost::shared_ptr< State > StatePtr
Used to represent a Boost shared pointer to a State object.
Definition: state_controller.h:412
virtual ~StateController()
Destructor.
Definition: state_controller.h:602
Signals successful execution of function.
Definition: state_controller.h:404
const std::string scname
The human-friendly name of the owning state controller.
Definition: state_controller.h:450
void prepend(StatePtr state)
Always successful.
Definition: state_controller.h:552
index_t length() const
Always successful.
Definition: state_controller.h:558
void insertAfter(index_t ind, StatePtr state)
Does nothing if ind is out of range or nullIndex.
Definition: state_controller.h:554
ret_t step()
Execute a single step of the state controller (callback-protected)
Definition: state_controller.cpp:237
action_t terminate()
Terminate the execution of the state controller (for callback)
Definition: state_controller.cpp:145
index_t headIndex() const
Returns nullIndex if queue is empty.
Definition: state_controller.h:563
Signals a clean termination of the state controller.
Definition: state_controller.h:405
ret_t forceState(StatePtr state)
Force a change of state of the state controller (callback-protected)
Definition: state_controller.cpp:212
boost::shared_ptr< State const > StateConstPtr
Used to represent a Boost shared pointer to a constant State object.
Definition: state_controller.h:413
const int id
The unique numeric ID of the state type.
Definition: state_controller.h:448
Base class for all state controllers.
Definition: state_controller.h:593
Implements a state that a state controller can be in.
Definition: state_controller.h:432
cycle_t execCycleNum(cycle_t cyc) const
The number of cycles since the first call to execute(), including the initial call cycle and the curr...
Definition: state_controller.h:470
const std::string name
The human-friendly name of the state type.
Definition: state_controller.h:449
unsigned int cycle_t
Used to count the number of executed state controller cycles.
Definition: state_controller.h:415
const action_t HOLD_THIS_STATE
Specifies that the state controller should remain in its current state in the next step...
Definition: state_controller.h:422
void set(index_t ind, StatePtr state)
Does nothing if ind is out of range or nullIndex.
Definition: state_controller.h:546
void clear()
For use by state controller states (guaranteed not to cause vector reallocation)
Definition: state_controller.h:540
virtual action_t execute(cycle_t cyc)=0
State execution callback.
void clear_hard()
For use during reset/initialisation (may trigger vector reallocation)
Definition: state_controller.h:541
bool action_t
Used to represent the state transition actions (HOLD_THIS_STATE and PROCEED_NEXT_STATE) ...
Definition: state_controller.h:416
StateConstPtr getCurState() const
Return a constant reference to the current state.
Definition: state_controller.h:619
cycle_t firstExecCycle() const
The cycle ID of the first call to execute() for this state instance.
Definition: state_controller.h:469
virtual StateController * scref() const =0
Return pointer to owning state controller.
virtual void preExecuteCallback()
A callback that is executed in the step() function immediately prior to the execution of the current ...
Definition: state_controller.h:641
ret_t init(StatePtr state)
Reset and initialise the state controller with a particular state (callback-protected) ...
Definition: state_controller.cpp:176
int ret_t
Used to represent the return codes from state controller functions.
Definition: state_controller.h:417
static bool isNull(StateConstPtr state)
Return whether a StatePtr is null.
Definition: state_controller.h:453
virtual void preActivateCallback(bool willCallActivate)
A callback that is executed in the step() function immediately prior to the activation of a state (ca...
Definition: state_controller.h:640
StatePtr headItem() const
Returns nullStatePtr if queue is empty.
Definition: state_controller.h:565
Implements a dynamic State queue.
Definition: state_controller.h:522
bool containsID(int id) const
Always successful.
Definition: state_controller.h:572
ret_t loop()
Execute state controller steps until either an error or clean termination occurs (callback-protected)...
Definition: state_controller.cpp:311
cycle_t getCycle() const
Return the current cycle count (zero after reset)
Definition: state_controller.h:618
const std::string name
The human-friendly name of the state controller.
Definition: state_controller.h:605
virtual ~State()
Destructor.
Definition: state_controller.h:445
Specialises the State class using templates to take care of the most common overloads.
Definition: state_controller.h:397
bool isEmpty() const
Always successful.
Definition: state_controller.h:559
static const index_t DEF_QUEUE_CAPACITY
Default queue capacity to reserve in memory on reset.
Definition: state_controller.h:526
void setNextState(StatePtr state)
Clears the entire queue and sets the next state to the given state instance (use only with simple nex...
Definition: state_controller.h:549
virtual ~StateQueue()
Destructor.
Definition: state_controller.h:530
State(int id, const std::string &name, const std::string &scname)
Default constructor.
Definition: state_controller.h:436
Signals that a state was encountered that belongs to another state controller instance (or none at al...
Definition: state_controller.h:407
const StatePtr nullStatePtr
Null StatePtr.
Definition: state_controller.h:420
std::size_t index_t
Used to represent an array/vector/queue index (this MUST be an unsigned type)
Definition: state_controller.h:414
virtual void deactivate(cycle_t cyc, bool wasExecuted)
State deactivation callback.
Definition: state_controller.h:465
index_t tailIndex() const
Returns nullIndex if queue is empty.
Definition: state_controller.h:564
Signals that a callback tried to call a function in StateController that it isn't supposed to...
Definition: state_controller.h:408
virtual void activate(cycle_t cyc)
State activation callback.
Definition: state_controller.h:463
Signals that a null state was encountered (where it wasn't expected)
Definition: state_controller.h:406
virtual StateController * scref() const
Return pointer to owning state controller.
Definition: state_controller.h:509
SCReturnID
Used to specify return values and error codes of state controller functions.
Definition: state_controller.h:402
StatePtr stateOf(int id, index_t start=0) const
Returns nullStatePtr if the given ID is not found.
Definition: state_controller.cpp:76
const action_t PROCEED_NEXT_STATE
Specifies that the state controller should advance the queue and retrieve a new State in the next ste...
Definition: state_controller.h:423
virtual bool postExecuteCallback()
A callback that is executed in the step() function immediately after the execution of the current sta...
Definition: state_controller.h:642
virtual void postStepCallback()
A callback that is executed at the very end of the step() function whenever the current state is not ...
Definition: state_controller.h:644
virtual bool preStepCallback()
A callback that is executed at the very beginning of the step() function, right after the cycle count...
Definition: state_controller.h:639
const index_t nullIndex
Null index_t (nullIndex is guaranteed to exceed std::vector::max_size(), so there is no loss here) ...
Definition: state_controller.h:421
bool isActive() const
Return whether the state controller is active or not.
Definition: state_controller.h:617