diff --git a/Code/max/Containers/StateMachine/Node.hpp b/Code/max/Containers/StateMachine/Node.hpp index 12bc53d..61b735e 100644 --- a/Code/max/Containers/StateMachine/Node.hpp +++ b/Code/max/Containers/StateMachine/Node.hpp @@ -16,25 +16,26 @@ namespace max { namespace Containers { namespace StateMachine { - template + template class Node { public: - constexpr explicit Node(std::tuple outbound_transitions) noexcept - : outbound_transitions_(std::move(outbound_transitions)) + constexpr explicit Node(NodeIndexType this_node, std::tuple outbound_transitions) noexcept + : this_node_(std::move(this_node)) + , outbound_transitions_(std::move(outbound_transitions)) {} template - constexpr std::optional AttemptTransition(const T& input) noexcept { + constexpr std::optional AttemptTransition(const T& input) noexcept { auto transition_happened = false; - auto new_node_index = std::optional{std::nullopt}; + auto new_node_index = std::optional{std::nullopt}; auto attempt_transition = [&transition_happened, &new_node_index, &input](auto&& arg) { // TODO: I added .value_ to RangeMatcher because I couldn't get this line to work //if constexpr (std::is_same_v) { if constexpr (std::is_same_v) { if (!transition_happened) { - auto possible_new_node_index = arg.AttemptTransition(input); + auto possible_new_node_index = arg.template AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; new_node_index = std::move(possible_new_node_index); @@ -50,13 +51,14 @@ namespace StateMachine { return new_node_index; } + NodeIndexType this_node_; std::tuple outbound_transitions_; }; - template - constexpr Node MakeNode(TransitionTypes... outbound_transitions) noexcept { - return Node{std::make_tuple(std::move(outbound_transitions) ...)}; + template + constexpr Node MakeNode(NodeIndexType this_node, TransitionTypes... outbound_transitions) noexcept { + return Node{std::move(this_node), std::make_tuple(std::move(outbound_transitions) ...)}; } } // namespace StateMachine diff --git a/Code/max/Containers/StateMachine/NodeTest.cpp b/Code/max/Containers/StateMachine/NodeTest.cpp index a12e2fd..330c621 100644 --- a/Code/max/Containers/StateMachine/NodeTest.cpp +++ b/Code/max/Containers/StateMachine/NodeTest.cpp @@ -32,7 +32,9 @@ namespace StateMachine { return size_t{2}; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -40,9 +42,11 @@ namespace StateMachine { return size_t{3}; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(size_t{0}, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(uint32_t{1}); @@ -59,7 +63,9 @@ namespace StateMachine { return 2; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -67,9 +73,11 @@ namespace StateMachine { return 3; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(0, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(uint32_t{2}); @@ -86,7 +94,9 @@ namespace StateMachine { return 2; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -94,13 +104,15 @@ namespace StateMachine { return 3; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(0, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(std::string_view{"test"}); - CurrentTest.MAX_TESTING_ASSERT( new_node_index == size_t{3} ); + CurrentTest.MAX_TESTING_ASSERT( new_node_index == 3 ); CurrentTest.MAX_TESTING_ASSERT( !range_callback_called ); CurrentTest.MAX_TESTING_ASSERT( string_callback_called ); } diff --git a/Code/max/Containers/StateMachine/StateMachine.hpp b/Code/max/Containers/StateMachine/StateMachine.hpp index f698a22..50b382a 100644 --- a/Code/max/Containers/StateMachine/StateMachine.hpp +++ b/Code/max/Containers/StateMachine/StateMachine.hpp @@ -12,21 +12,20 @@ namespace max { namespace Containers { namespace StateMachine { - template + template class StateMachine { public: - constexpr explicit StateMachine(std::tuple nodes) noexcept - : nodes_(std::move(nodes)) - , current_node_index_(0) + constexpr explicit StateMachine(NodeIndexType starting_node, std::tuple nodes) noexcept + : current_node_index_(std::move(starting_node)) + , nodes_(std::move(nodes)) {} template constexpr void AttemptTransition(T input) noexcept { auto transition_happened = false; - auto i = size_t{0}; - auto attempt_transition = [this, &transition_happened, &i, &input](auto&& arg) { - if (!transition_happened && current_node_index_ == i++) { + auto attempt_transition = [this, &transition_happened, &input](auto&& arg) { + if (!transition_happened && current_node_index_ == arg.this_node_) { auto possible_new_node_index = arg.AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; @@ -40,8 +39,8 @@ namespace StateMachine { }, nodes_); } + NodeIndexType current_node_index_; std::tuple nodes_; - size_t current_node_index_; }; diff --git a/Code/max/Containers/StateMachine/StateMachineTest.cpp b/Code/max/Containers/StateMachine/StateMachineTest.cpp index 9df8178..c259401 100644 --- a/Code/max/Containers/StateMachine/StateMachineTest.cpp +++ b/Code/max/Containers/StateMachine/StateMachineTest.cpp @@ -32,18 +32,22 @@ namespace StateMachine { range_callback_called = true; return size_t{1}; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; - auto node_0 = max::Containers::StateMachine::MakeNode(std::move(range_transition)); + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; + auto node_0 = max::Containers::StateMachine::MakeNode(size_t{0}, std::move(range_transition)); bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { string_callback_called = true; return size_t{0}; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; - auto node_1 = max::Containers::StateMachine::MakeNode(std::move(string_transition)); + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; + auto node_1 = max::Containers::StateMachine::MakeNode(size_t{1}, std::move(string_transition)); - auto state_machine = max::Containers::StateMachine::StateMachine{std::make_tuple(std::move(node_0), std::move(node_1))}; + auto state_machine = max::Containers::StateMachine::StateMachine{size_t{0}, std::make_tuple(std::move(node_0), std::move(node_1))}; state_machine.AttemptTransition(uint32_t{1}); diff --git a/Code/max/Containers/StateMachine/Transition.hpp b/Code/max/Containers/StateMachine/Transition.hpp index 5c88251..3c3af28 100644 --- a/Code/max/Containers/StateMachine/Transition.hpp +++ b/Code/max/Containers/StateMachine/Transition.hpp @@ -21,8 +21,8 @@ namespace StateMachine { , callback_(std::move(callback)) {} - template - constexpr std::optional AttemptTransition(const T& input) noexcept { + template + constexpr std::optional AttemptTransition(const T& input) noexcept { if (matcher_.DoesMatch(input)) { return callback_(std::move(input)); } diff --git a/Code/max/Containers/StateMachine/TransitionTest.cpp b/Code/max/Containers/StateMachine/TransitionTest.cpp index f9b1aae..ff16703 100644 --- a/Code/max/Containers/StateMachine/TransitionTest.cpp +++ b/Code/max/Containers/StateMachine/TransitionTest.cpp @@ -30,9 +30,11 @@ namespace StateMachine { return size_t{2}; }; - auto transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback)}; + auto transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback) + }; - auto new_node_index = transition.AttemptTransition(uint32_t{2}); + auto new_node_index = transition.AttemptTransition(uint32_t{2}); // TODO: This should be constexpr, right?? //static_assert( !callback_called, "" ); @@ -48,9 +50,11 @@ namespace StateMachine { return size_t{2}; }; - auto transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback)}; + auto transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback) + }; - auto new_node_index = transition.AttemptTransition(uint32_t{1}); + auto new_node_index = transition.AttemptTransition(uint32_t{1}); CurrentTest.MAX_TESTING_ASSERT( callback_called ); CurrentTest.MAX_TESTING_ASSERT( new_node_index == size_t{2} );