votca 2024-dev
Loading...
Searching...
No Matches
property.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef VOTCA_TOOLS_PROPERTY_H
19#define VOTCA_TOOLS_PROPERTY_H
20
21// Standard includes
22#include <cstdlib>
23#include <exception>
24#include <iostream>
25#include <list>
26#include <map>
27#include <stdexcept>
28#include <string>
29
30// Third party includes
31#include <boost/algorithm/string/trim.hpp>
32#include <boost/format.hpp>
33#include <utility>
34
35// Local VOTCA includes
36#include "eigen.h"
37#include "lexical_cast.h"
38#include "tokenizer.h"
39#include "types.h"
40
41namespace votca {
42namespace tools {
43
55class Property {
56
58 friend std::ostream &operator<<(std::ostream &out, const Property &p);
59
60 public:
61 Property() = default;
62 Property(const std::string &name, const std::string &value,
63 const std::string &path)
65
72 Property &add(const std::string &key, const std::string &value);
73
78 Property &add(const Property &other);
79
89 Property &addTree(const std::string &key, const std::string &value);
90 Property &addTree(const std::vector<std::string> &key,
91 const std::string &value);
92
100 Property &set(const std::string &key, const std::string &value);
101
112 Property &get(const std::string &key);
113 const Property &get(const std::string &key) const;
114
125 Property &getOradd(const std::string &key);
126
132 bool exists(const std::string &key) const;
133
134 template <typename T>
135 T ifExistsReturnElseReturnDefault(const std::string &key,
136 T defaultvalue) const;
137
146 std::vector<Property *> Select(const std::string &filter);
147 std::vector<const Property *> Select(const std::string &filter) const;
148
153 std::string &value() { return value_; }
154 const std::string &value() const { return value_; }
159 std::string &name() { return name_; }
160 const std::string &name() const { return name_; }
167 std::string &path() { return path_; }
168 const std::string &path() const { return path_; }
175 template <typename T>
176 T as() const;
177
182 bool HasChildren() const { return !map_.empty(); }
183
185 using iterator = std::vector<Property>::iterator;
186 using const_iterator = std::vector<Property>::const_iterator;
188 iterator begin() { return properties_.begin(); }
189 const_iterator begin() const { return properties_.begin(); }
191 iterator end() { return properties_.end(); }
192 const_iterator end() const { return properties_.end(); }
194 Index size() const { return Index(properties_.size()); }
195
202 template <class cond>
203 void deleteChildren(cond condition);
204
211 template <typename T>
212 T getAttribute(const std::string &attribute) const;
216 template <typename T>
217 void setAttribute(const std::string &attribute, const T &value);
218
222 void deleteAttribute(const std::string &attribute);
226 bool hasAttributes() const { return attributes_.size() > 0; }
230 bool hasAttribute(const std::string &attribute) const;
232 typedef std::map<std::string, std::string>::iterator AttributeIterator;
233 typedef std::map<std::string, std::string>::const_iterator
238 AttributeIterator findAttribute(const std::string &attribute) {
239 return attributes_.find(attribute);
240 }
241 const_AttributeIterator findAttribute(const std::string &attribute) const {
242 return attributes_.find(attribute);
243 }
260 template <typename T>
262
263 template <typename T>
265
266 void LoadFromXML(std::string filename);
267
268 static Index getIOindex() { return IOindex; };
269
270 private:
271 std::map<std::string, std::vector<Index>> map_;
272 std::map<std::string, std::string> attributes_;
273 std::vector<Property> properties_;
274
275 std::string name_ = "";
276 std::string value_ = "";
277 std::string path_ = "";
278
279 static const Index IOindex;
280};
281
282template <typename T>
283inline T Property::as() const {
284 std::string trimmed = value_;
285 boost::trim(trimmed);
286 try {
287 return convertFromString<T>(trimmed);
288 } catch (std::runtime_error &e) {
289 throw std::runtime_error("Property with name '" + name() + "' in path '" +
290 path() + "' and value :" + e.what());
291 }
292}
293
294template <typename T>
296 std::map<std::string, std::string>::const_iterator it) const {
297 if (it != attributes_.end()) {
298 return convertFromString<T>(it->second);
299 } else {
300 std::stringstream s;
301 s << *this << std::endl;
302 throw std::runtime_error(s.str() + "attribute " + it->first +
303 " not found\n");
304 }
305}
306
307template <typename T>
308inline T Property::getAttribute(const std::string &attribute) const {
309 std::map<std::string, std::string>::const_iterator it =
310 attributes_.find(attribute);
311 return getAttribute<T>(it);
312}
313template <typename T>
314inline void Property::setAttribute(const std::string &attribute,
315 const T &value) {
316 attributes_[attribute] =
317 lexical_cast<std::string>(value, "wrong type to set attribute");
318}
319
320template <typename T>
321inline T Property::ifExistsReturnElseReturnDefault(const std::string &key,
322 T defaultvalue) const {
323 T result;
324 if (this->exists(key)) {
325 result = this->get(key).as<T>();
326 } else {
327 result = defaultvalue;
328 }
329 return result;
330}
331
332template <>
334 const std::string &key, std::string defaultvalue) const {
335 std::string result;
336 if (this->exists(key)) {
337 result = this->get(key).as<std::string>();
338 if (result.empty()) {
339 result = defaultvalue;
340 }
341 } else {
342 result = defaultvalue;
343 }
344 return result;
345}
346
347template <class cond>
348void Property::deleteChildren(cond condition) {
349
350 properties_.erase(
351 std::remove_if(properties_.begin(), properties_.end(), condition),
352 properties_.end());
353
354 // rebuild map_
355 map_.clear();
356 Index index = 0;
357 for (const auto &prop : properties_) {
358 map_[prop.name()].push_back(index);
359 index++;
360 }
361 return;
362}
363
364} // namespace tools
365} // namespace votca
366
367#endif // VOTCA_TOOLS_PROPERTY_H
class to manage program options with xml serialization functionality
Definition property.h:55
T getAttribute(AttributeIterator it)
return attribute as type
Property & add(const std::string &key, const std::string &value)
add a new property to structure
Definition property.cc:108
const_AttributeIterator lastAttribute() const
Definition property.h:253
std::map< std::string, std::vector< Index > > map_
Definition property.h:271
const_iterator begin() const
Definition property.h:189
void deleteChildren(cond condition)
deletes all children that fulfill a condition
Definition property.h:348
std::map< std::string, std::string >::const_iterator const_AttributeIterator
Definition property.h:234
friend std::ostream & operator<<(std::ostream &out, const Property &p)
outputs the property to the ostream
Definition property.cc:440
std::string & path()
full path of property (including parents)
Definition property.h:167
Property(const std::string &name, const std::string &value, const std::string &path)
Definition property.h:62
Property & get(const std::string &key)
get existing property
Definition property.cc:79
const std::string & value() const
Definition property.h:154
const_AttributeIterator firstAttribute() const
Definition property.h:248
AttributeIterator findAttribute(const std::string &attribute)
returns an iterator to an attribute
Definition property.h:238
const std::string & path() const
Definition property.h:168
void deleteAttribute(const std::string &attribute)
deletes an attribute
Definition property.cc:206
std::string & value()
reference to value of property
Definition property.h:153
iterator end()
end iterator for child properties
Definition property.h:191
bool exists(const std::string &key) const
check whether property exists
Definition property.cc:122
T as() const
return value as type
Definition property.h:283
bool hasAttribute(const std::string &attribute) const
return true if an attribute exists
Definition property.cc:118
bool hasAttributes() const
return true if a node has attributes
Definition property.h:226
const_iterator end() const
Definition property.h:192
Index size() const
number of child properties
Definition property.h:194
T getAttribute(const_AttributeIterator it) const
T ifExistsReturnElseReturnDefault(const std::string &key, T defaultvalue) const
Definition property.h:321
std::map< std::string, std::string >::iterator AttributeIterator
Definition property.h:232
static Index getIOindex()
Definition property.h:268
iterator begin()
iterator to first child property
Definition property.h:188
const std::string & name() const
Definition property.h:160
Property & getOradd(const std::string &key)
adds new or gets existing property
Definition property.cc:156
T getAttribute(const std::string &attribute) const
return attribute as type
Definition property.h:308
std::map< std::string, std::string > attributes_
Definition property.h:272
bool HasChildren() const
does the property have children?
Definition property.h:182
std::string & name()
name of property
Definition property.h:159
Property & addTree(const std::string &key, const std::string &value)
add a new property tree to structure
Definition property.cc:89
std::vector< Property > properties_
Definition property.h:273
AttributeIterator firstAttribute()
returns an iterator to the first attribute
Definition property.h:247
std::vector< Property * > Select(const std::string &filter)
select property based on a filter
Definition property.cc:185
const_AttributeIterator findAttribute(const std::string &attribute) const
Definition property.h:241
std::vector< Property >::iterator iterator
iterator to iterate over properties
Definition property.h:185
std::vector< Property >::const_iterator const_iterator
Definition property.h:186
void setAttribute(const std::string &attribute, const T &value)
set an attribute
Definition property.h:314
AttributeIterator lastAttribute()
returns an iterator to the last attribute
Definition property.h:252
void LoadFromXML(std::string filename)
Definition property.cc:238
static const Index IOindex
Definition property.h:279
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26