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 26 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.
NameIdMap::const_iterator getNameIDMapEnd () const
 Returns the end of the name id mapping structure.
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.
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.
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.
virtual void setNameForElement (std::size_t id, std::string const &name)
 Sets the given name for the element of the given ID.

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

◆ NameIdPair

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

Definition at line 29 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
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.
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.
Attention
TemplateVec will take the ownership of the vector and also its elements, i.e. delete its elements and delete the vector itself!

Definition at line 44 of file TemplateVec.h.

46 : _name(name),
49 {
50 }
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition TemplateVec.h:27
std::vector< T * > _data_vec

◆ ~TemplateVec()

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

destructor, deletes all data elements

Definition at line 55 of file TemplateVec.h.

56 {
57 for (std::size_t k(0); k < size(); k++)
58 {
59 delete _data_vec[k];
60 }
61 }
std::size_t size() const
Definition TemplateVec.h:88

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

218 {
219 return std::find_if(_name_id_map.begin(), _name_id_map.end(),
220 [id](NameIdPair const& elem)
221 { return elem.second == id; });
222 }
std::pair< std::string, std::size_t > NameIdPair
Definition TemplateVec.h:29

Referenced by GeoLib::TemplateVec< GeoLib::Polyline >::getNameOfElementByID(), and GeoLib::TemplateVec< GeoLib::Polyline >::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 115 of file TemplateVec.h.

116 {
118 bool ret(getElementIDByName(name, id));
119 if (ret)
120 {
121 return _data_vec[id];
122 }
123
124 return nullptr;
125 }
bool getElementIDByName(const std::string &name, std::size_t &id) const

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

103 {
104 auto it(_name_id_map.find(name));
105
106 if (it != _name_id_map.end())
107 {
108 id = it->second;
109 return true;
110 }
111 return false;
112 }

Referenced by GeoLib::TemplateVec< GeoLib::Polyline >::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 71 of file TemplateVec.h.

71{ return _name; }

Referenced by GeoLib::GEOObjects::getStationVec().

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

75 {
76 return _name_id_map.cbegin();
77 }

Referenced by GeoTreeModel::addChildren(), GeoTreeModel::addChildren(), GeoTreeModel::addPointList(), 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 80 of file TemplateVec.h.

81 {
82 return _name_id_map.cend();
83 }

Referenced by GeoTreeModel::addChildren(), GeoTreeModel::addChildren(), GeoTreeModel::addPointList(), 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 162 of file TemplateVec.h.

163 {
164 for (std::size_t k(0); k < _data_vec.size(); k++)
165 {
166 if (_data_vec[k] == data)
167 {
168 return getNameOfElementByID(k, name);
169 }
170 }
171
172 return false;
173 }
bool getNameOfElementByID(std::size_t id, std::string &element_name) const

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

137 {
138 // search in map for id
139 auto it = findFirstElementByID(id);
140 if (it == _name_id_map.end())
141 {
142 return false;
143 }
144 element_name = it->first;
145 return true;
146 }
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const

Referenced by LineEditDialog::LineEditDialog(), MeshGeoToolsLib::appendLinesAlongPolylines(), GeoLib::GEOObjects::getElementNameByID(), GeoLib::TemplateVec< GeoLib::Polyline >::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 176 of file TemplateVec.h.

178 {
179 _data_vec.push_back(data_element);
180 if (!name || name->empty())
181 {
182 return;
183 }
184
185 auto it(_name_id_map.find(*name));
186 if (it == _name_id_map.end())
187 {
188 _name_id_map[*name] = _data_vec.size() - 1;
189 }
190 else
191 {
192 WARN(
193 "Name '{:s}' exists already. The object will be inserted "
194 "without a name",
195 *name);
196 }
197 }
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34

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

66{ _name = n; }

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

201 {
202 // Erase id if found in map.
203 auto it = findFirstElementByID(id);
204 if (it != _name_id_map.end())
205 {
206 _name_id_map.erase(it);
207 }
208
209 if (!name.empty())
210 {
211 // insert new or revised name
213 }
214 }

Referenced by 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 149 of file TemplateVec.h.

150 {
152 }

Referenced by GEOModels::connectPolylineSegments().

◆ size()

Member Data Documentation

◆ _data_vec

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

vector of data elements

Definition at line 236 of file TemplateVec.h.

◆ _name

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

the name of the object

Definition at line 231 of file TemplateVec.h.

◆ _name_id_map

template<class T>
NameIdMap GeoLib::TemplateVec< T >::_name_id_map
protected

store names associated with the element ids

Definition at line 240 of file TemplateVec.h.


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