Utopia  2
Framework for studying models of complex & adaptive systems.
state.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_CORE_STATE_HH
2 #define UTOPIA_CORE_STATE_HH
3 
4 namespace Utopia {
5 
12 
20 enum class Update {
21  manual,
22  sync,
23  async
24 };
25 
27 
36 template<typename StateType, Update mode>
38 
40 template<typename StateType>
41 class StateContainer<StateType, Update::manual>
42 {
43 public:
44  using State = StateType;
46 
48 
51  StateContainer (const State state_initial) :
52  state(state_initial)
53  { }
54 };
55 
57 
62 template<typename StateType>
63 class StateContainer<StateType, Update::async>
64 {
65 public:
67  using State = StateType;
68 
70  static constexpr bool is_sync () { return false; }
71 
73  StateContainer (const State state):
74  _state(state)
75  { }
76 
78  State& state () { return _state; }
80  const State& state () const { return _state; }
81 
82 private:
84 };
85 
87 
92 template<typename StateType>
93 class StateContainer<StateType, Update::sync>
94 {
95 public:
97  using State = StateType;
98 
100  static constexpr bool is_sync () { return true; }
101 
103  StateContainer (const State state):
104  _state(state),
105  _state_new(state)
106  { }
107 
109  State& state_new () { return _state_new; }
111  const State& state () const { return _state; }
113  void update () noexcept { _state = _state_new; }
114 
115 private:
118 };
119 
124 } // namespace Utopia
125 
126 #endif // UTOPIA_CORE_STATE_HH
State & state()
Return reference to state.
Definition: state.hh:78
static constexpr bool is_sync()
Export implementation type.
Definition: state.hh:70
StateType State
Type of state.
Definition: state.hh:67
StateContainer(const State state)
Construct state container with specific state.
Definition: state.hh:73
const State & state() const
Return const reference to state.
Definition: state.hh:80
StateContainer(const State state_initial)
Construct state container with specific state.
Definition: state.hh:51
State & state_new()
Return reference to state cache.
Definition: state.hh:109
static constexpr bool is_sync()
Export implementation type.
Definition: state.hh:100
void update() noexcept
Overwrite state with state cache.
Definition: state.hh:113
StateContainer(const State state)
Construct state container with specific state.
Definition: state.hh:103
StateType State
Type of state.
Definition: state.hh:97
const State & state() const
Return const reference to state.
Definition: state.hh:111
Container for states.
Definition: state.hh:37
@ manual
Fully manual: write_data method is always called.
Update
Update modes when applying rules.
Definition: state.hh:20
@ async
Asynchronous update.
@ manual
User chooses update type when calling apply_rule()
@ sync
Synchronous update.
Definition: agent.hh:11