OGS
GeoLib::TemplateVec< T > Class Template Reference

Detailed Description

template<typename 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 17 of file AppendLinesAlongPolyline.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<typename T >
using GeoLib::TemplateVec< T >::NameIdMap = std::map<std::string, std::size_t>

Definition at line 41 of file TemplateVec.h.

◆ NameIdPair

template<typename 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<typename 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 55 of file TemplateVec.h.

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

◆ ~TemplateVec()

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

destructor, deletes all data elements

Definition at line 66 of file TemplateVec.h.

67 {
68 for (std::size_t k(0); k < size(); k++)
69 {
70 delete _data_vec[k];
71 }
72 }
std::size_t size() const
Definition TemplateVec.h:99

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

◆ TemplateVec() [2/3]

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

◆ TemplateVec() [3/3]

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

Member Function Documentation

◆ findFirstElementByID()

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

Definition at line 228 of file TemplateVec.h.

229 {
230 return std::find_if(_name_id_map.begin(), _name_id_map.end(),
231 [id](NameIdPair const& elem)
232 { return elem.second == id; });
233 }
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<typename T >
const T * GeoLib::TemplateVec< T >::getElementByName ( const std::string & name) const
inline

Returns an element with the given name.

Definition at line 126 of file TemplateVec.h.

127 {
128 std::size_t id;
129 bool ret(getElementIDByName(name, id));
130 if (ret)
131 {
132 return _data_vec[id];
133 }
134
135 return nullptr;
136 }
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<typename 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 113 of file TemplateVec.h.

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

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

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

◆ getName()

template<typename 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 82 of file TemplateVec.h.

82{ return _name; }

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

◆ getNameIDMapBegin()

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

Returns the begin of the name id mapping structure.

Definition at line 85 of file TemplateVec.h.

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

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

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

◆ getNameIDMapEnd()

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

Returns the end of the name id mapping structure.

Definition at line 91 of file TemplateVec.h.

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

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

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

◆ getNameOfElement()

template<typename 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 173 of file TemplateVec.h.

174 {
175 for (std::size_t k(0); k < _data_vec.size(); k++)
176 {
177 if (_data_vec[k] == data)
178 {
179 return getNameOfElementByID(k, name);
180 }
181 }
182
183 return false;
184 }
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<typename 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 147 of file TemplateVec.h.

148 {
149 // search in map for id
150 auto it = findFirstElementByID(id);
151 if (it == _name_id_map.end())
152 {
153 return false;
154 }
155 element_name = it->first;
156 return true;
157 }
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<typename T >
TemplateVec & GeoLib::TemplateVec< T >::operator= ( TemplateVec< T > && rhs)
protecteddelete

◆ operator=() [2/2]

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

◆ push_back()

template<typename 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 187 of file TemplateVec.h.

189 {
190 _data_vec.push_back(data_element);
191 if (!name || name->empty())
192 {
193 return;
194 }
195
196 auto it(_name_id_map.find(*name));
197 if (it == _name_id_map.end())
198 {
199 _name_id_map[*name] = _data_vec.size() - 1;
200 }
201 else
202 {
203 WARN(
204 "Name '{:s}' exists already. The object will be inserted "
205 "without a name",
206 *name);
207 }
208 }
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<typename 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 77 of file TemplateVec.h.

77{ _name = n; }

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

◆ setNameForElement()

template<typename 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 211 of file TemplateVec.h.

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

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

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

◆ setNameOfElementByID()

template<typename 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 160 of file TemplateVec.h.

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

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

Referenced by GEOModels::connectPolylineSegments().

◆ size()

Member Data Documentation

◆ _data_vec

◆ _name

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

the name of the object

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