OGS
GeoLib::TemplateVec< T > Class Template Reference

Detailed Description

template<class T>
class GeoLib::TemplateVec< T >

The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of type T.

Instances are classes PolylineVec and SurfaceVec.

Definition at line 39 of file TemplateVec.h.

#include <TemplateVec.h>

Public Member Functions

 TemplateVec (std::string name, std::unique_ptr< std::vector< T * >> data_vec, std::unique_ptr< NameIdMap > elem_name_map=nullptr)
 
virtual ~TemplateVec ()
 
void setName (const std::string &n)
 
std::string getName () const
 
NameIdMap::const_iterator getNameIDMapBegin () const
 Returns the begin of the name id mapping structure. More...
 
NameIdMap::const_iterator getNameIDMapEnd () const
 Returns the end of the name id mapping structure. More...
 
std::size_t size () const
 
const std::vector< T * > * getVector () const
 
bool getElementIDByName (const std::string &name, std::size_t &id) const
 
const T * getElementByName (const std::string &name) const
 Returns an element with the given name. More...
 
bool getNameOfElementByID (std::size_t id, std::string &element_name) const
 
void setNameOfElementByID (std::size_t id, std::string const &element_name)
 Return the name of an element based on its ID. More...
 
bool getNameOfElement (const T *data, std::string &name) const
 
virtual void push_back (T *data_element, std::string const *const name=nullptr)
 Adds a new element to the vector. More...
 
virtual void setNameForElement (std::size_t id, std::string const &name)
 Sets the given name for the element of the given ID. More...
 

Protected Types

using NameIdPair = std::pair< std::string, std::size_t >
 
using NameIdMap = std::map< std::string, std::size_t >
 

Protected Member Functions

 TemplateVec (const TemplateVec &)
 
TemplateVecoperator= (const TemplateVec &rhs)
 

Protected Attributes

std::string _name
 
std::unique_ptr< std::vector< T * > > _data_vec
 
std::unique_ptr< NameIdMap_name_id_map
 

Private Member Functions

NameIdMap::const_iterator findFirstElementByID (std::size_t const &id) const
 

Member Typedef Documentation

◆ NameIdMap

template<class T >
using GeoLib::TemplateVec< T >::NameIdMap = std::map<std::string, std::size_t>
protected

Definition at line 43 of file TemplateVec.h.

◆ NameIdPair

template<class T >
using GeoLib::TemplateVec< T >::NameIdPair = std::pair<std::string, std::size_t>
protected

Definition at line 42 of file TemplateVec.h.

Constructor & Destructor Documentation

◆ TemplateVec() [1/2]

template<class T >
GeoLib::TemplateVec< T >::TemplateVec ( std::string  name,
std::unique_ptr< std::vector< T * >>  data_vec,
std::unique_ptr< NameIdMap elem_name_map = nullptr 
)
inline

Constructor of class TemlateVec.

Parameters
nameunique name of the project the elements belonging to. In order to access the data elements a unique name is required.
data_vecVector of data elements.
Attention
{TemplateVec will take the ownership of the vector and also its elements, i.e. delete its elements and delete the vector itself!}
Parameters
elem_name_mapNames of data elements can be given by a std::map<std::string, std::size_t>. Here the std::string is the name of the element and the value for std::size_t stands for an index in the data_vec.

Definition at line 59 of file TemplateVec.h.

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  }
#define OGS_FATAL(...)
Definition: Error.h:26
std::unique_ptr< NameIdMap > _name_id_map
Definition: TemplateVec.h:256
std::unique_ptr< std::vector< T * > > _data_vec
Definition: TemplateVec.h:252
std::string _name
Definition: TemplateVec.h:247

References GeoLib::TemplateVec< T >::_data_vec, GeoLib::TemplateVec< T >::_name_id_map, and OGS_FATAL.

◆ ~TemplateVec()

template<class T >
virtual GeoLib::TemplateVec< T >::~TemplateVec ( )
inlinevirtual

destructor, deletes all data elements

Definition at line 79 of file TemplateVec.h.

80  {
81  for (std::size_t k(0); k < size(); k++)
82  {
83  delete (*_data_vec)[k];
84  }
85  }
std::size_t size() const
Definition: TemplateVec.h:106

References GeoLib::TemplateVec< T >::size().

◆ TemplateVec() [2/2]

template<class T >
GeoLib::TemplateVec< T >::TemplateVec ( const TemplateVec< T > &  )
protected

copy constructor doesn't have an implementation

Member Function Documentation

◆ findFirstElementByID()

template<class T >
NameIdMap::const_iterator GeoLib::TemplateVec< T >::findFirstElementByID ( std::size_t const &  id) const
inlineprivate

Definition at line 232 of file TemplateVec.h.

233  {
234  return std::find_if(_name_id_map->begin(), _name_id_map->end(),
235  [id](NameIdPair const& elem) { return elem.second == id; });
236  }
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:42

References GeoLib::TemplateVec< T >::_name_id_map.

Referenced by GeoLib::TemplateVec< T >::getNameOfElementByID(), and GeoLib::TemplateVec< T >::setNameForElement().

◆ getElementByName()

template<class T >
const T* GeoLib::TemplateVec< T >::getElementByName ( const std::string &  name) const
inline

Returns an element with the given name.

Definition at line 132 of file TemplateVec.h.

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  }
bool getElementIDByName(const std::string &name, std::size_t &id) const
Definition: TemplateVec.h:119

References GeoLib::TemplateVec< T >::_data_vec, GeoLib::TemplateVec< T >::getElementIDByName(), and MaterialPropertyLib::name.

Referenced by GeoLib::GEOObjects::getGeoObject(), and main().

◆ getElementIDByName()

template<class T >
bool GeoLib::TemplateVec< T >::getElementIDByName ( const std::string &  name,
std::size_t &  id 
) const
inline

search the vector of names for the ID of the geometric element with the given name

Parameters
namethe name of the geometric element
idthe id of the geometric element

Definition at line 119 of file TemplateVec.h.

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  }

References GeoLib::TemplateVec< T >::_name_id_map, and MaterialPropertyLib::name.

Referenced by GeoLib::TemplateVec< T >::getElementByName().

◆ getName()

template<class T >
std::string GeoLib::TemplateVec< T >::getName ( ) const
inline

the name, the data element belonging to

Returns
the name of the object

Definition at line 95 of file TemplateVec.h.

95 { return _name; }

References GeoLib::TemplateVec< T >::_name.

◆ getNameIDMapBegin()

template<class T >
NameIdMap::const_iterator GeoLib::TemplateVec< T >::getNameIDMapBegin ( ) const
inline

Returns the begin of the name id mapping structure.

Definition at line 98 of file TemplateVec.h.

98 { return _name_id_map->cbegin(); }

References GeoLib::TemplateVec< T >::_name_id_map.

Referenced by GeoTreeModel::addChildren(), GeoTreeModel::addPointList(), GeoLib::DuplicateGeometry::duplicate(), and FileIO::Legacy::readGLIFileV4().

◆ getNameIDMapEnd()

template<class T >
NameIdMap::const_iterator GeoLib::TemplateVec< T >::getNameIDMapEnd ( ) const
inline

Returns the end of the name id mapping structure.

Definition at line 101 of file TemplateVec.h.

101 { return _name_id_map->cend(); }

References GeoLib::TemplateVec< T >::_name_id_map.

Referenced by GeoTreeModel::addChildren(), GeoTreeModel::addPointList(), GeoLib::DuplicateGeometry::duplicate(), and FileIO::Legacy::readGLIFileV4().

◆ getNameOfElement()

template<class T >
bool GeoLib::TemplateVec< T >::getNameOfElement ( const T *  data,
std::string &  name 
) const
inline

The method returns true if the given element of type T can be found and the element has a name, else method returns false.

Parameters
datathe data element, one wants to know the name
namethe name of the data element (if the data element is found and the data element has a name)
Returns
if element is found and has a name: true, else false

Definition at line 178 of file TemplateVec.h.

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  }
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:153

References GeoLib::TemplateVec< T >::_data_vec, GeoLib::TemplateVec< T >::getNameOfElementByID(), and MaterialPropertyLib::name.

Referenced by GeoLib::IO::BoostXmlGmlInterface::addPolylinesToPropertyTree(), GeoLib::IO::BoostXmlGmlInterface::addSurfacesToPropertyTree(), main(), and FileIO::Legacy::writeGLIFileV4().

◆ getNameOfElementByID()

template<class T >
bool GeoLib::TemplateVec< T >::getNameOfElementByID ( std::size_t  id,
std::string &  element_name 
) const
inline

The method returns true if there is a name associated with the given id, else method returns false.

Parameters
idthe id
element_nameif a name associated with the id is found name is assigned to element_name
Returns
if there is name associated with the given id: true, else false

Definition at line 153 of file TemplateVec.h.

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  }
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
Definition: TemplateVec.h:232

References GeoLib::TemplateVec< T >::_name_id_map, and GeoLib::TemplateVec< T >::findFirstElementByID().

Referenced by LineEditDialog::LineEditDialog(), MeshGeoToolsLib::appendLinesAlongPolylines(), GeoLib::GEOObjects::getElementNameByID(), GeoLib::TemplateVec< T >::getNameOfElement(), main(), GeoLib::IO::XmlGmlInterface::write(), FileIO::Legacy::writeAllDataToGLIFileV4(), and FileIO::Legacy::writeTINSurfaces().

◆ getVector()

◆ operator=()

template<class T >
TemplateVec& GeoLib::TemplateVec< T >::operator= ( const TemplateVec< T > &  rhs)
protected

assignment operator doesn't have an implementation

◆ push_back()

template<class T >
virtual void GeoLib::TemplateVec< T >::push_back ( T *  data_element,
std::string const *const  name = nullptr 
)
inlinevirtual

Adds a new element to the vector.

Reimplemented in GeoLib::PointVec.

Definition at line 192 of file TemplateVec.h.

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  }
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37

References GeoLib::TemplateVec< T >::_data_vec, GeoLib::TemplateVec< T >::_name_id_map, MaterialPropertyLib::name, and WARN().

◆ setName()

template<class T >
void GeoLib::TemplateVec< T >::setName ( const std::string &  n)
inline

sets the name of the vector of geometric objects the data elements belonging to

Parameters
nthe name as standard string

Definition at line 90 of file TemplateVec.h.

90 { _name = n; }

References GeoLib::TemplateVec< T >::_name.

◆ setNameForElement()

template<class T >
virtual void GeoLib::TemplateVec< T >::setNameForElement ( std::size_t  id,
std::string const &  name 
)
inlinevirtual

Sets the given name for the element of the given ID.

Reimplemented in GeoLib::PointVec.

Definition at line 214 of file TemplateVec.h.

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  }

References GeoLib::TemplateVec< T >::_name_id_map, GeoLib::TemplateVec< T >::findFirstElementByID(), and MaterialPropertyLib::name.

Referenced by GEOModels::addNameForElement(), main(), and GeoLib::PointVec::setNameForElement().

◆ setNameOfElementByID()

template<class T >
void GeoLib::TemplateVec< T >::setNameOfElementByID ( std::size_t  id,
std::string const &  element_name 
)
inline

Return the name of an element based on its ID.

Definition at line 165 of file TemplateVec.h.

166  {
167  _name_id_map->insert(NameIdPair(element_name, id));
168  }

References GeoLib::TemplateVec< T >::_name_id_map.

Referenced by GEOModels::connectPolylineSegments().

◆ size()

Member Data Documentation

◆ _data_vec

template<class T >
std::unique_ptr<std::vector <T*> > GeoLib::TemplateVec< T >::_data_vec
protected

◆ _name

template<class T >
std::string GeoLib::TemplateVec< T >::_name
protected

the name of the object

Definition at line 247 of file TemplateVec.h.

Referenced by GeoLib::TemplateVec< T >::getName(), and GeoLib::TemplateVec< T >::setName().

◆ _name_id_map


The documentation for this class was generated from the following file: