TuringSim
C++ framework to simulate abstract computing models
simpleStatePattern.h
1 #pragma once
2 
3 #include <state/statePattern.h>
4 
5 #include <variant>
6 
7 namespace TuringSim::State {
14  template <typename T>
15  class SimpleStatePattern : public StatePattern<T, std::monostate> {
17  public:
18  using typename StatePattern_::StateType;
19  using typename StatePattern_::MatcherType;
20 
21  static_assert(std::is_same_v<StateType, T>);
22  static_assert(std::is_same_v<MatcherType, std::monostate>);
23 
27  constexpr SimpleStatePattern(const T& acceptedState) :
28  StatePattern_(),
29  acceptedState(acceptedState)
30  {}
31 
35  constexpr SimpleStatePattern(const SimpleStatePattern& other) :
36  StatePattern_(other),
37  acceptedState(other.acceptedState)
38  {}
39 
44  StatePattern_(std::move(other)),
45  acceptedState(std::move(other.acceptedState))
46  {}
47 
52  constexpr SimpleStatePattern& operator=(const SimpleStatePattern& other) {
53  if(this != &other) {
55  acceptedState = other.acceptedState;
56  }
57  return *this;
58  }
59 
65  if(this != &other) {
66  StatePattern_::operator=(std::move(other));
67  acceptedState = std::move(other.acceptedState);
68  }
69  return *this;
70  }
71 
74  virtual ~SimpleStatePattern() override = default;
75 
82  constexpr const T& getState() const {
83  return acceptedState;
84  }
85 
90  virtual std::optional<MatcherType> match(const T& state) const noexcept override {
91  if(acceptedState == state) {
92  return std::monostate();
93  }
94  return std::nullopt;
95  }
96 
101  bool operator<(const SimpleStatePattern<T>& other) const {
102  return acceptedState < other.acceptedState;
103  }
104 
105  private:
106  T acceptedState;
107  };
108 }
TuringSim::State::SimpleStatePattern::operator=
constexpr SimpleStatePattern & operator=(const SimpleStatePattern &other)
Copy a state pattern.
Definition: simpleStatePattern.h:52
TuringSim::State::SimpleStatePattern
A simple explicit state pattern.
Definition: simpleStatePattern.h:15
TuringSim::State::SimpleStatePattern::~SimpleStatePattern
virtual ~SimpleStatePattern() override=default
Destructor.
TuringSim::State::StatePattern< T, std::monostate >::MatcherType
std::monostate MatcherType
The type returned when a state matches the pattern.
Definition: statePattern.h:25
TuringSim::State::SimpleStatePattern::SimpleStatePattern
constexpr SimpleStatePattern(const SimpleStatePattern &other)
Copy a state pattern.
Definition: simpleStatePattern.h:35
TuringSim::State::SimpleStatePattern::getState
constexpr const T & getState() const
get the accepting state
Definition: simpleStatePattern.h:82
TuringSim::State::SimpleStatePattern::operator<
bool operator<(const SimpleStatePattern< T > &other) const
Total ordering of SimpleStatePattern.
Definition: simpleStatePattern.h:101
TuringSim::State::SimpleStatePattern::match
virtual std::optional< MatcherType > match(const T &state) const noexcept override
Test if a state matches.
Definition: simpleStatePattern.h:90
TuringSim::State::SimpleStatePattern::operator=
constexpr SimpleStatePattern & operator=(SimpleStatePattern &&other)
Move a state pattern.
Definition: simpleStatePattern.h:64
TuringSim::State::StatePattern
The base class of all state patterns.
Definition: statePattern.h:15
TuringSim::State::SimpleStatePattern::SimpleStatePattern
constexpr SimpleStatePattern(SimpleStatePattern &&other)
Move a state pattern.
Definition: simpleStatePattern.h:43
TuringSim::State::StatePattern< T, std::monostate >::operator=
constexpr StatePattern & operator=(const StatePattern &other)=default
Copy a state pattern.
TuringSim::State::SimpleStatePattern::SimpleStatePattern
constexpr SimpleStatePattern(const T &acceptedState)
Construct a simple configuration from the letter in parameter.
Definition: simpleStatePattern.h:27
TuringSim::State::StatePattern< T, std::monostate >::StateType
T StateType
The type of states matched.
Definition: statePattern.h:20
TuringSim::State
The namespace for states and state patterns.