Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
state_manager.h
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 
20 #ifndef HEDGEHOG_STATE_MANAGER_H
21 #define HEDGEHOG_STATE_MANAGER_H
22 
23 #include "../abstract_task.h"
24 #include "abstract_state.h"
25 
27 namespace hh {
28 // Have to add -Woverloaded-virtual for clang because execute hides overloaded virtual function
29 #if defined (__clang__)
30 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Woverloaded-virtual"
32 #endif //__clang//
33 
35 namespace behavior {
36 
40 template<class StateOutput, class ...StateInputs>
41 class BaseStateManager : public AbstractTask<StateOutput, StateInputs...> {
42  public:
45  : AbstractTask<StateOutput, StateInputs...>("StateManager", 1, core::NodeType::StateManager, false) {}
46 
47 };
48 
65 template<class StateInput, class StateOutput, class ...StateInputs>
66 class StateManagerExecuteDefinition : public virtual BaseStateManager<StateOutput, StateInputs...> {
67  private:
68  std::shared_ptr<AbstractState<StateOutput, StateInputs...>> es_;
69  public:
75  : es_(state) { }
76 
78  virtual ~StateManagerExecuteDefinition() = default;
79 
82  void execute(std::shared_ptr<StateInput> input) final {
83  this->es_->lock();
84  std::static_pointer_cast<behavior::Execute<StateInput>>(this->es_)->execute(input);
85  while (!this->es_->readyList()->empty()) { this->addResult(this->es_->frontAndPop()); }
86  this->es_->unlock();
87  }
88 };
89 
90 }
91 #if defined (__clang__)
92 #pragma clang diagnostic pop
93 #endif //__clang//
94 
116 template<class StateOutput, class ...StateInputs>
117  class StateManager : public behavior::StateManagerExecuteDefinition<StateInputs, StateOutput, StateInputs...> ... {
118 
119  private:
120  std::shared_ptr<AbstractState<StateOutput, StateInputs...>> state_ = nullptr;
121 
122  public:
123  using behavior::StateManagerExecuteDefinition<StateInputs, StateOutput, StateInputs...>::execute...;
124 
126  StateManager() = delete;
127 
134  template<
135  class StateType,
136  class IsCompatibleState = typename std::enable_if_t<
137  std::is_base_of_v<AbstractState<StateOutput, StateInputs...>, StateType>
138  >
139  >
140  explicit StateManager(std::shared_ptr<StateType> const state) :
141  behavior::StateManagerExecuteDefinition<StateInputs, StateOutput, StateInputs...>(state)...,
142  state_(state) {}
143 
151  template<
152  class StateType,
153  class IsCompatibleState = typename std::enable_if_t<
154  std::is_base_of_v<AbstractState<StateOutput, StateInputs...>, StateType>
155  >
156  >
157  StateManager(std::shared_ptr<StateType> const state, bool automaticStart) :
158  behavior::StateManagerExecuteDefinition<StateInputs, StateOutput, StateInputs...>(state)...,
159  state_(state) {
160  std::dynamic_pointer_cast<core::CoreTask<StateOutput, StateInputs...>>(this->core())->automaticStart(automaticStart);
161  }
162 
171  template<
172  class StateType,
173  class IsCompatibleState = typename std::enable_if_t<
174  std::is_base_of_v<AbstractState<StateOutput, StateInputs...>, StateType>
175  >
176  >
177  StateManager(std::string_view const name,
178  std::shared_ptr<StateType> const state,
179  bool automaticStart = false) :
180  behavior::StateManagerExecuteDefinition<StateInputs, StateOutput, StateInputs...>(state)..., state_(state) {
181  this->core()->name(name);
182  std::dynamic_pointer_cast<core::CoreTask<StateOutput, StateInputs...>>(this->core())->automaticStart(automaticStart);
183  }
184 
187  std::shared_ptr<AbstractTask<StateOutput, StateInputs...>> copy() override {
188  return std::make_shared<StateManager<StateOutput, StateInputs...>>(this->name(),
189  this->state(),
190  this->automaticStart());
191  }
192 
193  protected:
196  std::shared_ptr<AbstractState<StateOutput, StateInputs...>> const &state() const { return state_; }
197 };
198 }
199 #endif //HEDGEHOG_STATE_MANAGER_H
std::shared_ptr< AbstractState< StateOutput, StateInputs... > > const & state() const
Managed state accessor.
Base node for computation.
Definition: abstract_task.h:76
void execute(std::shared_ptr< StateInput > input) final
Default execute method definition.
Definition: state_manager.h:82
StateManagerExecuteDefinition(std::shared_ptr< AbstractState< StateOutput, StateInputs... >> const &state)
Constructor for an AbstractStateManager with name, state, and automaticStart as mandatory parameters...
Definition: state_manager.h:74
void addResult(std::shared_ptr< StateOutput > output)
Add an output data.
Implementation of the execute method for the StateManager.
Definition: state_manager.h:66
Hedgehog main namespace.
std::shared_ptr< AbstractTask< StateOutput, StateInputs... > > copy() override
Default override of the copy method, copy the name, the state and the automatic start.
Core of the task node.
Definition: core_task.h:56
std::shared_ptr< core::CoreNode > core() final
Task&#39;s core accessor.
std::shared_ptr< AbstractState< StateOutput, StateInputs... > > es_
State used in execute definition.
Definition: state_manager.h:68
Base State Manager, needed to avoid virtual inheritance of AbstractTask.
Definition: state_manager.h:41
StateManager(std::string_view const name, std::shared_ptr< StateType > const state, bool automaticStart=false)
Constructor for a StateManager with name and state as mandatory parameter, and automatic start as opt...
StateManager(std::shared_ptr< StateType > const state, bool automaticStart)
Constructor for an StateManager with state and automatic start as mandatory parameter.
Hedgehog graph&#39;s node, used to manage an AbstractState.
StateManager(std::shared_ptr< StateType > const state)
Constructor for a StateManager with state as mandatory parameter.
State Interface for managing computation, need a corresponding AbstractStateManager to be embedded in...
BaseStateManager()
DEfault constructor, set the base property for a state manager.
Definition: state_manager.h:44
Execute Behavior definition, node that has an execution for an Input data type.
Definition: execute.h:30
std::string_view name()
Task&#39;s name accessor.
bool automaticStart()
Task&#39;s automatic start accessor.
virtual void execute(std::shared_ptr< TaskInputs >)=0
Virtual declaration of execute function for a data of type Input.