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> class TemplateVec
40 {
41 protected:
42  using NameIdPair = std::pair<std::string, std::size_t>;
43  using NameIdMap = std::map<std::string, std::size_t>;
44 
45 public:
59  TemplateVec(std::string name, std::unique_ptr<std::vector<T*>> data_vec,
60  std::unique_ptr<NameIdMap> elem_name_map = nullptr)
61  : _name(std::move(name)),
62  _data_vec(std::move(data_vec)),
63  _name_id_map(std::move(elem_name_map))
64  {
65  if (_data_vec == nullptr)
66  {
67  OGS_FATAL("Constructor TemplateVec: vector of data elements is a nullptr.");
68  }
69 
70  if (!_name_id_map)
71  {
72  _name_id_map = std::make_unique<NameIdMap>();
73  }
74  }
75 
79  virtual ~TemplateVec ()
80  {
81  for (std::size_t k(0); k < size(); k++)
82  {
83  delete (*_data_vec)[k];
84  }
85  }
86 
90  void setName (const std::string & n) { _name = n; }
95  std::string getName () const { return _name; }
96 
98  NameIdMap::const_iterator getNameIDMapBegin() const { return _name_id_map->cbegin(); }
99 
101  NameIdMap::const_iterator getNameIDMapEnd() const { return _name_id_map->cend(); }
102 
106  std::size_t size () const { return _data_vec->size(); }
107 
112  const std::vector<T*>* getVector () const { return _data_vec.get(); }
113 
119  bool getElementIDByName (const std::string& name, std::size_t &id) const
120  {
121  auto it (_name_id_map->find (name));
122 
123  if (it != _name_id_map->end())
124  {
125  id = it->second;
126  return true;
127  }
128  return false;
129  }
130 
132  const T* getElementByName (const std::string& name) const
133  {
134  std::size_t id;
135  bool ret (getElementIDByName (name, id));
136  if (ret)
137  {
138  return (*_data_vec)[id];
139  }
140 
141  return nullptr;
142  }
143 
153  bool getNameOfElementByID (std::size_t id, std::string& element_name) const
154  {
155  // search in map for id
156  auto it = findFirstElementByID(id);
157  if (it == _name_id_map->end()) {
158  return false;
159  }
160  element_name = it->first;
161  return true;
162  }
163 
165  void setNameOfElementByID (std::size_t id, std::string const& element_name)
166  {
167  _name_id_map->insert(NameIdPair(element_name, id));
168  }
169 
178  bool getNameOfElement (const T* data, std::string& name) const
179  {
180  for (std::size_t k(0); k < _data_vec->size(); k++)
181  {
182  if ((*_data_vec)[k] == data)
183  {
184  return getNameOfElementByID(k, name);
185  }
186  }
187 
188  return false;
189  }
190 
192  virtual void push_back (T* data_element, 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  std::map<std::string, std::size_t>::const_iterator it(
201  _name_id_map->find(*name)
202  );
203  if (it == _name_id_map->end()) {
204  _name_id_map->insert(NameIdPair(*name, _data_vec->size() - 1));
205  } else {
206  WARN(
207  "Name '{:s}' exists already. The object will be inserted "
208  "without a name",
209  name->c_str());
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  //insert new or revised name
225  _name_id_map->insert(NameIdPair(name, id));
226  }
227  }
228 
229 private:
230 
231  NameIdMap::const_iterator
232  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) { return elem.second == id; });
236  }
237 
238 protected:
240  // compiler does not create a (possible unwanted) copy constructor
243  // this way the compiler does not create a (possible unwanted) assignment operator
245 
247  std::string _name;
248 
252  std::unique_ptr<std::vector <T*>> _data_vec;
256  std::unique_ptr<NameIdMap> _name_id_map;
257 };
258 } // end namespace GeoLib
#define OGS_FATAL(...)
Definition: Error.h:26
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:40
std::unique_ptr< NameIdMap > _name_id_map
Definition: TemplateVec.h:256
std::map< std::string, std::size_t > NameIdMap
Definition: TemplateVec.h:43
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
Definition: TemplateVec.h:165
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:42
const std::vector< T * > * getVector() const
Definition: TemplateVec.h:112
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition: TemplateVec.h:98
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
Definition: TemplateVec.h:192
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:153
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:232
virtual ~TemplateVec()
Definition: TemplateVec.h:79
std::string getName() const
Definition: TemplateVec.h:95
TemplateVec(const TemplateVec &)
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:119
std::size_t size() const
Definition: TemplateVec.h:106
TemplateVec & operator=(const TemplateVec &rhs)
void setName(const std::string &n)
Definition: TemplateVec.h:90
bool getNameOfElement(const T *data, std::string &name) const
Definition: TemplateVec.h:178
TemplateVec(std::string name, std::unique_ptr< std::vector< T * >> data_vec, std::unique_ptr< NameIdMap > elem_name_map=nullptr)
Definition: TemplateVec.h:59
std::unique_ptr< std::vector< T * > > _data_vec
Definition: TemplateVec.h:252
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:132
std::string _name
Definition: TemplateVec.h:247
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition: TemplateVec.h:101