Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
copyable_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#ifndef HEDGEHOG_COPYABLE_ABSTRACTION_H_
20#define HEDGEHOG_COPYABLE_ABSTRACTION_H_
21#include <memory>
22#include <sstream>
23#include "../../../tools/concepts.h"
25
27namespace hh {
29namespace core {
31namespace abstraction {
32
35template<tool::CopyableNode CopyableNode>
37 private:
38 CopyableNode *const copyableNode_ = nullptr;
39 std::unique_ptr<std::set<std::shared_ptr<CopyableNode>>> nodeCopies_ = nullptr;
40
41 public:
44 explicit CopyableAbstraction(CopyableNode *const copyableNode)
45 : copyableNode_(copyableNode),
46 nodeCopies_(std::make_unique<std::set<std::shared_ptr<CopyableNode>>>()){}
47
49 virtual ~CopyableAbstraction() = default;
50
51 protected:
52
56 std::shared_ptr<CopyableNode> callCopy() {
57 auto copy = copyableNode_->copy();
58
59 if (copy == nullptr) {
60 std::ostringstream oss;
61 if (auto node = dynamic_cast<NodeAbstraction *>(this))
62 oss
63 << "A copy for the node \"" << node->name()
64 << "\" has been invoked but return nullptr. To fix this error, overload the "
65 << tool::typeToStr<CopyableNode>()
66 << "::copy function and return a valid object.";
67 else {
68 oss
69 << "A copy for the node has been invoked but return nullptr. To fix this error, overload the "
70 << tool::typeToStr<CopyableNode>()
71 << "::copy function and return a valid object.";
72 }
73 throw (std::runtime_error(oss.str()));
74 }
75
76 if (nodeCopies_->find(copy) != nodeCopies_->cend()) {
77 std::ostringstream oss;
78 if (auto node = dynamic_cast<NodeAbstraction *>(this)) {
79 oss << "A copy for the node \"" << node->name()
80 << "\" has been invoked and return a copy already registered. Each copy of a node should be different.";
81 }else {
82 oss << "A copy for the node has been invoked and return a copy already registered. Each copy of a node "
83 "should be different.";
84 }
85 throw (std::runtime_error(oss.str()));
86 }
87 nodeCopies_->insert(copy);
88 return copy;
89 }
90};
91}
92}
93}
94#endif //HEDGEHOG_COPYABLE_ABSTRACTION_H_
Hedgehog main namespace.
Core abstraction for copyable nodes.
CopyableAbstraction(CopyableNode *const copyableNode)
Constructor using a node abstraction.
std::shared_ptr< CopyableNode > callCopy()
Interface to call user-defined copy method.
std::unique_ptr< std::set< std::shared_ptr< CopyableNode > > > nodeCopies_
Set of copies of the node.
virtual ~CopyableAbstraction()=default
Default destructor.
CopyableNode *const copyableNode_
Pointer to copyable node abstraction.