diff --git a/.vscode/launch.json b/.vscode/launch.json index d5ef522..9645b72 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,8 +7,8 @@ { "type": "lldb", "request": "launch", - "name": "lldb mdoor", - "program": "${workspaceFolder}/builddir/example/mdoor/mdoor", + "name": "Wizard world", + "program": "${workspaceFolder}/builddir/example/wizard/wizard", "args": [], "cwd": "${workspaceFolder}" }, diff --git a/example/wizard/wizard.cpp b/example/wizard/wizard.cpp index 9ba8602..d9890f7 100644 --- a/example/wizard/wizard.cpp +++ b/example/wizard/wizard.cpp @@ -3,27 +3,27 @@ #include -WizardsWorld::WizardsWorld() : - Hsm("WizardsWorld", &world), - world("World", nullptr, [&] (auto event){startStd(event);}), - house("house", &world, [&] (auto event){houseAction(event);}), - hall("hall", &house, [&] (auto event){hallAction(event);}), - kitchen("kitchen", &house, [&] (auto event){kitchenAction(event);}), - garden("garden", &world, [&] (auto event){gardenAction(event);}), - frontGarden("frontGarden", &garden, [&] (auto event){frontGardenAction(event);}), - well("well", &garden, [&] (auto event){wellAction(event);}) -{ - transitions = std::vector{ - Transition{&frontGarden, &NORTH, &hall}, - Transition{&frontGarden, &WEST, &well}, - Transition{&hall, &NORTH, &kitchen}, - Transition{&hall, &SOUTH, &frontGarden}, - Transition{&kitchen, &SOUTH, &hall}, - Transition{&kitchen, &SMELL, &kitchen, [&]() { getHungry(); }}, - Transition{&kitchen, &EAT, &kitchen, [&]() { eat(); }}, - Transition{&well, &EAST, &frontGarden} +WizardsWorld::WizardsWorld() : Hsm("WizardsWorld") +{ + auto house = std::make_shared("house", nullptr, [&] (auto event){houseAction(event);}); + auto hall = std::make_shared("hall", house, [&] (auto event){hallAction(event);}); + auto kitchen = std::make_shared("kitchen", house, [&] (auto event){kitchenAction(event);}); + auto garden = std::make_shared("garden", nullptr, [&] (auto event){gardenAction(event);}); + auto frontGarden = std::make_shared("frontGarden", garden, [&] (auto event){frontGardenAction(event);}); + auto well = std::make_shared("well", garden, [&] (auto event){wellAction(event);}); + + auto states = {frontGarden, house, hall, kitchen, garden,well}; + auto transitions = { + Transition{frontGarden, &NORTH, hall}, + Transition{frontGarden, &WEST, well}, + Transition{hall, &NORTH, kitchen}, + Transition{hall, &SOUTH, frontGarden}, + Transition{kitchen, &SOUTH, hall}, + Transition{kitchen, &SMELL, kitchen, [&]() { getHungry(); }}, + Transition{kitchen, &EAT, kitchen, [&]() { eat(); }}, + Transition{well, &EAST, frontGarden} }; - enable(); + activate(states, transitions); } void WizardsWorld::north() @@ -61,19 +61,6 @@ void WizardsWorld::noAction(StdEvents event) } -void WizardsWorld::startStd(StdEvents event) -{ - switch (event) - { - case StdEvents::START: - transitionTo(&frontGarden); - break; - case StdEvents::ENTRY: - case StdEvents::EXIT: - break; - } -} - void WizardsWorld::houseAction(StdEvents event) { if(StdEvents::ENTRY == event) { diff --git a/example/wizard/wizard.h b/example/wizard/wizard.h index c5a2346..d1fdf11 100644 --- a/example/wizard/wizard.h +++ b/example/wizard/wizard.h @@ -12,7 +12,6 @@ class WizardsWorld : public Hsm { void take(); private: - State world, house, hall, kitchen, garden, frontGarden, well; Event NORTH, EAST, SOUTH, WEST; Event SMELL, EAT;