Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
state_manager.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19#ifndef HEDGEHOG_STATE_MANAGER_H_
20#define HEDGEHOG_STATE_MANAGER_H_
21
22#include <utility>
23
24#include "abstract_state.h"
25#include "../../behavior/copyable.h"
26#include "../../behavior/task_node.h"
27
29namespace hh {
30
59template<size_t Separator, class ...AllTypes>
61 : public behavior::TaskNode,
64 public behavior::Copyable<StateManager<Separator, AllTypes...>>,
65 public tool::BehaviorMultiReceiversTypeDeducer_t<tool::Inputs<Separator, AllTypes...>>,
66 public tool::BehaviorMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>> {
67 private:
68 std::shared_ptr<AbstractState<Separator, AllTypes...>> state_ = nullptr;
69 std::shared_ptr<core::CoreStateManager<Separator, AllTypes...>> coreStateManager_ = nullptr;
70
71 public:
78 explicit StateManager(
80 std::string const &name = "State manager",
81 bool const automaticStart = false)
82 : behavior::TaskNode(std::make_shared<core::CoreStateManager<Separator, AllTypes...>>(this,
83 state,
84 name,
86 behavior::Copyable<StateManager<Separator, AllTypes...>>(1),
87 tool::BehaviorMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>>(),
88 state_(state) {
89 if (state == nullptr) {
90 throw std::runtime_error("The state given to the state manager should be valid (!= nullptr).");
91 }
92
93 if (auto
94 coreStateManager = std::dynamic_pointer_cast<core::CoreStateManager<Separator, AllTypes...>>(this->core())) {
96 } else {
97 throw std::runtime_error("The core used by the state manager should be a CoreStateManager.");
98 }
99 }
100
107 explicit StateManager(
110 : behavior::TaskNode(std::move(core)),
111 behavior::Copyable<StateManager<Separator, AllTypes...>>(1),
112 tool::BehaviorMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>>() {
113 if (state == nullptr) {
114 throw std::runtime_error("The state given to the state manager should be valid (!= nullptr).");
115 }
116 if (this->core() == nullptr) {
117 throw std::runtime_error("The core given to the state manager should be valid (!= nullptr).");
118 }
119 if (auto
120 coreStateManager = std::dynamic_pointer_cast<core::CoreStateManager<Separator, AllTypes...>>(this->core())) {
122 } else {
123 throw std::runtime_error("The core used by the state manager should be a CoreStateManager.");
124 }
125 }
126
128 ~StateManager() override = default;
129
132 std::shared_ptr<AbstractState<Separator, AllTypes...>> const &state() const { return state_; }
133
136 [[nodiscard]] bool automaticStart() const { return this->coreStateManager()->automaticStart(); }
137
140 std::shared_ptr<core::CoreStateManager<Separator, AllTypes...>> const &coreStateManager() const {
141 return coreStateManager_;
142 }
143
146 [[nodiscard]] bool canTerminate() const override {
147 return !coreStateManager_->hasNotifierConnected() && coreStateManager_->receiversEmpty();
148 }
149
153 std::shared_ptr<StateManager<Separator, AllTypes...>> copy() final {
154 auto copy = copyStateManager(this->state_);
155 if (copy == nullptr) {
156 std::ostringstream oss;
157 oss
158 << "A copy of the state manager " << this->name() << " has been invoked but return nullptr. "
159 << "Please implement the copyStateManager method to create a copy of the current state manager with the same state.";
160 throw std::runtime_error(oss.str());
161 }
162 if (copy->state_ != this->state_) {
163 std::ostringstream oss;
164 oss << "A copy and the state manager \"" << this->name() << "\" do not share the same state.\n";
165 throw std::runtime_error(oss.str());
166 }
167 return copy;
168 }
169
173 virtual std::shared_ptr<StateManager<Separator, AllTypes...>>
175 return std::make_shared<StateManager<Separator, AllTypes...>>(state, this->name(), this->automaticStart());
176 }
177
178};
179}
180
181#endif //HEDGEHOG_STATE_MANAGER_H_
Hedgehog main namespace.
Hedgehog AbstractState.
AbstractState manager.
Definition: state_manager.h:66
bool automaticStart() const
Automatic start flag accessor.
~StateManager() override=default
Default destructor for the state manager.
std::shared_ptr< AbstractState< Separator, AllTypes... > > const & state() const
AbstractState accessor.
std::shared_ptr< AbstractState< Separator, AllTypes... > > state_
AbstractState managed.
Definition: state_manager.h:68
std::shared_ptr< core::CoreStateManager< Separator, AllTypes... > > const & coreStateManager() const
Accessor to the core.
std::shared_ptr< StateManager< Separator, AllTypes... > > copy() final
Provide a copy of the state manager.
StateManager(std::shared_ptr< AbstractState< Separator, AllTypes... > > state, std::string const &name="State manager", bool const automaticStart=false)
Main state manager constructor.
Definition: state_manager.h:78
StateManager(std::shared_ptr< core::CoreStateManager< Separator, AllTypes... > > core, std::shared_ptr< AbstractState< Separator, AllTypes... > > state)
AbstractState Manager constructor with a user-custom core.
virtual std::shared_ptr< StateManager< Separator, AllTypes... > > copyStateManager(std::shared_ptr< AbstractState< Separator, AllTypes... > > state)
Customizable copy method.
bool canTerminate() const override
Default termination rule, it terminates if there is no predecessor connection and there is no input d...
std::shared_ptr< core::CoreStateManager< Separator, AllTypes... > > coreStateManager_
AbstractState manager core.
Definition: state_manager.h:69
Behavior abstraction for nodes that expose termination condition.
Definition: can_terminate.h:30
Cleanable interface.
Definition: cleanable.h:29
Copy interface used to copy a node when either a group of nodes is created or a node is duplicated wh...
Definition: copyable.h:37
Copyable(size_t const numberThreads)
Copyable constructor, set the number of threads for a node.
Definition: copyable.h:44
std::string name() const
Node's name accessor.
Definition: node.h:53
std::shared_ptr< hh::core::abstraction::NodeAbstraction > const & core() const
Core accessor.
Definition: node.h:49
Behavior abstraction for TaskNode.
Definition: task_node.h:35
TaskNode(std::shared_ptr< hh::core::abstraction::TaskNodeAbstraction > core)
Constructor using a core.
Definition: task_node.h:44
AbstractState manager core.