Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
core_graph_receiver.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_CORE_GRAPH_RECEIVER_H
21 #define HEDGEHOG_CORE_GRAPH_RECEIVER_H
22 
23 #include "../../base/receiver/core_receiver.h"
24 
26 namespace hh::core {
27 
30 template<class GraphInput>
31 class CoreGraphReceiver : public virtual CoreReceiver<GraphInput> {
32  private:
33  std::unique_ptr<std::set<CoreReceiver < GraphInput> *>> graphReceiverInputs_ = nullptr;
34 
36  public:
41  CoreGraphReceiver(std::string_view const &name, NodeType const type, size_t const numberThreads) :
42  CoreNode(name, type, numberThreads),
43  CoreReceiver<GraphInput>(name, type, numberThreads) {
44  HLOG_SELF(0, "Creating CoreGraphReceiver with type: " << (int) type << " and name: " << name)
45  this->graphReceiverInputs_ = std::make_unique<std::set<CoreReceiver<GraphInput> *>>();
46  }
47 
49  virtual ~CoreGraphReceiver() {
50  HLOG_SELF(0, "Destructing CoreGraphReceiver")
51  }
52 
56  void addSender(CoreSender <GraphInput> *sender) final {
57  HLOG_SELF(0, "Add sender " << sender->name() << "(" << sender->id() << ")")
58  for (CoreReceiver<GraphInput> *inputNode: *(this->graphReceiverInputs_)) {
59  inputNode->addSender(sender);
60  }
61  }
62 
66  void removeSender(CoreSender <GraphInput> *sender) final {
67  HLOG_SELF(0, "Remove sender " << sender->name() << "(" << sender->id() << ")")
68  for (CoreReceiver<GraphInput> *inputNode: *(this->graphReceiverInputs_)) {
69  inputNode->removeSender(sender);
70  }
71  }
72 
75  void receive(std::shared_ptr<GraphInput> ptr) final {
76  HLOG_SELF(2, "Receive data")
77  for (CoreReceiver<GraphInput> *inputNode: *(this->graphReceiverInputs_)) { inputNode->receive(ptr); }
78  }
79 
83  HLOG_SELF(0, "Add Graph Receiver Input " << receiver->name() << "(" << receiver->id() << ")")
84  this->graphReceiverInputs_->insert(receiver);
85  }
86 
89  bool receiverEmpty() final {
90  auto result = true;
91  for (CoreReceiver<GraphInput> *inputNode: *(this->graphReceiverInputs_)) {
92  result &= inputNode->receiverEmpty();
93  }
94  HLOG_SELF(2, "Test receiver empty " << "(" << result << ")")
95  return result;
96  }
97 
100  std::set<CoreReceiver < GraphInput> *>
101  receivers() override {
102  std::set<CoreReceiver<GraphInput> *> receivers{};
103  std::set<CoreReceiver<GraphInput> *> tempReceivers;
104  for (CoreReceiver<GraphInput> *receiver : *(this->graphReceiverInputs_)) {
105  tempReceivers = receiver->receivers();
106  receivers.insert(tempReceivers.begin(), tempReceivers.end());
107  }
108  return receivers;
109  }
110 
114  [[noreturn]] behavior::Node *node() override {
115  std::ostringstream oss;
116  oss << "Internal error, should not be called, graph does not have a node: " << __FUNCTION__;
117  HLOG_SELF(0, oss.str())
118  throw (std::runtime_error(oss.str()));
119  }
120 };
121 
122 }
123 #endif //HEDGEHOG_CORE_GRAPH_RECEIVER_H
NodeType type() const
Node type accessor.
Definition: core_node.h:132
Receiver Interface, receive one data type from CoreSender.
Definition: core_receiver.h:44
CoreGraphReceiver(std::string_view const &name, NodeType const type, size_t const numberThreads)
CoreGraphReceiver constructor.
Graph Receiver for a type GraphInput.
void addGraphReceiverInput(CoreReceiver< GraphInput > *receiver)
Register a CoreReceiver from an input node.
Node Behavior definition.
Definition: node.h:39
virtual ~CoreGraphReceiver()
CoreGraphReceiver destructor.
NodeType
Hedgehog node&#39;s type.
Definition: core_node.h:40
Sender interface, send data to CoreReceiver.
Definition: core_sender.h:35
Main Hedgehog core abstraction.
Definition: core_node.h:48
Hedgehog core namespace.
Definition: core_execute.h:25
std::unique_ptr< std::set< CoreReceiver< GraphInput > * > > graphReceiverInputs_
Graph receivers (Input node receivers)
std::string_view const & name() const
Node name accessor.
Definition: core_node.h:128
void addSender(CoreSender< GraphInput > *sender) final
Add a CoreSender to the graph.
void removeSender(CoreSender< GraphInput > *sender) final
Remove a CoreSender from the graph.
size_t numberThreads() const
Number of threads associated accessor.
Definition: core_node.h:152
std::set< CoreReceiver< GraphInput > * > receivers() override
Get a set of CoreReceiver built from the input nodes.
behavior::Node * node() override
Get a node from the graph, that does not exist, throw an error in every case.
void receive(std::shared_ptr< GraphInput > ptr) final
Define how the graph receives data for a specific type and sends the data to all input nodes...
virtual std::string id() const
Unique Id accessor.
Definition: core_node.h:114
bool receiverEmpty() final
Test emptiness in all graph receivers.