Skip to content

Commit

Permalink
+ added TContext argument to all LoggerInterface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-gresyk committed Aug 16, 2019
1 parent 5a91328 commit f30ac97
Show file tree
Hide file tree
Showing 40 changed files with 407 additions and 499 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ cmake-*
/binaries-*
/hfsm_test.dir
/projects/code-lite/compile_commands.json
/projects/code-lite/test-gcc-tdm/CMakeLists.txt
/projects/code-lite/test-gcc-tdm/compile_flags.txt
/projects/code-lite/*/CMakeLists.txt
/projects/code-lite/*/compile_flags.txt
5 changes: 3 additions & 2 deletions examples/basic_audio_player/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static_assert(FSM::stateId<Paused>() == 3, "");

// custom logger for recording all transitions
struct Logger
: hfsm2::LoggerInterface
: hfsm2::LoggerInterfaceT<Context>
{
static const char* stateName(const StateID stateId) {
switch (stateId) {
Expand All @@ -59,7 +59,8 @@ struct Logger
}
}

void recordTransition(const StateID origin,
void recordTransition(Context& /*context*/,
const StateID origin,
const Transition /*transition*/,
const StateID target) override
{
Expand Down
1 change: 1 addition & 0 deletions examples/calculator/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// added by Kjeld Mathias Petersen
// in https://github.com/DonMathi/HFSM2/commit/d258e9c0790cde92e04d98402e6b2636cbaf57fe
// based on https://www.embedded.com/print/4026976

#include <hfsm2/machine.hpp>
#include <iostream>
Expand Down
6 changes: 4 additions & 2 deletions examples/debug_logger_interface/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ static_assert(FSM::stateId<To>() == 2, "");
struct Logger
: hfsm2::LoggerInterface
{
void recordMethod(const hfsm2::StateID /*origin*/,
void recordMethod(Context& /*context*/,
const hfsm2::StateID /*origin*/,
const Method method) override
{
std::cout //<< hfsm2::stateName(origin) << "::"
<< hfsm2::methodName(method) << "()\n";
}

void recordTransition(const hfsm2::StateID /*origin*/,
void recordTransition(Context& /*context*/,
const hfsm2::StateID /*origin*/,
const Transition transition,
const hfsm2::StateID /*target*/) override
{
Expand Down
4 changes: 2 additions & 2 deletions examples/snippets/wiki_plans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

TEST_CASE("Plans.Traffic Light", "[Wiki]") {
TEST_CASE("Wiki.Plans.Traffic Light", "[Wiki]") {
using M = hfsm2::Machine; // stateID

using FSM = M::Root<struct Apex, // 0
Expand Down Expand Up @@ -70,7 +70,7 @@ TEST_CASE("Plans.Traffic Light", "[Wiki]") {

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

TEST_CASE("Plans.Detailed Demo", "[Wiki]") {
TEST_CASE("Wiki.Plans.Detailed Demo", "[Wiki]") {
using M = hfsm2::Machine; // stateID

using FSM = M::Root<struct Apex, // 0
Expand Down
16 changes: 8 additions & 8 deletions examples/snippets/wiki_transitions-within-hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

TEST_CASE("Transitions within Hierarchy.Transitions into States", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.Transitions into States", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand All @@ -27,7 +27,7 @@ TEST_CASE("Transitions within Hierarchy.Transitions into States", "[Wiki]") {

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

TEST_CASE("Transitions within Hierarchy.Transitions into Regions", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.Transitions into Regions", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand All @@ -54,7 +54,7 @@ TEST_CASE("Transitions within Hierarchy.Transitions into Regions", "[Wiki]") {

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

TEST_CASE("Transitions within Hierarchy.External Transition Interface", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.External Transition Interface", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand All @@ -75,7 +75,7 @@ TEST_CASE("Transitions within Hierarchy.External Transition Interface", "[Wiki]"

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

TEST_CASE("Transitions within Hierarchy.Internal Transition Interface", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.Internal Transition Interface", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand All @@ -102,7 +102,7 @@ TEST_CASE("Transitions within Hierarchy.Internal Transition Interface", "[Wiki]"

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

TEST_CASE("Transitions within Hierarchy.'Restart' Transition", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.'Restart' Transition", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand All @@ -128,7 +128,7 @@ TEST_CASE("Transitions within Hierarchy.'Restart' Transition", "[Wiki]") {

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

TEST_CASE("Transitions within Hierarchy.'Resume' Transition", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.'Resume' Transition", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand Down Expand Up @@ -166,7 +166,7 @@ TEST_CASE("Transitions within Hierarchy.'Resume' Transition", "[Wiki]") {

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

TEST_CASE("Transitions within Hierarchy.'Utilize' Transition", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.'Utilize' Transition", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand Down Expand Up @@ -202,7 +202,7 @@ TEST_CASE("Transitions within Hierarchy.'Utilize' Transition", "[Wiki]") {

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

TEST_CASE("Transitions within Hierarchy.'Randomize' Transition", "[Wiki]") {
TEST_CASE("Wiki.Transitions within Hierarchy.'Randomize' Transition", "[Wiki]") {
using M = hfsm2::Machine;

using FSM = M::PeerRoot<
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/wiki_tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct Done

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

TEST_CASE("Tutorial", "[Wiki]") {
TEST_CASE("Wiki.Tutorial", "[Wiki]") {
Context context;
context.powerOn = true;

Expand Down
11 changes: 7 additions & 4 deletions examples/snippets/wiki_utility-theory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ using Events = std::vector<Event>;
struct Logger
: hfsm2::LoggerInterface // requires HFSM_ENABLE_LOG_INTERFACE defined
{
void recordMethod(const StateID state,
void recordMethod(Context& /*context*/,
const StateID state,
const Method method) override
{
if (method == Method::RANK)
Expand All @@ -48,14 +49,16 @@ struct Logger
history.emplace_back(state, Event::UTILITY);
}

void recordUtilityResolution(const StateID head,
void recordUtilityResolution(Context& /*context*/,
const StateID head,
const StateID prong,
const Utilty /*utilty*/) override
{
history.emplace_back(head, Event::UTILITY_RESOLUTION, prong);
}

void recordRandomResolution(const StateID head,
void recordRandomResolution(Context& /*context*/,
const StateID head,
const StateID prong,
const Utilty /*utilty*/) override
{
Expand Down Expand Up @@ -167,7 +170,7 @@ struct O_1 : FSM::State {};

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

TEST_CASE("Utility Theory", "[Wiki]") {
TEST_CASE("Wiki.Utility Theory", "[Wiki]") {
Logger logger;
FSM::Instance fsm{&logger};
REQUIRE(fsm.isActive<Origin>()); // Initial activation
Expand Down
34 changes: 17 additions & 17 deletions include/hfsm2/detail/control.inl
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ FullControlT<TA>::buildPlanStatus() {
case Status::SUCCESS:
_planData.tasksSuccesses.template set<STATE_ID>();

HFSM_LOG_PLAN_STATUS(_regionId, StatusEvent::SUCCEEDED);
HFSM_LOG_PLAN_STATUS(context(), _regionId, StatusEvent::SUCCEEDED);
break;

case Status::FAILURE:
_planData.tasksFailures.template set<STATE_ID>();

HFSM_LOG_PLAN_STATUS(_regionId, StatusEvent::FAILED);
HFSM_LOG_PLAN_STATUS(context(), _regionId, StatusEvent::FAILED);
break;

default:
Expand All @@ -245,7 +245,7 @@ FullControlT<TA>::changeTo(const StateID stateId) {
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::CHANGE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::CHANGE, stateId);
}
}

Expand All @@ -263,7 +263,7 @@ FullControlT<TA>::changeTo(const StateID stateId,
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::CHANGE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::CHANGE, stateId);
}
}

Expand All @@ -279,7 +279,7 @@ FullControlT<TA>::restart(const StateID stateId) {
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RESTART, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RESTART, stateId);
}
}

Expand All @@ -297,7 +297,7 @@ FullControlT<TA>::restart(const StateID stateId,
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RESTART, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RESTART, stateId);
}
}

Expand All @@ -313,7 +313,7 @@ FullControlT<TA>::resume(const StateID stateId) {
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RESUME, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RESUME, stateId);
}
}

Expand All @@ -331,7 +331,7 @@ FullControlT<TA>::resume(const StateID stateId,
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RESUME, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RESUME, stateId);
}
}

Expand All @@ -347,7 +347,7 @@ FullControlT<TA>::utilize(const StateID stateId) {
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::UTILIZE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::UTILIZE, stateId);
}
}

Expand All @@ -365,7 +365,7 @@ FullControlT<TA>::utilize(const StateID stateId,
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::UTILIZE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::UTILIZE, stateId);
}
}

Expand All @@ -381,7 +381,7 @@ FullControlT<TA>::randomize(const StateID stateId) {
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RANDOMIZE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RANDOMIZE, stateId);
}
}

Expand All @@ -399,7 +399,7 @@ FullControlT<TA>::randomize(const StateID stateId,
if (_regionIndex + _regionSize <= stateId || stateId < _regionIndex)
_status.outerTransition = true;

HFSM_LOG_TRANSITION(_originId, Transition::RANDOMIZE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::RANDOMIZE, stateId);
}
}

Expand All @@ -411,7 +411,7 @@ FullControlT<TA>::schedule(const StateID stateId) {
const Request transition{Request::Type::SCHEDULE, stateId};
_requests << transition;

HFSM_LOG_TRANSITION(_originId, Transition::SCHEDULE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::SCHEDULE, stateId);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -424,7 +424,7 @@ FullControlT<TA>::schedule(const StateID stateId,
const Request transition{Request::Type::SCHEDULE, stateId, payload};
_requests << transition;

HFSM_LOG_TRANSITION(_originId, Transition::SCHEDULE, stateId);
HFSM_LOG_TRANSITION(context(), _originId, Transition::SCHEDULE, stateId);
}

//------------------------------------------------------------------------------
Expand All @@ -443,7 +443,7 @@ FullControlT<TA>::succeed() {
_planData.tasksSuccesses.set(_regionIndex);
}

HFSM_LOG_TASK_STATUS(_regionId, _originId, StatusEvent::SUCCEEDED);
HFSM_LOG_TASK_STATUS(context(), _regionId, _originId, StatusEvent::SUCCEEDED);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -462,7 +462,7 @@ FullControlT<TA>::fail() {
_planData.tasksFailures.set(_regionIndex);
}

HFSM_LOG_TASK_STATUS(_regionId, _originId, StatusEvent::FAILED);
HFSM_LOG_TASK_STATUS(context(), _regionId, _originId, StatusEvent::FAILED);
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -472,7 +472,7 @@ void
GuardControlT<TA>::cancelPendingTransitions() {
_cancelled = true;

HFSM_LOG_CANCELLED_PENDING(_originId);
HFSM_LOG_CANCELLED_PENDING(context(), _originId);
}

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

0 comments on commit f30ac97

Please sign in to comment.