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 
35 namespace Pt {
36 
37 namespace Xml {
38 
50 class 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,
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 
89 template <typename T>
90 T* 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 
101 template <typename T>
102 const 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 
113 template <typename T>
114 T& 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 
123 template <typename T>
124 const 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
Represents a closing element tag in an XML document.
Definition: EndElement.h:44
A processing instruction of an XML document.
Definition: ProcessingInstruction.h:47
A DocType node represents the begin of a DTD.
Definition: DocType.h:52
virtual ~Node()
Destructor.
Definition: Node.h:69
A Character node represents text in an XML document.
Definition: Characters.h:47
A Node representing the begin of the XML document.
Definition: StartDocument.h:44
An entity reference XML node.
Definition: Entity.h:136
An EndDocType node represents the end of a DTD.
Definition: DocType.h:158
A Comment node represents a comment in an XML document.
Definition: Comment.h:44
Represents the end of an XML document.
Definition: EndDocument.h:45
Node(Type type)
Constructs a new Node object with the specified node type.
Definition: Node.h:80
Type type() const
Returns the type of the node.
Definition: Node.h:74
XML document node.
Definition: Node.h:50
Represents the start of an element in an XML document.
Definition: StartElement.h:318