Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Code/max/Containers/StateMachine/AnythingMatcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_CONTAINERS_STATEMACHINE_ANYTHINGMATCHER_HPP
#define MAX_CONTAINERS_STATEMACHINE_ANYTHINGMATCHER_HPP

namespace max {
namespace Containers {
namespace StateMachine {

template<typename T>
class AnythingMatcher {
public:

using parameter_type = T;

constexpr bool DoesMatch(const T& input) const noexcept;

T value_ = {}; // TODO: I don't want this. I couldn't get parameter_type to work for me.

};

} // namespace StateMachine
} // namespace Containers
} // namespace max

#include <max/Containers/StateMachine/AnythingMatcher.inl>

#endif // #ifndef MAXSTATEMACHINE_ANYTHINGMATCHER_HPP
16 changes: 16 additions & 0 deletions Code/max/Containers/StateMachine/AnythingMatcher.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace max {
namespace Containers {
namespace StateMachine {

template<typename T>
constexpr bool AnythingMatcher<T>::DoesMatch(const T& /*input*/) const noexcept {
return true;
}

} // namespace StateMachine
} // namespace Containers
} // namespace max
32 changes: 32 additions & 0 deletions Code/max/Containers/StateMachine/AnythingMatcherTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <max/Containers/StateMachine/AnythingMatcherTest.hpp>

#include <max/Containers/StateMachine/NumberInput.hpp>
#include <max/Containers/StateMachine/AnythingMatcher.hpp>

#include <max/Testing/CoutResultPolicy.hpp>
#include <max/Testing/TestSuite.hpp>

#include <cstdint>

namespace maxStateMachine {

void RunAnythingMatcherTestSuite() noexcept {

max::Testing::CoutResultPolicy ResultPolicy;
auto AnythingMatcherTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "maxStateMathinc::AnythingMatcher test suite", std::move( ResultPolicy ) };

AnythingMatcherTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "DoesMatch() always matches", []( max::Testing::Test< max::Testing::CoutResultPolicy > & /*CurrentTest*/, max::Testing::CoutResultPolicy const & /*ResultPolicy*/ ) {
constexpr auto anything_matcher = max::Containers::StateMachine::AnythingMatcher<uint32_t>{};

static_assert( anything_matcher.DoesMatch(uint32_t{1}), "DoesMatch() should match all values" );
}
} );

AnythingMatcherTestSuite.RunTests();
}

} // namespace maxStateMachine
18 changes: 18 additions & 0 deletions Code/max/Containers/StateMachine/AnythingMatcherTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAXAUTOMATEDTESTS_CONTAINERS_STATEMACHINE_ANYTHINGMATCHERTEST_HPP
#define MAXAUTOMATEDTESTS_CONTAINERS_STATEMACHINE_ANYTHINGMATCHERTEST_HPP

namespace maxAutomatedTests {
namespace Containers {
namespace StateMachine {

void RunAnythingMatcherTestSuite() noexcept;

} // namespace StateMachine
} // namespace Containers
} // namespace maxAutomatedTests

#endif // #ifndef MAXAUTOMATEDTESTS_CONTAINERS_STATEMACHINE_ANYTHINGMATCHERTEST_HPP
19 changes: 19 additions & 0 deletions Code/max/Containers/StateMachine/EnumMatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <max/Containers/StateMachine/EnumMatcher.hpp>

namespace max {
namespace Containers {
namespace StateMachine {

/*
constexpr bool EnumMatcher::DoesMatch(const Input& input) noexcept {
return true;
}
*/

} // namespace StateMachine
} // namespace Containers
} // namespace max
44 changes: 44 additions & 0 deletions Code/max/Containers/StateMachine/EnumMatcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_CONTAINERS_STATEMACHINE_ENUMMATCHER_HPP
#define MAX_CONTAINERS_STATEMACHINE_ENUMMATCHER_HPP

#include <string_view>
#include <vector>

namespace max {
namespace Containers {
namespace StateMachine {

template<typename T>
class EnumValue {
public:

constexpr explicit EnumValue(T value, std::string_view representation) noexcept;

T value_;
std::string_view representation_;

};

template<typename T>
class EnumMatcher {
public:

constexpr explicit EnumMatcher(std::vector<EnumValue<T>> values) noexcept;

//constexpr bool DoesMatch(const Input& input) noexcept;

std::vector<EnumValue<T>> values_;

};

} // namespace StateMachine
} // namespace Containers
} // namespace max

#include <max/Containers/StateMachine/EnumMatcher.inl>

#endif // #ifndef MAX_CONTAINERS_STATEMACHINE_ENUMMATCHER_HPP
24 changes: 24 additions & 0 deletions Code/max/Containers/StateMachine/EnumMatcher.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

namespace max {
namespace Containers {
namespace StateMachine {

template<typename T>
constexpr EnumValue<T>::EnumValue(T value, std::string_view representation) noexcept
: value_(std::move(value))
, representation_(std::move(representation))
{}

template<typename T>
constexpr EnumMatcher<T>::EnumMatcher(std::vector<EnumValue<T>> values) noexcept
: values_(std::move(values))
{}

} // namespace StateMachine
} // namespace Containers
} // namespace max
18 changes: 18 additions & 0 deletions Code/max/Containers/StateMachine/Input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_CONTAINERS_STATEMACHINE_INPUT_HPP
#define MAX_CONTAINERS_STATEMACHINE_INPUT_HPP

namespace max {
namespace Containers {
namespace StateMachine {

// TODO: Make Input concept

} // namespace StateMachine
} // namespace Containers
} // namespace max

#endif // #ifndef MAX_CONTAINERS_STATEMACHINE_INPUT_HPP
23 changes: 23 additions & 0 deletions Code/max/Containers/StateMachine/Matcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_CONTAINERS_STATEMACHINE_MATCHER_HPP
#define MAX_CONTAINERS_STATEMACHINE_MATCHER_HPP

namespace max {
namespace Containers {
namespace StateMachine {

// TODO: Make Matcher concept

template<typename MatcherType, typename InputType>
bool DoesMatch(const MatcherType& matcher_type, const InputType& input_type) noexcept {
return matcher_type.DoesMatch(input_type.value_);
}

} // namespace StateMachine
} // namespace Containers
} // namespace max

#endif // #ifndef MAX_CONTAINERS_STATEMACHINE_MATCHER_HPP
66 changes: 66 additions & 0 deletions Code/max/Containers/StateMachine/Node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2025, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_CONTAINERS_STATEMACHINE_NODE_HPP
#define MAX_CONTAINERS_STATEMACHINE_NODE_HPP

#include <optional>
#include <tuple>
#include <type_traits>
#include <utility>

#include <max/Containers/StateMachine/Transition.hpp>

namespace max {
namespace Containers {
namespace StateMachine {

template<typename... TransitionTypes>
class Node {
public:

constexpr explicit Node(std::tuple<TransitionTypes...> outbound_transitions) noexcept
: outbound_transitions_(std::move(outbound_transitions))
{}

template<typename T>
constexpr std::optional<size_t> AttemptTransition(const T& input) noexcept {
auto transition_happened = false;
auto new_node_index = std::optional<size_t>{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<T, decltype(arg.matcher_)::parameter_type>) {
if constexpr (std::is_same_v<T, decltype(arg.matcher_.value_)>) {
if (!transition_happened) {
auto possible_new_node_index = arg.AttemptTransition(input);
if (possible_new_node_index) {
transition_happened = true;
new_node_index = std::move(possible_new_node_index);
}
}
}
};

std::apply([&attempt_transition](auto&&... args) {
((attempt_transition(args)), ...);
}, outbound_transitions_);

return new_node_index;
}

std::tuple<TransitionTypes...> outbound_transitions_;

};

template<typename... TransitionTypes>
Node<TransitionTypes...> MakeNode(TransitionTypes... outbound_transitions) noexcept {
return Node{std::make_tuple(std::move(outbound_transitions) ...)};
}

} // namespace StateMachine
} // namespace Containers
} // namespace max

#endif // #ifndef MAX_CONTAINERS_STATEMACHINE_NODE_HPP
Loading
Loading