grive2/libgrive/src/xml/Node.hh

119 lines
2.9 KiB
C++
Raw Normal View History

2012-05-03 21:44:52 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
2013-04-28 19:50:20 +04:00
#include "util/Exception.hh"
2012-05-10 20:06:05 +04:00
#include <boost/iterator_adaptors.hpp>
2012-05-09 18:25:42 +04:00
#include <iosfwd>
2012-05-03 21:44:52 +04:00
#include <string>
#include <vector>
2012-05-06 20:03:33 +04:00
#include <utility>
2012-05-03 21:44:52 +04:00
namespace gr { namespace xml {
2012-05-12 06:48:54 +04:00
class NodeSet ;
2012-05-03 21:44:52 +04:00
class Node
{
2012-05-10 20:06:05 +04:00
private :
class Impl ;
typedef std::vector<Impl*> ImplVec ;
2012-05-06 13:13:48 +04:00
public :
class iterator ;
2013-04-28 19:50:20 +04:00
typedef boost::error_info<struct DupAttr, std::string> DupAttr_ ;
2012-05-03 21:44:52 +04:00
public :
Node() ;
Node( const Node& node ) ;
~Node() ;
static Node Element( const std::string& name ) ;
static Node Text( const std::string& name ) ;
Node& operator=( const Node& node ) ;
2012-05-12 08:27:35 +04:00
void Swap( Node& node ) ;
2012-05-03 21:44:52 +04:00
Node AddElement( const std::string& name ) ;
Node AddText( const std::string& text ) ;
2012-05-05 09:01:23 +04:00
void AddNode( const Node& node ) ;
2012-05-12 07:47:51 +04:00
void AddNode( iterator first, iterator last ) ;
2012-05-05 09:01:23 +04:00
void AddAttribute( const std::string& name, const std::string& val ) ;
2012-05-03 21:44:52 +04:00
2012-05-12 07:09:31 +04:00
NodeSet operator[]( const std::string& name ) const ;
2012-05-12 07:47:51 +04:00
operator std::string() const ;
bool operator==( const std::string& value ) const ;
2012-05-05 09:01:23 +04:00
const std::string& Name() const ;
std::string Value() const ;
2012-05-03 21:44:52 +04:00
// read-only access to the reference counter. for checking.
std::size_t RefCount() const ;
enum Type { element, attr, text } ;
Type GetType() const ;
2012-05-05 09:01:23 +04:00
static bool IsCompatible( Type parent, Type child ) ;
2012-05-09 18:25:42 +04:00
static std::ostream& PrintChar( std::ostream& os, char c ) ;
static std::ostream& PrintString( std::ostream& os, const std::string& s ) ;
2012-05-06 20:03:33 +04:00
iterator begin() const ;
iterator end() const ;
std::size_t size() const ;
NodeSet Children() const ;
2012-05-03 21:44:52 +04:00
2012-05-12 06:48:54 +04:00
NodeSet Attr() const ;
std::string Attr( const std::string& attr ) const ;
bool HasAttr( const std::string& attr ) const ;
2012-05-12 06:48:54 +04:00
2012-05-03 21:44:52 +04:00
private :
explicit Node( Impl *impl ) ;
2012-05-12 07:09:31 +04:00
typedef std::pair<ImplVec::iterator, ImplVec::iterator> Range ;
2012-05-03 21:44:52 +04:00
private :
Impl *m_ptr ;
} ;
2012-05-10 20:06:05 +04:00
class Node::iterator : public boost::iterator_adaptor<
Node::iterator,
Node::ImplVec::iterator,
Node,
boost::random_access_traversal_tag,
Node
>
{
public :
2012-06-03 21:07:14 +04:00
iterator( ) ;
2012-05-10 20:06:05 +04:00
explicit iterator( ImplVec::iterator i ) ;
private :
friend class boost::iterator_core_access;
reference dereference() const ;
} ;
std::ostream& operator<<( std::ostream& os, const Node& node ) ;
2012-05-03 21:44:52 +04:00
} } // end of namespace