OGS
TemplateVec.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <algorithm>
18#include <cstdlib>
19#include <map>
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "BaseLib/Error.h"
26#include "BaseLib/Logging.h"
27
28namespace GeoLib
29{
36template <class T>
38{
39public:
40 using NameIdPair = std::pair<std::string, std::size_t>;
41 using NameIdMap = std::map<std::string, std::size_t>;
42
56 TemplateVec(std::string const& name, std::vector<T*>&& data_vec,
57 NameIdMap&& elem_name_map)
58 : _name(name),
59 _data_vec(std::move(data_vec)),
60 _name_id_map(std::move(elem_name_map))
61 {
62 }
63
67 virtual ~TemplateVec()
68 {
69 for (std::size_t k(0); k < size(); k++)
70 {
71 delete _data_vec[k];
72 }
73 }
74
78 void setName(const std::string& n) { _name = n; }
83 std::string getName() const { return _name; }
84
86 NameIdMap::const_iterator getNameIDMapBegin() const
87 {
88 return _name_id_map.cbegin();
89 }
90
92 NameIdMap::const_iterator getNameIDMapEnd() const
93 {
94 return _name_id_map.cend();
95 }
96
100 std::size_t size() const { return _data_vec.size(); }
101
106 std::vector<T*> const& getVector() const { return _data_vec; }
107
114 bool getElementIDByName(const std::string& name, std::size_t& id) const
115 {
116 auto it(_name_id_map.find(name));
117
118 if (it != _name_id_map.end())
119 {
120 id = it->second;
121 return true;
122 }
123 return false;
124 }
125
127 const T* getElementByName(const std::string& name) const
128 {
129 std::size_t id;
130 bool ret(getElementIDByName(name, id));
131 if (ret)
132 {
133 return _data_vec[id];
134 }
135
136 return nullptr;
137 }
138
148 bool getNameOfElementByID(std::size_t id, std::string& element_name) const
149 {
150 // search in map for id
151 auto it = findFirstElementByID(id);
152 if (it == _name_id_map.end())
153 {
154 return false;
155 }
156 element_name = it->first;
157 return true;
158 }
159
161 void setNameOfElementByID(std::size_t id, std::string const& element_name)
162 {
163 _name_id_map[element_name] = id;
164 }
165
174 bool getNameOfElement(const T* data, std::string& name) const
175 {
176 for (std::size_t k(0); k < _data_vec.size(); k++)
177 {
178 if (_data_vec[k] == data)
179 {
180 return getNameOfElementByID(k, name);
181 }
182 }
183
184 return false;
185 }
186
188 virtual void push_back(T* data_element,
189 std::string const* const name = nullptr)
190 {
191 _data_vec.push_back(data_element);
192 if (!name || name->empty())
193 {
194 return;
195 }
196
197 auto it(_name_id_map.find(*name));
198 if (it == _name_id_map.end())
199 {
200 _name_id_map[*name] = _data_vec.size() - 1;
201 }
202 else
203 {
204 WARN(
205 "Name '{:s}' exists already. The object will be inserted "
206 "without a name",
207 *name);
208 }
209 }
210
212 virtual void setNameForElement(std::size_t id, std::string const& name)
213 {
214 // Erase id if found in map.
215 auto it = findFirstElementByID(id);
216 if (it != _name_id_map.end())
217 {
218 _name_id_map.erase(it);
219 }
220
221 if (!name.empty())
222 {
223 // insert new or revised name
224 _name_id_map[name] = id;
225 }
226 }
227
228private:
229 NameIdMap::const_iterator findFirstElementByID(std::size_t const& id) const
230 {
231 return std::find_if(_name_id_map.begin(), _name_id_map.end(),
232 [id](NameIdPair const& elem)
233 { return elem.second == id; });
234 }
235
236protected:
237 TemplateVec(TemplateVec const&) = delete;
239 TemplateVec& operator=(TemplateVec const& rhs) = delete;
241
243 std::string _name;
244
248 std::vector<T*> _data_vec;
253};
254} // end namespace GeoLib
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition TemplateVec.h:38
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:41
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
std::pair< std::string, std::size_t > NameIdPair
Definition TemplateVec.h:40
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition TemplateVec.h:86
TemplateVec(TemplateVec &&)=delete
std::vector< T * > _data_vec
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
TemplateVec(std::string const &name, std::vector< T * > &&data_vec, NameIdMap &&elem_name_map)
Definition TemplateVec.h:56
virtual void setNameForElement(std::size_t id, std::string const &name)
Sets the given name for the element of the given ID.
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
virtual ~TemplateVec()
Definition TemplateVec.h:67
std::string getName() const
Definition TemplateVec.h:83
bool getElementIDByName(const std::string &name, std::size_t &id) const
std::size_t size() const
void setName(const std::string &n)
Definition TemplateVec.h:78
TemplateVec & operator=(TemplateVec const &rhs)=delete
bool getNameOfElement(const T *data, std::string &name) const
TemplateVec & operator=(TemplateVec &&rhs)=delete
std::vector< T * > const & getVector() const
TemplateVec(TemplateVec const &)=delete
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition TemplateVec.h:92