Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
sender_abstraction.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
20
21#ifndef HEDGEHOG_SENDER_ABSTRACTION_H
22#define HEDGEHOG_SENDER_ABSTRACTION_H
23
24#include <memory>
25#include <utility>
26#include <ostream>
27
28#include "../../../implementors/implementor/implementor_notifier.h"
29#include "../../../implementors/implementor/implementor_sender.h"
30#include "../clonable_abstraction.h"
31
33namespace hh {
35namespace core {
36
38namespace abstraction {
39
40#ifndef DOXYGEN_SHOULD_SKIP_THIS
41template<class Input>
42class ReceiverAbstraction;
43#endif //DOXYGEN_SHOULD_SKIP_THIS
44
47template<class Output>
49 private:
50 std::shared_ptr<implementor::ImplementorSender<Output>>
51 concreteSender_ = nullptr;
52
53 public:
56 explicit SenderAbstraction(std::shared_ptr<implementor::ImplementorSender<Output>> concreteSender)
57 : concreteSender_(std::move(concreteSender)) {
58 concreteSender_->initialize(this);
59 }
60
62 virtual ~SenderAbstraction() = default;
63
68 [[nodiscard]] std::set<SenderAbstraction<Output> *> const &senders() const { return concreteSender_->senders(); }
69
74 [[nodiscard]] std::set<SenderAbstraction<Output> *> &senders() { return concreteSender_->senders(); }
75
78 [[nodiscard]] std::set<ReceiverAbstraction<Output> *> const &connectedReceivers() const {
79 return concreteSender_->connectedReceivers();
80 }
81
84 void addReceiver(ReceiverAbstraction<Output> *const receiver) { concreteSender_->addReceiver(receiver); }
85
88 void removeReceiver(ReceiverAbstraction<Output> *const receiver) { concreteSender_->removeReceiver(receiver); }
89
92 void send(std::shared_ptr<Output> data) { concreteSender_->send(data); }
93
94 protected:
98 this->concreteSender_ = copyableCore->concreteSender_;
99 }
100
105 void duplicateEdgeSender(std::map<abstraction::NodeAbstraction *, std::shared_ptr<NodeAbstraction>> &mapping) {
106 std::shared_ptr<NodeAbstraction> duplicateReceiver;
107 auto senderAsNode = dynamic_cast<abstraction::NodeAbstraction *>(this);
108 if (!mapping.contains(senderAsNode)) {
109 throw std::runtime_error("A node that we are trying to connect is not mapped yet.");
110 }
111 auto mappedSender = std::dynamic_pointer_cast<SenderAbstraction < Output>>
112 (mapping.at(senderAsNode));
113 if (mappedSender == nullptr) {
114 std::ostringstream oss;
115 oss << "The mapped type of a node is not of the right type: Sender<" << hh::tool::typeToStr<Output>() << ">.";
116 throw std::runtime_error(oss.str());
117 }
118
119 for (auto &sender : this->senders()) {
120 for (auto &receiver : sender->connectedReceivers()) {
121 for (auto &r : receiver->receivers()) {
122 if (auto receiverAsNode = dynamic_cast<abstraction::NodeAbstraction *>(r)) {
123 if (mapping.contains(receiverAsNode)) {
124 auto mappedReceiver = std::dynamic_pointer_cast<ReceiverAbstraction<Output>>(mapping.at(receiverAsNode));
125
126 if (mappedReceiver == nullptr) {
127 std::ostringstream oss;
128 oss
129 << "The mapped type of a node is not of the right type: ReceiverAbstraction<"
130 << hh::tool::typeToStr<Output>() << ">.";
131 throw std::runtime_error(oss.str());
132 }
133
134 mappedSender->addReceiver(mappedReceiver.get());
135 mappedReceiver->addSender(mappedSender.get());
136 }
137 } else {
138 throw std::runtime_error("A receiver is not a node when duplicating edges.");
139 }
140 }
141 }
142
143 }
144
145 }
146};
147}
148}
149}
150#endif //HEDGEHOG_SENDER_ABSTRACTION_H
Hedgehog main namespace.
Core's abstraction to receive a piece of data.
Core abstraction to send data.
SenderAbstraction(std::shared_ptr< implementor::ImplementorSender< Output > > concreteSender)
Constructor using the concrete implementation.
std::set< SenderAbstraction< Output > * > const & senders() const
Const accessor to senders.
void duplicateEdgeSender(std::map< abstraction::NodeAbstraction *, std::shared_ptr< NodeAbstraction > > &mapping)
Duplicate edges of the current sender to receiver to clone in map.
void copyInnerStructure(SenderAbstraction< Output > *copyableCore)
Copy inner structure of the sender to this one.
void removeReceiver(ReceiverAbstraction< Output > *const receiver)
Remove a ReceiverAbstraction.
void addReceiver(ReceiverAbstraction< Output > *const receiver)
Add a ReceiverAbstraction.
std::set< ReceiverAbstraction< Output > * > const & connectedReceivers() const
Accessor to the receivers attached to this SenderAbstraction.
std::set< SenderAbstraction< Output > * > & senders()
Accessor to senders.
virtual ~SenderAbstraction()=default
Default destructor.
void send(std::shared_ptr< Output > data)
Send a data as output of the node.
std::shared_ptr< implementor::ImplementorSender< Output > > concreteSender_
Concrete implementation of the sender used in the node.
Implementor for the SenderAbstraction.