OGS 6.1.0-1699-ge946d4c5f
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  _name_id_map = std::make_unique<NameIdMap>();
73  }
74 
78  virtual ~TemplateVec ()
79  {
80  for (std::size_t k(0); k < size(); k++) delete (*_data_vec)[k];
81  }
82 
86  void setName (const std::string & n) { _name = n; }
91  std::string getName () const { return _name; }
92 
94  NameIdMap::const_iterator getNameIDMapBegin() const { return _name_id_map->cbegin(); }
95 
97  NameIdMap::const_iterator getNameIDMapEnd() const { return _name_id_map->cend(); }
98 
102  std::size_t size () const { return _data_vec->size(); }
103 
108  const std::vector<T*>* getVector () const { return _data_vec.get(); }
109 
115  bool getElementIDByName (const std::string& name, std::size_t &id) const
116  {
117  auto it (_name_id_map->find (name));
118 
119  if (it != _name_id_map->end())
120  {
121  id = it->second;
122  return true;
123  }
124  return false;
125  }
126 
128  const T* getElementByName (const std::string& name) const
129  {
130  std::size_t id;
131  bool ret (getElementIDByName (name, id));
132  if (ret)
133  return (*_data_vec)[id];
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  return false;
153  }
154  element_name = it->first;
155  return true;
156  }
157 
159  void setNameOfElementByID (std::size_t id, std::string const& element_name)
160  {
161  _name_id_map->insert(NameIdPair(element_name, id));
162  }
163 
172  bool getNameOfElement (const T* data, std::string& name) const
173  {
174  for (std::size_t k(0); k < _data_vec->size(); k++)
175  if ((*_data_vec)[k] == data)
176  return getNameOfElementByID (k, name);
177 
178  return false;
179  }
180 
182  virtual void push_back (T* data_element, std::string const* const name = nullptr)
183  {
184  _data_vec->push_back (data_element);
185  if (!name || name->empty())
186  return;
187 
188  std::map<std::string, std::size_t>::const_iterator it(
189  _name_id_map->find(*name)
190  );
191  if (it == _name_id_map->end()) {
192  _name_id_map->insert(NameIdPair(*name, _data_vec->size() - 1));
193  } else {
194  WARN(
195  "Name '%s' exists already. The object will be inserted "
196  "without a name",
197  name->c_str());
198  }
199  }
200 
202  virtual void setNameForElement(std::size_t id, std::string const& name)
203  {
204  // Erase id if found in map.
205  auto it = findFirstElementByID(id);
206  if (it != _name_id_map->end())
207  _name_id_map->erase(it);
208 
209  if (!name.empty()) {
210  //insert new or revised name
211  _name_id_map->insert(NameIdPair(name, id));
212  }
213  }
214 
215 private:
216 
217  NameIdMap::const_iterator
218  findFirstElementByID(std::size_t const& id) const
219  {
220  return std::find_if(_name_id_map->begin(), _name_id_map->end(),
221  [id](NameIdPair const& elem) { return elem.second == id; });
222  }
223 
224 protected:
226  // compiler does not create a (possible unwanted) copy constructor
227  TemplateVec (const TemplateVec &);
229  // this way the compiler does not create a (possible unwanted) assignment operator
230  TemplateVec& operator= (const TemplateVec& rhs);
231 
233  std::string _name;
234 
238  std::unique_ptr<std::vector <T*>> _data_vec;
242  std::unique_ptr<NameIdMap> _name_id_map;
243 };
244 } // end namespace GeoLib
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
Definition: TemplateVec.h:218
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:102
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:43
std::string getName() const
Definition: TemplateVec.h:91
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
Definition: TemplateVec.h:182
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition: TemplateVec.h:97
std::map< std::string, std::size_t > NameIdMap
Definition: TemplateVec.h:44
std::unique_ptr< std::vector< T * > > _data_vec
Definition: TemplateVec.h:238
Definition of the GEOObjects class.
Definition: BaseItem.h:20
virtual ~TemplateVec()
Definition: TemplateVec.h:78
void setName(const std::string &n)
Definition: TemplateVec.h:86
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:128
bool getNameOfElement(const T *data, std::string &name) const
Definition: TemplateVec.h:172
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition: TemplateVec.h:94
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
Definition: TemplateVec.h:159
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:202
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:147
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:108
std::string _name
Definition: TemplateVec.h:233
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
TemplateVec & operator=(const TemplateVec &rhs)
std::unique_ptr< NameIdMap > _name_id_map
Definition: TemplateVec.h:242
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:115