TuringSim
C++ framework to simulate abstract computing models
transition.h
1 #pragma once
2 
3 #include <utils/messageException.h>
4 #include <utils/printer.h>
5 
9 namespace TuringSim::Transition {
14  template <typename TransitionType>
16  public:
20 
28  std::string message,
29  typename TransitionType::StateType_ preState,
30  typename TransitionType::StorageType_ storage,
31  typename TransitionType::ApplyHelperType& applyHelper
32  ) :
33  MessageException(message),
34  preState(preState),
35  storage(storage),
36  applyHelper(applyHelper)
37  {}
38 
39  protected:
40  virtual std::string makeFullMessage() const override {
41  using Utils::Debug::operator<<;
42  std::stringstream ss;
43  ss << this->msg << std::endl;
44  ss << " transition: " << transition << std::endl;
45  ss << " preState: " << preState << std::endl;
46  ss << " storage: " << storage << std::endl;
47  ss << " applyHelper: " << applyHelper << std::endl;
48  }
49 
50  TransitionType transition;
51  typename TransitionType::StateType_ preState;
52  typename TransitionType::StorageType_ storage;
53  typename TransitionType::ApplyHelperType& applyHelper;
54  };
55 
62  template <
63  typename StateType_,
64  typename StorageType_,
65  typename ApplyHelperType_
66  >
67  class Transition {
68  public:
72  typedef StateType_ StateType;
73 
77  typedef StorageType_ StorageType;
78 
82  typedef ApplyHelperType_ ApplyHelperType;
83 
85  Transition() = default;
86 
88  Transition(const Transition&) = default;
89 
91  Transition(Transition&&) = default;
92 
96  Transition& operator=(const Transition&) = default;
97 
102 
104  virtual ~Transition() = default;
105 
113  virtual std::optional<ApplyHelperType> match(const StateType& state, const StorageType& storage) const = 0;
114 
127  virtual StateType apply(const StateType& state, StorageType& storage, ApplyHelperType&& helper, bool& running) const = 0;
128  };
129 
130 }
TuringSim::Transition::Transition::apply
virtual StateType apply(const StateType &state, StorageType &storage, ApplyHelperType &&helper, bool &running) const =0
Apply a previously matching transition and return the new state.
TuringSim::Transition::ConfigurationDoesNotMatchException::ConfigurationDoesNotMatchException
ConfigurationDoesNotMatchException()=delete
ConfigurationDoesNotMatchException must be build with interesting data.
TuringSim::Transition::ConfigurationDoesNotMatchException::storage
TransitionType::StorageType_ storage
the storage before transition application.
Definition: transition.h:52
TuringSim::Transition::Transition::ApplyHelperType
ApplyHelperType_ ApplyHelperType
The type of apply helpers.
Definition: transition.h:82
TuringSim::Transition::ConfigurationDoesNotMatchException::applyHelper
TransitionType::ApplyHelperType & applyHelper
the apply helper provided to apply.
Definition: transition.h:53
TuringSim::Transition::Transition::Transition
Transition()=default
The default constructor.
TuringSim::Transition
Namespace for transitions.
TuringSim::Utils::MessageException
The Base class for all custom exceptions.
Definition: messageException.h:12
TuringSim::Transition::Transition::StateType
StateType_ StateType
The type of states.
Definition: transition.h:72
TuringSim::Transition::Transition
Base class for transitions.
Definition: transition.h:67
TuringSim::Transition::Transition::~Transition
virtual ~Transition()=default
The default virtual destructor.
TuringSim::Transition::ConfigurationDoesNotMatchException::transition
TransitionType transition
the transition we apply.
Definition: transition.h:50
TuringSim::Transition::Transition::operator=
Transition & operator=(Transition &&)=default
The default move assignment operator.
TuringSim::Transition::Transition::Transition
Transition(const Transition &)=default
The default copy constructor.
TuringSim::Transition::ConfigurationDoesNotMatchException::preState
TransitionType::StateType_ preState
the state before transition application.
Definition: transition.h:51
TuringSim::Transition::Transition::Transition
Transition(Transition &&)=default
The default move constructor.
TuringSim::Transition::Transition::match
virtual std::optional< ApplyHelperType > match(const StateType &state, const StorageType &storage) const =0
Test whether the transition matches a configuration. Returned optional is empty iff the transition do...
TuringSim::Transition::ConfigurationDoesNotMatchException::makeFullMessage
virtual std::string makeFullMessage() const override
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
Definition: transition.h:40
TuringSim::Transition::Transition::operator=
Transition & operator=(const Transition &)=default
The default copy assignment operator.
TuringSim::Transition::Transition::StorageType
StorageType_ StorageType
The type of storage.
Definition: transition.h:77
TuringSim::Transition::ConfigurationDoesNotMatchException
Exception thrown when we try to apply a transition to a configuration which does not match.
Definition: transition.h:15