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

◆ NameIdPair

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

Definition at line 40 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 56 of file TemplateVec.h.

58 : _name(name),
59 _data_vec(std::move(data_vec)),
60 _name_id_map(std::move(elem_name_map))
61 {
62 }
std::vector< T * > _data_vec

◆ ~TemplateVec()

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

destructor, deletes all data elements

Definition at line 67 of file TemplateVec.h.

68 {
69 for (std::size_t k(0); k < size(); k++)
70 {
71 delete _data_vec[k];
72 }
73 }
std::size_t size() const

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

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

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

128 {
129 std::size_t id;
130 bool ret(getElementIDByName(name, id));
131 if (ret)
132 {
133 return _data_vec[id];
134 }
135
136 return nullptr;
137 }
bool getElementIDByName(const std::string &name, std::size_t &id) const

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

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

115 {
116 auto it(_name_id_map.find(name));
117
118 if (it != _name_id_map.end())
119 {
120 id = it->second;
121 return true;
122 }
123 return false;
124 }

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

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

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

87 {
88 return _name_id_map.cbegin();
89 }

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

93 {
94 return _name_id_map.cend();
95 }

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

175 {
176 for (std::size_t k(0); k < _data_vec.size(); k++)
177 {
178 if (_data_vec[k] == data)
179 {
180 return getNameOfElementByID(k, name);
181 }
182 }
183
184 return false;
185 }
bool getNameOfElementByID(std::size_t id, std::string &element_name) const

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

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

149 {
150 // search in map for id
151 auto it = findFirstElementByID(id);
152 if (it == _name_id_map.end())
153 {
154 return false;
155 }
156 element_name = it->first;
157 return true;
158 }
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const

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

190 {
191 _data_vec.push_back(data_element);
192 if (!name || name->empty())
193 {
194 return;
195 }
196
197 auto it(_name_id_map.find(*name));
198 if (it == _name_id_map.end())
199 {
200 _name_id_map[*name] = _data_vec.size() - 1;
201 }
202 else
203 {
204 WARN(
205 "Name '{:s}' exists already. The object will be inserted "
206 "without a name",
207 *name);
208 }
209 }
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References GeoLib::TemplateVec< T >::_data_vec, GeoLib::TemplateVec< T >::_name_id_map, 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 78 of file TemplateVec.h.

78{ _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 212 of file TemplateVec.h.

213 {
214 // Erase id if found in map.
215 auto it = findFirstElementByID(id);
216 if (it != _name_id_map.end())
217 {
218 _name_id_map.erase(it);
219 }
220
221 if (!name.empty())
222 {
223 // insert new or revised name
224 _name_id_map[name] = id;
225 }
226 }

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

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

162 {
163 _name_id_map[element_name] = id;
164 }

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 243 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: