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 40 of file TemplateVec.h.

#include <TemplateVec.h>

Public Types

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

Public Member Functions

 TemplateVec (std::string const &name, std::vector< T * > &&data_vec, NameIdMap &&elem_name_map)
 
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
 
std::vector< T * > const & 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 Member Functions

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

Protected Attributes

std::string _name
 
std::vector< T * > _data_vec
 
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>

Definition at line 44 of file TemplateVec.h.

◆ NameIdPair

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

Definition at line 43 of file TemplateVec.h.

Constructor & Destructor Documentation

◆ TemplateVec() [1/3]

template<class T >
GeoLib::TemplateVec< T >::TemplateVec ( std::string const &  name,
std::vector< T * > &&  data_vec,
NameIdMap &&  elem_name_map 
)
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(name),
62  _data_vec(std::move(data_vec)),
63  _name_id_map(std::move(elem_name_map))
64  {
65  }
NameIdMap _name_id_map
Definition: TemplateVec.h:255
std::vector< T * > _data_vec
Definition: TemplateVec.h:251
std::string _name
Definition: TemplateVec.h:246

◆ ~TemplateVec()

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

destructor, deletes all data elements

Definition at line 70 of file TemplateVec.h.

71  {
72  for (std::size_t k(0); k < size(); k++)
73  {
74  delete _data_vec[k];
75  }
76  }
std::size_t size() const
Definition: TemplateVec.h:103

References GeoLib::TemplateVec< T >::_data_vec, and GeoLib::TemplateVec< T >::size().

◆ TemplateVec() [2/3]

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

◆ TemplateVec() [3/3]

template<class T >
GeoLib::TemplateVec< T >::TemplateVec ( TemplateVec< T > &&  )
protecteddelete

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)
236  { return elem.second == id; });
237  }
std::pair< std::string, std::size_t > NameIdPair
Definition: TemplateVec.h:43

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 130 of file TemplateVec.h.

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

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 117 of file TemplateVec.h.

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  }

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 86 of file TemplateVec.h.

86 { 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 89 of file TemplateVec.h.

90  {
91  return _name_id_map.cbegin();
92  }

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 95 of file TemplateVec.h.

96  {
97  return _name_id_map.cend();
98  }

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 177 of file TemplateVec.h.

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

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

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

◆ 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 151 of file TemplateVec.h.

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  }
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=() [1/2]

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

◆ operator=() [2/2]

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

◆ 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 191 of file TemplateVec.h.

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  }
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 81 of file TemplateVec.h.

81 { _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 215 of file TemplateVec.h.

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  }

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

Referenced by GEOModels::addNameForElement(), 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 164 of file TemplateVec.h.

165  {
166  _name_id_map[element_name] = id;
167  }

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

Referenced by GEOModels::connectPolylineSegments().

◆ size()

Member Data Documentation

◆ _data_vec

◆ _name

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

the name of the object

Definition at line 246 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: