/usr/include/votca/tools/property.h is in libvotca-tools-dev 1.3.0-2+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | /*
* Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _VOTCA_TOOLS_PROPERTY_H
#define _VOTCA_TOOLS_PROPERTY_H
#include <map>
#include <string>
#include <iostream>
#include <list>
#include <stdexcept>
#include "lexical_cast.h"
#include <boost/algorithm/string/trim.hpp>
#include <stdlib.h>
#include "vec.h"
#include "tokenizer.h"
namespace votca { namespace tools {
/**
* \brief class to manage program options with xml serialization functionality
*
* This class stores tags and content in a hierarchical (tree) structure similar
* to the one used in the XML format. The structure can be either filled manually
* or read in from an XML file using load_property_from_xml.
* The supported XML constructs are TAGS, ATTRIBUTES, and CONTENT:
* <tag attribute_name="attribute_value">
* content
* </tag>
* The property object can be output to an ostream using format modifiers:
* cout << XML << property;
* Supported formats are XML, TXT, TEX, HLP
*/
class Property {
/// \brief outputs the property to the ostream
friend std::ostream &operator<<(std::ostream &out, Property& p);
public:
Property() : _path("") {}
Property(const string &name, const string &value, const string &path)
: _name(name), _value(value), _path(path) {}
/**
* \brief add a new property to structure
* @param key identifier
* @param value value
* @return reference to the created Property object
*/
Property &add(const string &key, const string &value);
/**
* \brief set value of existing property
* @param key identifier
* @param value value
* @return reference to the created Property object
*/
Property &set(const string &key, const string &value);
/**
* \brief get existing property
* @param key identifier
* @return Reference to property object
*
* This function tries to find a property specified by key separated
* by "." to step down hierarchy. If the property is not
* found a runtime_exception is thrown.
*/
Property &get(const string &key);
/**
* \brief check weather property exists
* @param key identifier
* @return true or false
*/
bool exists(const string &key);
/**
* \brief select property based on a filter
* @param filter
* @return list of pointers to property objects
*
* returns a list of properties that match the key criteria including
* wildcards "*" and "?". Example: "base.item*.value"
*/
std::list<Property *> Select(const string &filter);
/**
* \brief reference to value of property
* @return string content
*/
string &value() { return _value; }
/**
* \brief name of property
* @return name
*/
string name() { return _name; }
/**
* \brief full path of property (including parents)
* @return path
*
* e.g. cg.inverse.value
*/
string path() { return _path; }
/**
* \brief return value as type
*
* returns the value after type conversion, e.g.
* p.as<int>() returns an integer
*/
template<typename T>
T as() const;
/**
* \brief does the property has childs?
* \return true or false
*/
bool HasChilds() { return !_map.empty(); }
/// iterator to iterate over properties
typedef list<Property>::iterator iterator;
/// \brief iterator to first child property
iterator begin() { return _properties.begin(); }
/// \brief end iterator for child properties
iterator end() { return _properties.end(); }
/// \brief number of child properties
list<Property>::size_type size() { return _properties.size(); }
// throw error and comment (with filename+code line)
void throwRuntimeError(string message);
/**
* \brief return attribute as type
*
* returns an attribute after type conversion, e.g.
* p.getAttribute<int>() returns an integer
*/
template<typename T>
T getAttribute(const string &attribute);
/**
* \brief set an attribute
*/
template<typename T>
void setAttribute(const string &attribute, const T &value);
/**
* \brief return true if a node has attributes
*/
bool hasAttributes() { return _attributes.size() > 0; }
/**
* \brief return true if an attribute exists
*/
bool hasAttribute(const string &attribute);
/** for iterator-based access of Attributes */
typedef std::map<string,string>::iterator AttributeIterator;
/**
* \brief returns an iterator to an attribute
*/
AttributeIterator findAttribute(const string &attribute){ return _attributes.find(attribute); }
/**
* \brief returns an iterator to the first attribute
*/
AttributeIterator firstAttribute(){ return _attributes.begin(); }
/**
* \brief returns an iterator to the last attribute
*/
AttributeIterator lastAttribute(){ return _attributes.end(); }
/**
* \brief return attribute as type
*
* returns an attribute after type conversion, e.g.
* p.getAttribute<int>() returns an integer
*/
template<typename T>
T getAttribute( AttributeIterator it);
static int getIOindex(){return IOindex;};
private:
map<string,Property*> _map;
map<string,string> _attributes;
list<Property> _properties;
string _name;
string _value;
string _path;
static const int IOindex;
};
inline Property &Property::set(const string &key, const string &value)
{
Property &p = get(key);
p.value() = value;
return p;
}
inline Property &Property::add(const string &key, const string &value)
{
string path = _path;
if(path != "") path = path + ".";
_properties.push_back(Property(key, value, path + _name ));
_map[key] = &(_properties.back());
return _properties.back();
}
inline bool Property::exists(const string &key)
{
try { get(key); }
catch(std::exception &err) { return false;}
return true;
}
bool load_property_from_xml(Property &p, string file);
// TO DO: write a better function for this!!!!
template<>
inline bool Property::as<bool>() const
{
if(_value == "true" || _value == "TRUE" || _value == "1") return true;
else return false;
}
template<typename T>
inline T Property::as() const
{
return lexical_cast<T>(_value, "wrong type in " + _path + "." + _name + "\n");
}
template<>
inline std::string Property::as<std::string>() const
{
string tmp(_value);
boost::trim(tmp);
return tmp;
}
template<>
inline vec Property::as<vec>() const {
vector<double> tmp;
Tokenizer tok(as<string > (), " ,");
tok.ConvertToVector<double>(tmp);
if (tmp.size() != 3)
throw runtime_error("Vector has " + boost::lexical_cast<string > (tmp.size()) + " instead of three entries");
return vec(tmp[0], tmp[1], tmp[2]);
}
template<>
inline vector<unsigned int> Property::as<vector <unsigned int> >() const {
vector<unsigned int> tmp;
Tokenizer tok(as<string > (), " ,");
tok.ConvertToVector<unsigned int>(tmp);
return tmp;
}
template<>
inline vector<int> Property::as<vector <int> >() const {
vector<int> tmp;
Tokenizer tok(as<string > (), " ,\n\t");
tok.ConvertToVector<int>(tmp);
return tmp;
}
template<>
inline vector<double> Property::as<vector <double> >() const {
vector<double> tmp;
Tokenizer tok(as<string > (), " ,\n\t");
tok.ConvertToVector<double>(tmp);
return tmp;
}
inline bool Property::hasAttribute(const string &attribute) {
std::map<string,string>::iterator it;
it = _attributes.find(attribute);
if ( it == _attributes.end() ) return false;
return true;
}
template<typename T>
inline T Property::getAttribute(std::map<string,string>::iterator it)
{
if (it != _attributes.end()) {
return lexical_cast<T>((*it).second);
} else {
throw std::runtime_error("attribute " + (*it).first + " not found\n");
}
}
template<typename T>
inline T Property::getAttribute(const string &attribute)
{
std::map<string,string>::iterator it;
it = _attributes.find(attribute);
if (it != _attributes.end()) {
return lexical_cast<T>(_attributes[attribute], "wrong type in attribute " + attribute + " of element " + _path + "." + _name + "\n");
} else {
throw std::runtime_error("attribute " + attribute + " not found\n");
}
}
template<typename T>
inline void Property::setAttribute(const string &attribute, const T &value)
{
_attributes[attribute] = lexical_cast<string>(value, "wrong type to set attribute");
}
inline void throwRuntimeError(string message) {
}
}}
#endif /* _VOTCA_TOOLS_PROPERTY_H */
|