OGS
TemplateVec.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <algorithm>
7#include <cstdlib>
8#include <map>
9#include <memory>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "BaseLib/Error.h"
15#include "BaseLib/Logging.h"
16
17namespace GeoLib
18{
25template <class T>
27{
28public:
29 using NameIdPair = std::pair<std::string, std::size_t>;
30 using NameIdMap = std::map<std::string, std::size_t>;
31
44 TemplateVec(std::string const& name, std::vector<T*>&& data_vec,
45 NameIdMap&& elem_name_map)
46 : _name(name),
47 _data_vec(std::move(data_vec)),
48 _name_id_map(std::move(elem_name_map))
49 {
50 }
51
55 virtual ~TemplateVec()
56 {
57 for (std::size_t k(0); k < size(); k++)
58 {
59 delete _data_vec[k];
60 }
61 }
62
66 void setName(const std::string& n) { _name = n; }
71 std::string getName() const { return _name; }
72
74 NameIdMap::const_iterator getNameIDMapBegin() const
75 {
76 return _name_id_map.cbegin();
77 }
78
80 NameIdMap::const_iterator getNameIDMapEnd() const
81 {
82 return _name_id_map.cend();
83 }
84
88 std::size_t size() const { return _data_vec.size(); }
89
94 std::vector<T*> const& getVector() const { return _data_vec; }
95
102 bool getElementIDByName(const std::string& name, std::size_t& id) const
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 }
113
115 const T* getElementByName(const std::string& name) const
116 {
117 std::size_t id;
118 bool ret(getElementIDByName(name, id));
119 if (ret)
120 {
121 return _data_vec[id];
122 }
123
124 return nullptr;
125 }
126
136 bool getNameOfElementByID(std::size_t id, std::string& element_name) const
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 }
147
149 void setNameOfElementByID(std::size_t id, std::string const& element_name)
150 {
151 _name_id_map[element_name] = id;
152 }
153
162 bool getNameOfElement(const T* data, std::string& name) const
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 }
174
176 virtual void push_back(T* data_element,
177 std::string const* const name = nullptr)
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 }
198
200 virtual void setNameForElement(std::size_t id, std::string const& name)
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
212 _name_id_map[name] = id;
213 }
214 }
215
216private:
217 NameIdMap::const_iterator findFirstElementByID(std::size_t const& id) const
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 }
223
224protected:
225 TemplateVec(TemplateVec const&) = delete;
227 TemplateVec& operator=(TemplateVec const& rhs) = delete;
229
231 std::string _name;
232
236 std::vector<T*> _data_vec;
241};
242} // end namespace GeoLib
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:30
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
std::pair< std::string, std::size_t > NameIdPair
Definition TemplateVec.h:29
NameIdMap::const_iterator getNameIDMapBegin() const
Returns the begin of the name id mapping structure.
Definition TemplateVec.h:74
TemplateVec(TemplateVec &&)=delete
std::vector< GeoLib::Polyline * > _data_vec
virtual void push_back(T *data_element, std::string const *const name=nullptr)
Adds a new element to the vector.
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
TemplateVec(std::string const &name, std::vector< T * > &&data_vec, NameIdMap &&elem_name_map)
Definition TemplateVec.h:44
virtual void setNameForElement(std::size_t id, std::string const &name)
Sets the given name for the element of the given ID.
NameIdMap::const_iterator findFirstElementByID(std::size_t const &id) const
virtual ~TemplateVec()
Definition TemplateVec.h:55
std::string getName() const
Definition TemplateVec.h:71
bool getElementIDByName(const std::string &name, std::size_t &id) const
std::size_t size() const
Definition TemplateVec.h:88
void setName(const std::string &n)
Definition TemplateVec.h:66
TemplateVec & operator=(TemplateVec const &rhs)=delete
bool getNameOfElement(const T *data, std::string &name) const
TemplateVec & operator=(TemplateVec &&rhs)=delete
std::vector< T * > const & getVector() const
Definition TemplateVec.h:94
TemplateVec(TemplateVec const &)=delete
NameIdMap::const_iterator getNameIDMapEnd() const
Returns the end of the name id mapping structure.
Definition TemplateVec.h:80