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/Logging.h"
26 
27 #include "BaseLib/Error.h"
28 
29 namespace GeoLib
30 {
39 template <class T>
41 {
42 public:
43  using NameIdPair = std::pair<std::string, std::size_t>;
44  using NameIdMap = std::map<std::string, std::size_t>;
45 
59  TemplateVec(std::string const& name, std::vector<T*>&& data_vec,
60  NameIdMap&& elem_name_map)
61  : _name(name),
62  _data_vec(std::move(data_vec)),
63  _name_id_map(std::move(elem_name_map))
64  {
65  }
66 
70  virtual ~TemplateVec()
71  {
72  for (std::size_t k(0); k < size(); k++)
73  {
74  delete _data_vec[k];
75  }
76  }
77 
81  void setName(const std::string& n) { _name = n; }
86  std::string getName() const { return _name; }
87 
89  NameIdMap::const_iterator getNameIDMapBegin() const
90  {
91  return _name_id_map.cbegin();
92  }
93 
95  NameIdMap::const_iterator getNameIDMapEnd() const
96  {
97  return _name_id_map.cend();
98  }
99 
103  std::size_t size() const { return _data_vec.size(); }
104 
109  std::vector<T*> const& getVector() const { return _data_vec; }
110 
117  bool getElementIDByName(const std::string& name, std::size_t& id) const
118  {
119  auto it(_name_id_map.find(name));
120 
121  if (it != _name_id_map.end())
122  {
123  id = it->second;
124  return true;
125  }
126  return false;
127  }
128 
130  const T* getElementByName(const std::string& name) const
131  {
132  std::size_t id;
133  bool ret(getElementIDByName(name, id));
134  if (ret)
135  {
136  return _data_vec[id];
137  }
138 
139  return nullptr;
140  }
141 
151  bool getNameOfElementByID(std::size_t id, std::string& element_name) const
152  {
153  // search in map for id
154  auto it = findFirstElementByID(id);
155  if (it == _name_id_map.end())
156  {
157  return false;
158  }
159  element_name = it->first;
160  return true;
161  }
162 
164  void setNameOfElementByID(std::size_t id, std::string const& element_name)
165  {
166  _name_id_map[element_name] = id;
167  }
168 
177  bool getNameOfElement(const T* data, std::string& name) const
178  {
179  for (std::size_t k(0); k < _data_vec.size(); k++)
180  {
181  if (_data_vec[k] == data)
182  {
183  return getNameOfElementByID(k, name);
184  }
185  }
186 
187  return false;
188  }
189 
191  virtual void push_back(T* data_element,
192  std::string const* const name = nullptr)
193  {
194  _data_vec.push_back(data_element);
195  if (!name || name->empty())
196  {
197  return;
198  }
199 
200  auto it(_name_id_map.find(*name));
201  if (it == _name_id_map.end())
202  {
203  _name_id_map[*name] = _data_vec.size() - 1;
204  }
205  else
206  {
207  WARN(
208  "Name '{:s}' exists already. The object will be inserted "
209  "without a name",
210  *name);
211  }
212  }
213 
215  virtual void setNameForElement(std::size_t id, std::string const& name)
216  {
217  // Erase id if found in map.
218  auto it = findFirstElementByID(id);
219  if (it != _name_id_map.end())
220  {
221  _name_id_map.erase(it);
222  }
223 
224  if (!name.empty())
225  {
226  // insert new or revised name
227  _name_id_map[name] = id;
228  }
229  }
230 
231 private:
232  NameIdMap::const_iterator findFirstElementByID(std::size_t const& id) const
233  {
234  return std::find_if(_name_id_map.begin(), _name_id_map.end(),
235  [id](NameIdPair const& elem)
236  { return elem.second == id; });
237  }
238 
239 protected:
240  TemplateVec(TemplateVec const&) = delete;
242  TemplateVec& operator=(TemplateVec const& rhs) = delete;
244 
246  std::string _name;
247 
251  std::vector<T*> _data_vec;
256 };
257 } // end namespace GeoLib
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition: TemplateVec.h:41
NameIdMap _name_id_map
Definition: TemplateVec.h:255
std::map< std::string, std::size_t > NameIdMap
Definition: TemplateVec.h:44
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
Definition: TemplateVec.h:164
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:43
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition: TemplateVec.h:89
TemplateVec & operator=(TemplateVec &&rhs)=delete
TemplateVec(TemplateVec &&)=delete
std::vector< T * > _data_vec
Definition: TemplateVec.h:251
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
Definition: TemplateVec.h:191
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:151
TemplateVec(std::string const &name, std::vector< T * > &&data_vec, NameIdMap &&elem_name_map)
Definition: TemplateVec.h:59
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:215
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
Definition: TemplateVec.h:232
std::vector< T * > const & getVector() const
Definition: TemplateVec.h:109
virtual ~TemplateVec()
Definition: TemplateVec.h:70
std::string getName() const
Definition: TemplateVec.h:86
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:117
std::size_t size() const
Definition: TemplateVec.h:103
void setName(const std::string &n)
Definition: TemplateVec.h:81
TemplateVec & operator=(TemplateVec const &rhs)=delete
bool getNameOfElement(const T *data, std::string &name) const
Definition: TemplateVec.h:177
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:130
TemplateVec(TemplateVec const &)=delete
std::string _name
Definition: TemplateVec.h:246
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition: TemplateVec.h:95