-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexport_dot_context.cpp
More file actions
42 lines (37 loc) · 1.31 KB
/
export_dot_context.cpp
File metadata and controls
42 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "CXXStateTree/StateTree.h"
#include <iostream>
#include <fstream>
using namespace CXXStateTree;
// A custom guard that checks if the context contains an authorized user
class UserAuthorizedGuard : public IGuard
{
public:
bool evaluate(const std::any &context) const override
{
if (context.type() == typeid(std::string))
{
std::string user = std::any_cast<std::string>(context);
return user == "admin";
}
return false;
}
};
int main()
{
UserAuthorizedGuard auth_guard;
auto tree = StateTree::Builder()
.initial("Idle")
.state("Idle", [&](State &s)
{ s.on("login", "Dashboard", &auth_guard, [](const std::any &ctx)
{ std::cout << "Login accepted for user: " << std::any_cast<std::string>(ctx) << "\n"; }); })
.state("Dashboard", [&](State &s)
{ s.on("logout", "Idle", nullptr, [](const std::any &ctx)
{ std::cout << "User logged out.\n"; }); })
.build();
// Export to DOT format and print
std::string dot = tree.export_dot();
std::cout << dot;
// Optionally write to file
std::ofstream("example_state_tree.dot") << dot;
return 0;
}