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>
37class TemplateVec
38{
39public:
40 using NameIdPair = std::pair<std::string, std::size_t>;
41 using NameIdMap = std::map<std::string, std::size_t>;
42
55 TemplateVec(std::string const& name, std::vector<T*>&& data_vec,
56 NameIdMap&& elem_name_map)
57 : _name(name),
58 _data_vec(std::move(data_vec)),
59 _name_id_map(std::move(elem_name_map))
60 {
61 }
62
66 virtual ~TemplateVec()
67 {
68 for (std::size_t k(0); k < size(); k++)
69 {
70 delete _data_vec[k];
71 }
72 }
73
77 void setName(const std::string& n) { _name = n; }
82 std::string getName() const { return _name; }
83
85 NameIdMap::const_iterator getNameIDMapBegin() const
86 {
87 return _name_id_map.cbegin();
88 }
89
91 NameIdMap::const_iterator getNameIDMapEnd() const
92 {
93 return _name_id_map.cend();
94 }
95
99 std::size_t size() const { return _data_vec.size(); }
100
105 std::vector<T*> const& getVector() const { return _data_vec; }
106
113 bool getElementIDByName(const std::string& name, std::size_t& id) const
114 {
115 auto it(_name_id_map.find(name));
116
117 if (it != _name_id_map.end())
118 {
119 id = it->second;
120 return true;
121 }
122 return false;
123 }
124
126 const T* getElementByName(const std::string& name) const
127 {
128 std::size_t id;
129 bool ret(getElementIDByName(name, id));
130 if (ret)
131 {
132 return _data_vec[id];
133 }
134
135 return nullptr;
136 }
137
147 bool getNameOfElementByID(std::size_t id, std::string& element_name) const
148 {
149 // search in map for id
150 auto it = findFirstElementByID(id);
151 if (it == _name_id_map.end())
152 {
153 return false;
154 }
155 element_name = it->first;
156 return true;
157 }
158
160 void setNameOfElementByID(std::size_t id, std::string const& element_name)
161 {
162 _name_id_map[element_name] = id;
163 }
164
173 bool getNameOfElement(const T* data, std::string& name) const
174 {
175 for (std::size_t k(0); k < _data_vec.size(); k++)
176 {
177 if (_data_vec[k] == data)
178 {
179 return getNameOfElementByID(k, name);
180 }
181 }
182
183 return false;
184 }
185
187 virtual void push_back(T* data_element,
188 std::string const* const name = nullptr)
189 {
190 _data_vec.push_back(data_element);
191 if (!name || name->empty())
192 {
193 return;
194 }
195
196 auto it(_name_id_map.find(*name));
197 if (it == _name_id_map.end())
198 {
199 _name_id_map[*name] = _data_vec.size() - 1;
200 }
201 else
202 {
203 WARN(
204 "Name '{:s}' exists already. The object will be inserted "
205 "without a name",
206 *name);
207 }
208 }
209
211 virtual void setNameForElement(std::size_t id, std::string const& name)
212 {
213 // Erase id if found in map.
214 auto it = findFirstElementByID(id);
215 if (it != _name_id_map.end())
216 {
217 _name_id_map.erase(it);
218 }
219
220 if (!name.empty())
221 {
222 // insert new or revised name
223 _name_id_map[name] = id;
224 }
225 }
226
227private:
228 NameIdMap::const_iterator findFirstElementByID(std::size_t const& id) const
229 {
230 return std::find_if(_name_id_map.begin(), _name_id_map.end(),
231 [id](NameIdPair const& elem)
232 { return elem.second == id; });
233 }
234
235protected:
236 TemplateVec(TemplateVec const&) = delete;
238 TemplateVec& operator=(TemplateVec const& rhs) = delete;
240
242 std::string _name;
243
247 std::vector<T*> _data_vec;
252};
253} // 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...
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:85
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:55
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:66
std::string getName() const
Definition TemplateVec.h:82
bool getElementIDByName(const std::string &name, std::size_t &id) const
std::size_t size() const
Definition TemplateVec.h:99
void setName(const std::string &n)
Definition TemplateVec.h:77
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:91