OGS 6.2.0-97-g4a610c866
TemplateVec.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include <algorithm>
19 #include <cstdlib>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <logog/include/logog.hpp>
27 
28 #include "BaseLib/Error.h"
29 
30 namespace GeoLib
31 {
40 template <class T> class TemplateVec
41 {
42 protected:
43  using NameIdPair = std::pair<std::string, std::size_t>;
44  using NameIdMap = std::map<std::string, std::size_t>;
45 
46 public:
60  TemplateVec(std::string name, std::unique_ptr<std::vector<T*>> data_vec,
61  std::unique_ptr<NameIdMap> elem_name_map = nullptr)
62  : _name(std::move(name)),
63  _data_vec(std::move(data_vec)),
64  _name_id_map(std::move(elem_name_map))
65  {
66  if (_data_vec == nullptr)
67  {
68  OGS_FATAL("Constructor TemplateVec: vector of data elements is a nullptr.");
69  }
70 
71  if (!_name_id_map)
72  {
73  _name_id_map = std::make_unique<NameIdMap>();
74  }
75  }
76 
80  virtual ~TemplateVec ()
81  {
82  for (std::size_t k(0); k < size(); k++)
83  {
84  delete (*_data_vec)[k];
85  }
86  }
87 
91  void setName (const std::string & n) { _name = n; }
96  std::string getName () const { return _name; }
97 
99  NameIdMap::const_iterator getNameIDMapBegin() const { return _name_id_map->cbegin(); }
100 
102  NameIdMap::const_iterator getNameIDMapEnd() const { return _name_id_map->cend(); }
103 
107  std::size_t size () const { return _data_vec->size(); }
108 
113  const std::vector<T*>* getVector () const { return _data_vec.get(); }
114 
120  bool getElementIDByName (const std::string& name, std::size_t &id) const
121  {
122  auto it (_name_id_map->find (name));
123 
124  if (it != _name_id_map->end())
125  {
126  id = it->second;
127  return true;
128  }
129  return false;
130  }
131 
133  const T* getElementByName (const std::string& name) const
134  {
135  std::size_t id;
136  bool ret (getElementIDByName (name, id));
137  if (ret)
138  {
139  return (*_data_vec)[id];
140  }
141 
142  return nullptr;
143  }
144 
154  bool getNameOfElementByID (std::size_t id, std::string& element_name) const
155  {
156  // search in map for id
157  auto it = findFirstElementByID(id);
158  if (it == _name_id_map->end()) {
159  return false;
160  }
161  element_name = it->first;
162  return true;
163  }
164 
166  void setNameOfElementByID (std::size_t id, std::string const& element_name)
167  {
168  _name_id_map->insert(NameIdPair(element_name, id));
169  }
170 
179  bool getNameOfElement (const T* data, std::string& name) const
180  {
181  for (std::size_t k(0); k < _data_vec->size(); k++)
182  {
183  if ((*_data_vec)[k] == data)
184  {
185  return getNameOfElementByID(k, name);
186  }
187  }
188 
189  return false;
190  }
191 
193  virtual void push_back (T* data_element, std::string const* const name = nullptr)
194  {
195  _data_vec->push_back (data_element);
196  if (!name || name->empty())
197  {
198  return;
199  }
200 
201  std::map<std::string, std::size_t>::const_iterator it(
202  _name_id_map->find(*name)
203  );
204  if (it == _name_id_map->end()) {
205  _name_id_map->insert(NameIdPair(*name, _data_vec->size() - 1));
206  } else {
207  WARN(
208  "Name '%s' exists already. The object will be inserted "
209  "without a name",
210  name->c_str());
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  //insert new or revised name
226  _name_id_map->insert(NameIdPair(name, id));
227  }
228  }
229 
230 private:
231 
232  NameIdMap::const_iterator
233  findFirstElementByID(std::size_t const& id) const
234  {
235  return std::find_if(_name_id_map->begin(), _name_id_map->end(),
236  [id](NameIdPair const& elem) { return elem.second == id; });
237  }
238 
239 protected:
241  // compiler does not create a (possible unwanted) copy constructor
242  TemplateVec (const TemplateVec &);
244  // this way the compiler does not create a (possible unwanted) assignment operator
245  TemplateVec& operator= (const TemplateVec& rhs);
246 
248  std::string _name;
249 
253  std::unique_ptr<std::vector <T*>> _data_vec;
257  std::unique_ptr<NameIdMap> _name_id_map;
258 };
259 } // end namespace GeoLib
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
Definition: TemplateVec.h:233
TemplateVec(std::string name, std::unique_ptr< std::vector< T *>> data_vec, std::unique_ptr< NameIdMap > elem_name_map=nullptr)
Definition: TemplateVec.h:60
std::size_t size() const
Definition: TemplateVec.h:107
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:43
std::string getName() const
Definition: TemplateVec.h:96
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
Definition: TemplateVec.h:193
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition: TemplateVec.h:102
std::map< std::string, std::size_t > NameIdMap
Definition: TemplateVec.h:44
std::unique_ptr< std::vector< T * > > _data_vec
Definition: TemplateVec.h:253
Definition of the GEOObjects class.
Definition: BaseItem.h:20
virtual ~TemplateVec()
Definition: TemplateVec.h:80
void setName(const std::string &n)
Definition: TemplateVec.h:91
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:133
bool getNameOfElement(const T *data, std::string &name) const
Definition: TemplateVec.h:179
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition: TemplateVec.h:99
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
Definition: TemplateVec.h:166
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
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:154
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition: TemplateVec.h:40
const std::vector< T * > * getVector() const
Definition: TemplateVec.h:113
std::string _name
Definition: TemplateVec.h:248
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
TemplateVec & operator=(const TemplateVec &rhs)
std::unique_ptr< NameIdMap > _name_id_map
Definition: TemplateVec.h:257
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:120