Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
property_map.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_CX_PROPERTY_MAP_H_
20#define HEDGEHOG_CX_PROPERTY_MAP_H_
21
22#ifdef HH_ENABLE_HH_CX
23
24#include <vector>
25#include <string>
26#include <algorithm>
27#include <stdexcept>
28
30namespace hh_cx {
31
37template<class PropertyType>
38class PropertyMap {
39 private:
40 static_assert(std::is_default_constructible_v<PropertyType>, "The property mapped should be default constructible.");
41 std::vector<std::string>
42 ids_{};
43 std::vector<PropertyType>
44 properties_{};
45
46 public:
48 constexpr PropertyMap() = default;
49
51 constexpr void clear() {
52 ids_.clear();
53 properties_.clear();
54 }
55
60 constexpr void insert(std::string const &staticNodeName, PropertyType const &property) {
61 if (std::find(ids_.cbegin(), ids_.cend(), staticNodeName) != ids_.cend()) {
62 throw std::runtime_error("The node has already been inserted.");
63 } else {
64 ids_.push_back(staticNodeName);
65 properties_.push_back(property);
66 }
67 }
68
72 constexpr void insert_or_assign(std::string const &staticNodeName, PropertyType const &property) {
73 auto posIt = std::find(ids_.cbegin(), ids_.cend(), staticNodeName);
74 if (posIt != ids_.cend()) {
75 properties_.at(std::distance(ids_.cbegin(), posIt)) = property;
76 } else {
77 ids_.push_back(staticNodeName);
78 properties_.push_back(property);
79 }
80 }
81
84 constexpr void erase(std::string const &staticNodeName) {
85 auto posIt = std::find(ids_.cbegin(), ids_.cend(), staticNodeName);
86 if (posIt != ids_.cend()) {
87 for(auto pos = std::distance(ids_.cbegin(), posIt); pos < ids_.size() - 1; ++pos){
88 ids_.at(pos) = ids_.at(pos + 1);
89 properties_.at(pos) = properties_.at(pos + 1);
90 ids_.pop_back();
91 properties_.pop_back();
92 }
93 }
94 }
95
99 [[nodiscard]] constexpr bool contains(std::string const &staticNodeName){
100 return std::find(ids_.cbegin(), ids_.cend(), staticNodeName) != ids_.cend();
101 }
102
107 [[nodiscard]] constexpr PropertyType & property(std::string const &staticNodeName){
108 if(contains(staticNodeName)){
109 return properties_.at(std::distance(ids_.cbegin(), std::find(ids_.cbegin(), ids_.cend(), staticNodeName)));
110 }else{
111 throw std::runtime_error("You are looking for hedgehog property associated to an unregistered node.");
112 }
113 }
114
115
116};
117}
118#endif //HH_ENABLE_HH_CX
119#endif //HEDGEHOG_CX_PROPERTY_MAP_H_