Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
abstract_state.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_ABSTRACT_STATE_H
21 #define HEDGEHOG_ABSTRACT_STATE_H
22 
23 #include <memory>
24 #include <queue>
25 #include <shared_mutex>
26 
27 #include "../../behavior/execute.h"
28 #include "../../tools/traits.h"
29 
31 namespace hh {
32 
57 template<class StateOutput, class ...StateInputs>
58 class AbstractState : public behavior::Execute<StateInputs> ... {
59  static_assert(traits::isUnique<StateInputs...>, "A Task can't accept multiple inputs with the same type.");
60  static_assert(sizeof... (StateInputs) >= 1, "A state need to have one output type and at least one output type.");
61  private:
62  mutable std::unique_ptr<std::shared_mutex> stateMutex_ = nullptr;
63  std::unique_ptr<std::queue<std::shared_ptr<StateOutput>>> readyList_ = nullptr;
64 
65  public:
69  stateMutex_ = std::make_unique<std::shared_mutex>();
70  readyList_ = std::make_unique<std::queue<std::shared_ptr<StateOutput>>>();
71  }
72 
74  virtual ~AbstractState() = default;
75 
78  std::unique_ptr<std::queue<std::shared_ptr<StateOutput>>> const &readyList() const { return readyList_; }
79 
82  void push(std::shared_ptr<StateOutput> const &elem) { readyList_->push(elem); }
83 
86  std::shared_ptr<StateOutput> frontAndPop() {
87  std::shared_ptr<StateOutput> elem = readyList_->front();
88  readyList_->pop();
89  return elem;
90  }
91 
93  void lock() { stateMutex_->lock(); }
94 
96  void unlock() { stateMutex_->unlock(); }
97 };
98 
99 }
100 #endif //HEDGEHOG_ABSTRACT_STATE_H
AbstractState()
Default constructor, initialize the mutex (AbstractState::stateMutex_) and the ready list (AbstractSt...
Hedgehog main namespace.
std::unique_ptr< std::queue< std::shared_ptr< StateOutput > > > readyList_
State Ready list.
void lock()
Lock the state.
State Interface for managing computation, need a corresponding AbstractStateManager to be embedded in...
std::unique_ptr< std::shared_mutex > stateMutex_
State Mutex.
void unlock()
Unlock the state.
Execute Behavior definition, node that has an execution for an Input data type.
Definition: execute.h:30
virtual ~AbstractState()=default
Default destructor.
std::shared_ptr< StateOutput > frontAndPop()
Used by AbstractStateManager to get the ready list&#39;s front element.
std::unique_ptr< std::queue< std::shared_ptr< StateOutput > > > const & readyList() const
Ready list accessor.
void push(std::shared_ptr< StateOutput > const &elem)
Add an element to the ready list.