ALPS Project

Header file parser/xmlattributes.h

This header contains the XMLAttribute and XMLAttributes classes that can store attributes of XML tags.

Synopsis

namespace alps {

class XMLAttribute
{
public:
  typedef std::string key_type;
  typedef std::string value_type;

  XMLAttribute();
  XMLAttribute(const XMLAttribute& attr);
  XMLAttribute(const key_type& k);
  XMLAttribute(const key_type& k, const value_type& v);
  XMLAttribute(const key_type& k, const char * v);
  template<class T> XMLAttribute(const key_type& k, const T& v);

  key_type& key();
  const key_type& key() const;
  value_type& value();
  const value_type& value() const;
};

class XMLAttributes
{
public:
  typedef XMLAttribute::key_type    key_type;
  typedef XMLAttribute::value_type  value_type;
  typedef std::vector<XMLAttribute> list_type;
  typedef list_type::size_type      size_type;

  typedef list_type::iterator       iterator;
  typedef list_type::const_iterator const_iterator;

  XMLAttributes();

  void clear();
  size_type size() const;

  bool defined(const key_type& k);

  // accessing elements by key
  value_type& operator[](const key_type& k);
  const value_type& operator[](const key_type& k) const;
  value_type value_or_default(const key_type& k, const value_type& v) const;

  iterator begin();
  const_iterator begin() const;
  iterator end();
  const_iterator end() const;

  void push_back(const XMLAttribute& attr);
  void push_back(const key_type& k, const value_type& v);
  XMLAttributes& operator<<(const XMLAttribute& a);
  XMLAttributes& operator<<(const XMLAttributes& attr);
};

} // namespace alps

XMLAttribute class

XMLAttribute class is a pair of strings which stores an attribute of a XML tag.

Member functions

XMLAttribute();
the default constructor
XMLAttribute(const XMLAttribute& attr);
the copy constructor
XMLAttribute(const key_type& k);
constructor with a given name (key) but an empty value
XMLAttribute(const key_type& k, const value_type& v);
XMLAttribute(const key_type& k, const char * v);
template<class T> XMLAttribute(const key_type& k, const T& v);
constructors from a pair of a name (key) and a value
key_type& key();
const key_type& key() const;
access the name of the attribute
value_type& value();
const value_type& value() const;
access the value of the attribute

XMLAttributes class

XMLAttributes class is a set of XMLAttribute's. Each XMLAttribute element can be accessed directly by a name (key), or sequentially by using iterators. The difference from std::map<XMLAttribute> is that the order of attributes in the set is preserved as defined.

Member functions

XMLAttributes();
the default constructor
void clear();
clears the contents
size_type size();
returns the number of attributes
bool defined(const key_type& k);
returns true if an attribute exists for the given attribute name.
value_type& operator[](const key_type& k);
const value_type& operator[](const key_type& k) const;
access the attribute value with a given name. Note that if the attribute does not exists, a new attribute with the given name will be created and appended at the last of the set.
value_type value_or_default(const key_type& k, const value_type& v) const;
returns the attribute value with a given name. If the attribute does not exists, the default value specified by the second argument will be returned.
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
return iterators pointing the first element in the set, or the next to the last element.
void push_back(const XMLAttribute& attr);
void push_back(const key_type& k, const value_type& v);
append a new attribute. If there already exists an attribute with the same name, an exception will be thrown.
XMLAttributes& operator<<(const XMLAttribute& a);
appends a new attribute. If there already exists an attribute with the same name, the attribute value will be replaced by a new one.
XMLAttributes& operator<<(const XMLAttributes& attr);
appends new attributes. If there already exist attributes with the same names, the attribute values will be replaced by new ones.

copyright (c) 1994-2010 by Matthias Troyer

Distributed under the Boost Software License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)