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{
38template <class T>
40{
41public:
42 using NameIdPair = std::pair<std::string, std::size_t>;
43 using NameIdMap = std::map<std::string, std::size_t>;
44
58 TemplateVec(std::string const& name, std::vector<T*>&& data_vec,
59 NameIdMap&& elem_name_map)
60 : _name(name),
61 _data_vec(std::move(data_vec)),
62 _name_id_map(std::move(elem_name_map))
63 {
64 }
65
69 virtual ~TemplateVec()
70 {
71 for (std::size_t k(0); k < size(); k++)
72 {
73 delete _data_vec[k];
74 }
75 }
76
80 void setName(const std::string& n) { _name = n; }
85 std::string getName() const { return _name; }
86
88 NameIdMap::const_iterator getNameIDMapBegin() const
89 {
90 return _name_id_map.cbegin();
91 }
92
94 NameIdMap::const_iterator getNameIDMapEnd() const
95 {
96 return _name_id_map.cend();
97 }
98
102 std::size_t size() const { return _data_vec.size(); }
103
108 std::vector<T*> const& getVector() const { return _data_vec; }
109
116 bool getElementIDByName(const std::string& name, std::size_t& id) const
117 {
118 auto it(_name_id_map.find(name));
119
120 if (it != _name_id_map.end())
121 {
122 id = it->second;
123 return true;
124 }
125 return false;
126 }
127
129 const T* getElementByName(const std::string& name) const
130 {
131 std::size_t id;
132 bool ret(getElementIDByName(name, id));
133 if (ret)
134 {
135 return _data_vec[id];
136 }
137
138 return nullptr;
139 }
140
150 bool getNameOfElementByID(std::size_t id, std::string& element_name) const
151 {
152 // search in map for id
153 auto it = findFirstElementByID(id);
154 if (it == _name_id_map.end())
155 {
156 return false;
157 }
158 element_name = it->first;
159 return true;
160 }
161
163 void setNameOfElementByID(std::size_t id, std::string const& element_name)
164 {
165 _name_id_map[element_name] = id;
166 }
167
176 bool getNameOfElement(const T* data, std::string& name) const
177 {
178 for (std::size_t k(0); k < _data_vec.size(); k++)
179 {
180 if (_data_vec[k] == data)
181 {
182 return getNameOfElementByID(k, name);
183 }
184 }
185
186 return false;
187 }
188
190 virtual void push_back(T* data_element,
191 std::string const* const name = nullptr)
192 {
193 _data_vec.push_back(data_element);
194 if (!name || name->empty())
195 {
196 return;
197 }
198
199 auto it(_name_id_map.find(*name));
200 if (it == _name_id_map.end())
201 {
202 _name_id_map[*name] = _data_vec.size() - 1;
203 }
204 else
205 {
206 WARN(
207 "Name '{:s}' exists already. The object will be inserted "
208 "without a name",
209 *name);
210 }
211 }
212
214 virtual void setNameForElement(std::size_t id, std::string const& name)
215 {
216 // Erase id if found in map.
217 auto it = findFirstElementByID(id);
218 if (it != _name_id_map.end())
219 {
220 _name_id_map.erase(it);
221 }
222
223 if (!name.empty())
224 {
225 // insert new or revised name
226 _name_id_map[name] = id;
227 }
228 }
229
230private:
231 NameIdMap::const_iterator findFirstElementByID(std::size_t const& id) const
232 {
233 return std::find_if(_name_id_map.begin(), _name_id_map.end(),
234 [id](NameIdPair const& elem)
235 { return elem.second == id; });
236 }
237
238protected:
239 TemplateVec(TemplateVec const&) = delete;
241 TemplateVec& operator=(TemplateVec const& rhs) = delete;
243
245 std::string _name;
246
250 std::vector<T*> _data_vec;
255};
256} // 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:40
NameIdMap _name_id_map
Definition: TemplateVec.h:254
std::map< std::string, std::size_t > NameIdMap
Definition: TemplateVec.h:43
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:129
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
Definition: TemplateVec.h:163
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:42
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition: TemplateVec.h:88
TemplateVec(TemplateVec &&)=delete
std::vector< T * > _data_vec
Definition: TemplateVec.h:250
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
Definition: TemplateVec.h:190
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:150
TemplateVec(std::string const &name, std::vector< T * > &&data_vec, NameIdMap &&elem_name_map)
Definition: TemplateVec.h:58
virtual void setNameForElement(std::size_t id, std::string const &name)
Sets the given name for the element of the given ID.
Definition: TemplateVec.h:214
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
Definition: TemplateVec.h:231
virtual ~TemplateVec()
Definition: TemplateVec.h:69
std::string getName() const
Definition: TemplateVec.h:85
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:116
std::size_t size() const
Definition: TemplateVec.h:102
void setName(const std::string &n)
Definition: TemplateVec.h:80
TemplateVec & operator=(TemplateVec const &rhs)=delete
bool getNameOfElement(const T *data, std::string &name) const
Definition: TemplateVec.h:176
TemplateVec & operator=(TemplateVec &&rhs)=delete
std::vector< T * > const & getVector() const
Definition: TemplateVec.h:108
TemplateVec(TemplateVec const &)=delete
std::string _name
Definition: TemplateVec.h:245
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition: TemplateVec.h:94