Node.h
1/*
2 * Copyright (C) 2006-2012 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Xml_Node_h
30#define Pt_Xml_Node_h
31
32#include <Pt/Xml/Api.h>
33#include <Pt/Xml/XmlError.h>
34
35namespace Pt {
36
37namespace Xml {
38
50class Node
51{
52 public:
53 enum Type
54 {
55 Unknown = 0,
56 DocType = 1,
57 EndDocType = 2,
58 StartDocument = 3,
59 EndDocument = 4,
60 StartElement = 5,
61 EndElement = 6,
62 Characters = 7,
63 Comment = 8,
64 ProcessingInstruction = 9,
65 EntityReference = 10
66 };
67
69 virtual ~Node()
70 {}
71
74 Type type() const
75 { return _type; }
76
77 protected:
80 explicit Node(Type type)
81 : _type(type)
82 { }
83
84 private:
85 Type _type;
86};
87
89template <typename T>
90T* nodeCast(Node* node)
91{
92 T* e = 0;
93
94 if( node->type() == T::nodeId() )
95 e = static_cast<T*>(node);
96
97 return e;
98}
99
101template <typename T>
102const T* nodeCast(const Node* node)
103{
104 const T* e = 0;
105
106 if( node->type() == T::nodeId() )
107 e = static_cast<const T*>(node);
108
109 return e;
110}
111
113template <typename T>
114T& nodeCast(Node& node)
115{
116 if( node.type() != T::nodeId() )
117 throw XmlError("unexpected node type");
118
119 return static_cast<T&>(node);
120}
121
123template <typename T>
124const T& nodeCast(const Node& node)
125{
126 if( node.type() != T::nodeId() )
127 throw XmlError("unexpected node type");
128
129 return static_cast<const T&>(node);
130}
131
132} // namespace Xml
133
134} // namespace Pt
135
136#endif // Pt_Xml_Node_h
XML document node.
Definition Node.h:51
Node(Type type)
Constructs a new Node object with the specified node type.
Definition Node.h:80
virtual ~Node()
Destructor.
Definition Node.h:69
Type type() const
Returns the type of the node.
Definition Node.h:74
XmlError(const std::string &what)
Construct with message.
Read and write XML ducuments.
Core module.
Definition Allocator.h:33