Skip to content

Commit d4a02ec

Browse files
committed
Add NodeIndexType
1 parent ea38ad5 commit d4a02ec

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

Code/max/Containers/StateMachine/Node.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ namespace max {
1616
namespace Containers {
1717
namespace StateMachine {
1818

19-
template<typename... TransitionTypes>
19+
template<typename NodeIndexType, typename... TransitionTypes>
2020
class Node {
2121
public:
2222

23-
constexpr explicit Node(std::tuple<TransitionTypes...> outbound_transitions) noexcept
24-
: outbound_transitions_(std::move(outbound_transitions))
23+
constexpr explicit Node(NodeIndexType this_node, std::tuple<TransitionTypes...> outbound_transitions) noexcept
24+
: this_node_(std::move(this_node))
25+
, outbound_transitions_(std::move(outbound_transitions))
2526
{}
2627

27-
template<typename T>
28-
constexpr std::optional<size_t> AttemptTransition(const T& input) noexcept {
28+
template<typename NodeIndexType, typename T>
29+
constexpr std::optional<NodeIndexType> AttemptTransition(const T& input) noexcept {
2930
auto transition_happened = false;
30-
auto new_node_index = std::optional<size_t>{std::nullopt};
31+
auto new_node_index = std::optional<NodeIndexType>{std::nullopt};
3132

3233
auto attempt_transition = [&transition_happened, &new_node_index, &input](auto&& arg) {
3334
// TODO: I added .value_ to RangeMatcher because I couldn't get this line to work
3435
//if constexpr (std::is_same_v<T, decltype(arg.matcher_)::parameter_type>) {
3536
if constexpr (std::is_same_v<T, decltype(arg.matcher_.value_)>) {
3637
if (!transition_happened) {
37-
auto possible_new_node_index = arg.AttemptTransition(input);
38+
auto possible_new_node_index = arg.AttemptTransition<NodeIndexType>(input);
3839
if (possible_new_node_index) {
3940
transition_happened = true;
4041
new_node_index = std::move(possible_new_node_index);
@@ -50,13 +51,14 @@ namespace StateMachine {
5051
return new_node_index;
5152
}
5253

54+
NodeIndexType this_node_;
5355
std::tuple<TransitionTypes...> outbound_transitions_;
5456

5557
};
5658

57-
template<typename... TransitionTypes>
58-
constexpr Node<TransitionTypes...> MakeNode(TransitionTypes... outbound_transitions) noexcept {
59-
return Node{std::make_tuple(std::move(outbound_transitions) ...)};
59+
template<typename NodeIndexType, typename... TransitionTypes>
60+
constexpr Node<NodeIndexType, TransitionTypes...> MakeNode(NodeIndexType this_node, TransitionTypes... outbound_transitions) noexcept {
61+
return Node{std::move(this_node), std::make_tuple(std::move(outbound_transitions) ...)};
6062
}
6163

6264
} // namespace StateMachine

Code/max/Containers/StateMachine/StateMachine.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ namespace max {
1212
namespace Containers {
1313
namespace StateMachine {
1414

15-
template<typename... NodeTypes>
15+
template<typename NodeIndexType, typename... NodeTypes>
1616
class StateMachine {
1717
public:
1818

19-
constexpr explicit StateMachine(std::tuple<NodeTypes...> nodes) noexcept
19+
constexpr explicit StateMachine(NodeIndexType starting_node, std::tuple<NodeTypes...> nodes) noexcept
2020
: nodes_(std::move(nodes))
21-
, current_node_index_(0)
21+
, current_node_index_(std::move(starting_node))
2222
{}
2323

2424
template<typename T>
2525
constexpr void AttemptTransition(T input) noexcept {
2626
auto transition_happened = false;
2727
auto i = size_t{0};
2828
auto attempt_transition = [this, &transition_happened, &i, &input](auto&& arg) {
29-
if (!transition_happened && current_node_index_ == i++) {
30-
auto possible_new_node_index = arg.AttemptTransition(input);
29+
if (!transition_happened && current_node_index_ == arg.this_node_) {
30+
auto possible_new_node_index = arg.AttemptTransition<NodeIndexType>(input);
3131
if (possible_new_node_index) {
3232
transition_happened = true;
3333
current_node_index_ = std::move(possible_new_node_index.value());
@@ -41,7 +41,7 @@ namespace StateMachine {
4141
}
4242

4343
std::tuple<NodeTypes...> nodes_;
44-
size_t current_node_index_;
44+
NodeIndexType current_node_index_;
4545

4646
};
4747

Code/max/Containers/StateMachine/Transition.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace StateMachine {
2121
, callback_(std::move(callback))
2222
{}
2323

24-
template<typename T>
25-
constexpr std::optional<size_t> AttemptTransition(const T& input) noexcept {
24+
template<typename NodeIndexType, typename T>
25+
constexpr std::optional<NodeIndexType> AttemptTransition(const T& input) noexcept {
2626
if (matcher_.DoesMatch(input)) {
2727
return callback_(std::move(input));
2828
}

0 commit comments

Comments
 (0)