Skip to content

Commit

Permalink
+ added resumable variation of composite region
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-gresyk committed Apr 13, 2019
1 parent cc17c69 commit 858d55e
Show file tree
Hide file tree
Showing 49 changed files with 1,852 additions and 794 deletions.
20 changes: 12 additions & 8 deletions include/hfsm2/detail/control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ControlT {
template <StateID, typename, typename>
friend struct _S;

template <StateID, ShortIndex, ShortIndex, typename, typename, typename...>
template <StateID, ShortIndex, ShortIndex, typename, typename, RegionStrategy, typename...>
friend struct _C;

template <StateID, ShortIndex, ShortIndex, typename, typename, typename...>
Expand Down Expand Up @@ -64,10 +64,10 @@ class ControlT {
StateRegistry& stateRegistry,
PlanData& planData,
LoggerInterface* const HFSM_IF_LOGGER(logger))
: _context(context)
, _stateRegistry(stateRegistry)
, _planData(planData)
HFSM_IF_LOGGER(, _logger(logger))
: _context{context}
, _stateRegistry{stateRegistry}
, _planData{planData}
HFSM_IF_LOGGER(, _logger{logger})
{}

HFSM_INLINE void setOrigin (const StateID id);
Expand Down Expand Up @@ -150,7 +150,7 @@ class FullControlT
template <StateID, typename, typename>
friend struct _S;

template <StateID, ShortIndex, ShortIndex, typename, typename, typename...>
template <StateID, ShortIndex, ShortIndex, typename, typename, RegionStrategy, typename...>
friend struct _C;

template <StateID, ShortIndex, ShortIndex, typename, typename, typename...>
Expand Down Expand Up @@ -202,7 +202,7 @@ class FullControlT
Requests& requests,
LoggerInterface* const logger)
: Control{context, stateRegistry, planData, logger}
, _requests(requests)
, _requests{requests}
{}

template <typename T>
Expand All @@ -226,12 +226,16 @@ class FullControlT
using Control::plan;

HFSM_INLINE void changeTo(const StateID stateId);
HFSM_INLINE void restart (const StateID stateId);
HFSM_INLINE void resume (const StateID stateId);
HFSM_INLINE void schedule(const StateID stateId);

template <typename TState>
HFSM_INLINE void changeTo() { changeTo(stateId<TState>()); }

template <typename TState>
HFSM_INLINE void restart() { restart (stateId<TState>()); }

template <typename TState>
HFSM_INLINE void resume() { resume (stateId<TState>()); }

Expand Down Expand Up @@ -289,7 +293,7 @@ class GuardControlT
const Requests& pendingChanges,
LoggerInterface* const logger)
: FullControl{context, stateRegistry, planData, requests, logger}
, _pending(pendingChanges)
, _pending{pendingChanges}
{}

template <typename T>
Expand Down
21 changes: 20 additions & 1 deletion include/hfsm2/detail/control.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ template <typename TA>
ControlT<TA>::Origin::Origin(ControlT& control_,
const StateID id)
: control{control_}
, prevId(control._originId)
, prevId{control._originId}
{
control.setOrigin(id);
}
Expand Down Expand Up @@ -208,6 +208,25 @@ FullControlT<TA>::buildPlanStatus(const bool outerTransition) {
template <typename TA>
void
FullControlT<TA>::changeTo(const StateID stateId) {
if (!_locked) {
const Request request{Request::Type::CHANGE, stateId};
_requests << request;

if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

#if defined HFSM_ENABLE_LOG_INTERFACE || defined HFSM_VERBOSE_DEBUG_LOG
if (_logger)
_logger->recordTransition(_originId, Transition::CHANGE, stateId);
#endif
}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template <typename TA>
void
FullControlT<TA>::restart(const StateID stateId) {
if (!_locked) {
const Request request{Request::Type::RESTART, stateId};
_requests << request;
Expand Down
4 changes: 3 additions & 1 deletion include/hfsm2/detail/debug/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class Method : ShortIndex {
};

enum class Transition : ShortIndex {
CHANGE,
RESTART,
RESUME,
SCHEDULE,
Expand Down Expand Up @@ -85,7 +86,8 @@ static inline
const char*
transitionName(const Transition transition) {
switch (transition) {
case Transition::RESTART: return "changeTo";
case Transition::CHANGE: return "changeTo";
case Transition::RESTART: return "restart";
case Transition::RESUME: return "resume";
case Transition::SCHEDULE: return "schedule";

Expand Down
20 changes: 13 additions & 7 deletions include/hfsm2/detail/debug/structure_report.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ struct alignas(alignof(void*)) StructureStateInfo {
const RegionType region_,
const ShortIndex depth_,
const char* const name_)
: name(name_)
, parent(parent_)
, region(region_)
, depth(depth_)
: name{name_}
, parent{parent_}
, region{region_}
, depth{depth_}
{}

const char* name;
Expand All @@ -58,15 +58,21 @@ HFSM_INLINE get(const typename RequestT<TPayloadList>::Type type) {
using Request = RequestT<TPayloadList>;

switch (type) {
case Request::CHANGE:
return Transition::CHANGE;

case Request::RESTART:
return Transition::RESTART;

case Request::RESUME:
return Transition::RESUME;

case Request::SCHEDULE:
return Transition::SCHEDULE;

default:
HFSM_BREAK();
return Transition::RESTART;
return Transition::CHANGE;
}
}

Expand All @@ -82,8 +88,8 @@ struct alignas(4) TransitionInfoT {
HFSM_INLINE TransitionInfoT(const Request transition_,
const Method method_)
: stateId{transition_.stateId}
, method(method_)
, transition(get<PayloadList>(transition_.type))
, method{method_}
, transition{get<PayloadList>(transition_.type)}
{
HFSM_ASSERT(method_ < Method::COUNT);
}
Expand Down
62 changes: 36 additions & 26 deletions include/hfsm2/detail/state_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
namespace hfsm2 {
namespace detail {

//------------------------------------------------------------------------------

enum RegionStrategy {
Composite,
Resumable,
Utilitarian,
Selector,
};

////////////////////////////////////////////////////////////////////////////////

#pragma pack(push, 1)
Expand Down Expand Up @@ -40,6 +49,7 @@ struct RequestT {

enum Type {
REMAIN,
CHANGE,
RESTART,
RESUME,
SCHEDULE,
Expand Down Expand Up @@ -72,7 +82,7 @@ struct RequestT {
HFSM_ASSERT(type_ < Type::COUNT);
}

Type type = RESTART;
Type type = CHANGE;
StateID stateId = INVALID_STATE_ID;
PayloadBox payload;
};
Expand Down Expand Up @@ -151,28 +161,28 @@ struct StateRegistryT<ArgsT<TContext,

using Request = RequestT<PayloadList>;

static constexpr LongIndex STATE_COUNT = StateList::SIZE;
static constexpr ShortIndex COMPO_COUNT = NCompoCount;
static constexpr ShortIndex ORTHO_COUNT = NOrthoCount;
static constexpr ShortIndex ORTHO_UNITS = NOrthoUnits;
static constexpr LongIndex STATE_COUNT = StateList::SIZE;
static constexpr ShortIndex COMPO_COUNT = NCompoCount;
static constexpr ShortIndex ORTHO_COUNT = NOrthoCount;
static constexpr ShortIndex ORTHO_UNITS = NOrthoUnits;

using StateParents = StaticArray<Parent, STATE_COUNT>;

using CompoParents = StaticArray<Parent, COMPO_COUNT>;
using OrthoParents = StaticArray<Parent, ORTHO_COUNT>;

using CompoForks = StaticArray<ShortIndex, COMPO_COUNT>;
using CompoForks = StaticArray<ShortIndex, COMPO_COUNT>;
using AllForks = AllForksT<COMPO_COUNT, ORTHO_COUNT, ORTHO_UNITS>;

bool isActive (const StateID stateId) const;
bool isResumable(const StateID stateId) const;
bool isActive (const StateID stateId) const;
bool isResumable (const StateID stateId) const;

bool isPendingChange(const StateID stateId) const;
bool isPendingEnter (const StateID stateId) const;
bool isPendingExit (const StateID stateId) const;

HFSM_INLINE const Parent& forkParent(const ForkID forkId) const;
HFSM_INLINE ForkID parentCompoForkId(const ForkID forkId) const;
HFSM_INLINE const Parent& forkParent(const ForkID forkId) const;
HFSM_INLINE ForkID parentCompoForkId(const ForkID forkId) const;

HFSM_INLINE ShortIndex activeCompoProng(const ForkID forkId) const;
HFSM_INLINE ShortIndex resumableCompoProng(const ForkID forkId) const;
Expand All @@ -184,7 +194,7 @@ struct StateRegistryT<ArgsT<TContext,
HFSM_INLINE ShortIndex& requestedCompoFork(const ForkID forkId);
HFSM_INLINE OrthoFork& requestedOrthoFork(const ForkID forkId);

void requestImmediate(const Request request);
bool requestImmediate(const Request request);
void requestScheduled(const StateID stateId);

void clearOrthoRequested();
Expand Down Expand Up @@ -234,14 +244,14 @@ struct StateRegistryT<ArgsT<TContext,

using AllForks = AllForksT<COMPO_COUNT, 0, 0>;

bool isActive (const StateID stateId) const;
bool isResumable(const StateID stateId) const;
bool isActive (const StateID stateId) const;
bool isResumable (const StateID stateId) const;

bool isPendingChange(const StateID stateId) const;
bool isPendingEnter (const StateID stateId) const;
bool isPendingExit (const StateID stateId) const;

HFSM_INLINE const Parent& forkParent(const ForkID forkId) const;
HFSM_INLINE const Parent& forkParent(const ForkID forkId) const;

HFSM_INLINE ShortIndex activeCompoProng(const ForkID forkId) const;
HFSM_INLINE ShortIndex resumableCompoProng(const ForkID forkId) const;
Expand All @@ -250,7 +260,7 @@ struct StateRegistryT<ArgsT<TContext,
HFSM_INLINE ShortIndex& resumableCompoFork(const ForkID forkId);
HFSM_INLINE ShortIndex& requestedCompoFork(const ForkID forkId);

void requestImmediate(const Request request);
bool requestImmediate(const Request request);
void requestScheduled(const StateID stateId);

HFSM_INLINE void clearOrthoRequested() {}
Expand Down Expand Up @@ -290,26 +300,26 @@ struct StateRegistryT<ArgsT<TContext,

using Request = RequestT<PayloadList>;

static constexpr LongIndex STATE_COUNT = StateList::SIZE;
static constexpr ShortIndex ORTHO_COUNT = NOrthoCount;
static constexpr ShortIndex ORTHO_UNITS = NOrthoUnits;
static constexpr LongIndex STATE_COUNT = StateList::SIZE;
static constexpr ShortIndex ORTHO_COUNT = NOrthoCount;
static constexpr ShortIndex ORTHO_UNITS = NOrthoUnits;

using StateParents = StaticArray<Parent, STATE_COUNT>;
using OrthoParents = StaticArray<Parent, ORTHO_COUNT>;

using AllForks = AllForksT<0, ORTHO_COUNT, ORTHO_UNITS>;

HFSM_INLINE bool isActive (const StateID) const { return true; }
HFSM_INLINE bool isResumable(const StateID) const { return false; }
HFSM_INLINE bool isActive (const StateID) const { return true; }
HFSM_INLINE bool isResumable (const StateID) const { return false; }

HFSM_INLINE bool isPendingChange(const StateID) const { return false; }
HFSM_INLINE bool isPendingEnter (const StateID) const { return false; }
HFSM_INLINE bool isPendingExit (const StateID) const { return false; }
HFSM_INLINE bool isPendingChange(const StateID) const { return false; }
HFSM_INLINE bool isPendingEnter (const StateID) const { return false; }
HFSM_INLINE bool isPendingExit (const StateID) const { return false; }

HFSM_INLINE void requestImmediate(const Request) { HFSM_BREAK(); }
HFSM_INLINE void requestScheduled(const Request) { HFSM_BREAK(); }
HFSM_INLINE bool requestImmediate(const Request) { HFSM_BREAK(); return true; }
HFSM_INLINE void requestScheduled(const Request) { HFSM_BREAK(); }

HFSM_INLINE void clearOrthoRequested() {}
HFSM_INLINE void clearOrthoRequested() {}

StateParents stateParents;
OrthoParents orthoParents;
Expand Down
20 changes: 15 additions & 5 deletions include/hfsm2/detail/state_registry.inl
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, NOC, NOU, TPL, NTC>>::requestedOrtho
//------------------------------------------------------------------------------

template <typename TC, typename TG, typename TSL, typename TRL, LongIndex NCC, LongIndex NOC, LongIndex NOU, typename TPL, LongIndex NTC>
void
bool
StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, NOC, NOU, TPL, NTC>>::requestImmediate(const Request request) {
if (HFSM_CHECKED(request.stateId < STATE_COUNT)) {
if (request.stateId == 0)
return false;
else if (HFSM_CHECKED(request.stateId < STATE_COUNT)) {
Parent parent;

for (parent = stateParents[request.stateId];
Expand Down Expand Up @@ -266,6 +268,8 @@ StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, NOC, NOU, TPL, NTC>>::requestImmedia
requestedOrthoFork(parent.forkId)[parent.prong] = true;
}
}

return true;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -469,10 +473,14 @@ StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, 0, 0, TPL, NTC>>::requestedCompoFork
//------------------------------------------------------------------------------

template <typename TC, typename TG, typename TSL, typename TRL, LongIndex NCC, typename TPL, LongIndex NTC>
void
bool
StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, 0, 0, TPL, NTC>>::requestImmediate(const Request request) {
if (HFSM_CHECKED(request.stateId < STATE_COUNT)) {
if (Parent parent = stateParents[request.stateId]) {
if (request.stateId == 0)
return false;
else if (HFSM_CHECKED(request.stateId < STATE_COUNT)) {
Parent parent = stateParents[request.stateId];

if (HFSM_CHECKED(parent)) {
HFSM_ASSERT(parent.forkId > 0);

requestedCompoFork(parent.forkId) = parent.prong;
Expand All @@ -490,6 +498,8 @@ StateRegistryT<ArgsT<TC, TG, TSL, TRL, NCC, 0, 0, TPL, NTC>>::requestImmediate(c
}
}
}

return true;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
Loading

0 comments on commit 858d55e

Please sign in to comment.