Skip to content

Commit

Permalink
doc: added more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jfayot committed Aug 27, 2024
1 parent 1f7ef3b commit e7f2228
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 85 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Considering the following minimal state machine, it can be coded as follows:

### Flat-style StateMachine setup

[minimal_flat.cpp](./examples/minimal_flat.cpp)

```c++
#include "dsm/dsm.hpp"
#include <cassert>
Expand Down Expand Up @@ -135,6 +137,8 @@ int main()
### Enclosed-style StateMachine setup
[minimal_enclosed.cpp](./examples/minimal_enclosed.cpp)
```c++
#include "dsm/dsm.hpp"
#include <cassert>
Expand Down Expand Up @@ -174,16 +178,22 @@ int main()

![entry_exit_actions](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/jfayot/dynamic-state-machine/master/resources/entry_exit_actions.puml)

[entry_exit_actions.cpp](./examples/entry_exit_actions/entry_exit_actions.cpp)
[entry_exit_actions.cpp](./examples/entry_exit_actions.cpp)

## Transition action

![transition_action](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/jfayot/dynamic-state-machine/master/resources/transition_action.puml)

[transition_action.cpp](./examples/transition_action/transition_action.cpp)
[transition_action.cpp](./examples/transition_action.cpp)

## Transition guard

![transition_guard](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/jfayot/dynamic-state-machine/master/resources/transition_guard.puml)

[transition_guard.cpp](./examples/transition_guard.cpp)

## Self transition

![self_transition](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/jfayot/dynamic-state-machine/master/resources/self_transition.puml)

[self_transition.cpp](./examples/self_transition/self_transition.cpp)
[self_transition.cpp](./examples/self_transition.cpp)
25 changes: 20 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
cmake_minimum_required(VERSION 3.16)

add_subdirectory(minimal_flat)
add_subdirectory(minimal_enclosed)
add_subdirectory(entry_exit_actions)
add_subdirectory(transition_action)
add_subdirectory(self_transition)
add_executable(minimal_flat minimal_flat.cpp)
add_executable(minimal_enclosed minimal_enclosed.cpp)
add_executable(entry_exit_actions entry_exit_actions.cpp)
add_executable(transition_action transition_action.cpp)
add_executable(transition_guard transition_guard.cpp)
add_executable(self_transition self_transition.cpp)

target_link_libraries(minimal_flat PRIVATE dsm::dsm)
target_link_libraries(minimal_enclosed PRIVATE dsm::dsm)
target_link_libraries(entry_exit_actions PRIVATE dsm::dsm)
target_link_libraries(transition_action PRIVATE dsm::dsm)
target_link_libraries(transition_guard PRIVATE dsm::dsm)
target_link_libraries(self_transition PRIVATE dsm::dsm)

set_target_properties(minimal_flat PROPERTIES FOLDER examples)
set_target_properties(minimal_enclosed PROPERTIES FOLDER examples)
set_target_properties(entry_exit_actions PROPERTIES FOLDER examples)
set_target_properties(transition_action PROPERTIES FOLDER examples)
set_target_properties(transition_guard PROPERTIES FOLDER examples)
set_target_properties(self_transition PROPERTIES FOLDER examples)
File renamed without changes.
15 changes: 0 additions & 15 deletions examples/entry_exit_actions/CMakeLists.txt

This file was deleted.

File renamed without changes.
15 changes: 0 additions & 15 deletions examples/minimal_enclosed/CMakeLists.txt

This file was deleted.

File renamed without changes.
15 changes: 0 additions & 15 deletions examples/minimal_flat/CMakeLists.txt

This file was deleted.

File renamed without changes.
15 changes: 0 additions & 15 deletions examples/self_transition/CMakeLists.txt

This file was deleted.

File renamed without changes.
15 changes: 0 additions & 15 deletions examples/transition_action/CMakeLists.txt

This file was deleted.

32 changes: 32 additions & 0 deletions examples/transition_guard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "dsm/dsm.hpp"
#include <cassert>
#include <iostream>

using namespace dsm;

struct e1 : Event<e1>{
bool guard_flag;
e1(bool guard_flag) : guard_flag{ guard_flag } {}
};

struct sm : StateMachine<sm>{};
struct s0 : State<s0, sm>{
void onEvent1(const e1& evt) { std::cout << "Received event " << evt.name() << std::endl; }
bool guard(const e1& evt) { return evt.guard_flag; }
};
struct s1 : State<s1, sm>{};

int main()
{
sm sm;

sm.addState<s0, Entry>();
sm.addState<s1>();
sm.addTransition<s0, e1, s0, &s0::onEvent1, &s0::guard, s1>();

sm.start();
sm.processEvent(e1{false});
sm.processEvent(e1{true});

return 0;
}
2 changes: 1 addition & 1 deletion resources/self_transition.puml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@startuml
hide empty description
[*] -right-> s0
s0 -> s0 : e1
s0 -> s0 : e1/onEvent1
s0: onEvent1()
@enduml
2 changes: 1 addition & 1 deletion resources/transition_action.puml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@startuml
hide empty description
[*] -right-> s0
s0 -right-> s1 : e1
s0 -right-> s1 : e1/onEvent1
s0: onEvent1()
@enduml
6 changes: 6 additions & 0 deletions resources/transition_guard.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@startuml
hide empty description
[*] -right-> s0
s0 -right-> s1 : e1[guard]/onEvent1
s0: onEvent1()
@enduml

0 comments on commit e7f2228

Please sign in to comment.