MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
treewalker.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  *
42  * Software, Files, Libraries and all other items referenced in this clause refers only
43  * to the contents of this file and associated documentation.
44  *
45  * Pugixml parser - version 1.0
46  * --------------------------------------------------------
47  * Copyright © 2006-2012, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
48  * Report bugs and download new versions at http://pugixml.org/
49  *
50  * This library is distributed under the MIT License. See notice at the end
51  * of this file.
52  *
53  * This work is based on the pugxml parser, which is:
54  * Copyright © 2003, by Kristen Wegner (kristen@tima.net)
55  */
56 #ifndef _xmltreewalker_h
57 #define _xmltreewalker_h
58 
59 /// @file
60 /// @brief This defines the TreeWalker Interface
61 
62 #include "datatypes.h"
63 #include "XML/node.h"
64 
65 
66 
67 namespace Mezzanine
68 {
69  namespace XML
70  {
71  //////////////////////////////////////////////////////////////////////////////
72  /// @brief Used to call a function OnEachNode member of the subtree of nodes descended from a specific node.
73  /// @details If you want to do a deep tree traversal, you'll either have to do it via a recursive function or some
74  /// equivalent method or use a TreeWalker. This provides a helper for depth-first traversal of a subtree. In order
75  /// to use it, you have to implement XML::TreeWalker interface and call XML::Node::Traverse() function. \n\n
76  /// * First, TreeWalker::OnTraversalBegin() is called with traversal root as its argument.\n
77  /// * Then, TreeWalker::OnEachNode() function is called for all nodes in the traversal subtree in depth first order, excluding the traversal root. Each Node is passed as an argument.\n
78  /// * Finally, TreeWalker::OnTraversalEnd() function is called with traversal root as its argument.\n\n
79  /// If TreeWalker::OnTraversalBegin(), TreeWalker::OnTraversalEnd() or any of the TreeWalker::OnEachNode() calls return false, the traversal
80  /// is terminated and false is returned as the traversal result; otherwise, the traversal results in true. Note that
81  /// you don't have to override begin or end functions; their default implementations return true.\n\n
82  /// You can get the node's depth relative to the traversal root at any point by calling TreeWalker::Depth() function.
84  {
85  friend class Node;
86 
87  private:
88 
89  /// @internal
90  /// @brief Stores How deep in the tree we are
91  int TraversalDepth;
92 
93  protected:
94 
95  /// @brief How many descendants deep are we during traversal.
96  /// @return This returns -1 if called from TreeWalker::OnTraversalBegin() or TreeWalker::OnTraversalEnd(), and returns 0-based depth if called from OnEachNode - depth is 0 for all children of the traversal root, 1 for all grandchildren, 2 for great-grandchildren and so on.
97  int Depth() const;
98 
99  public:
100 
101  /// @brief Default constructor, initializes depth, and can do little else without a fully implemented treewalker.
102  TreeWalker();
103 
104  /// @brief Virtual deconstructor. Tears down a TreeWalker
105  virtual ~TreeWalker();
106 
107  /// @brief Called by the root Node of the xml subtree when traversal begins.
108  /// @param node The first node the Tree to traverse
109  /// @details By default this simply returns true, but is expected to be overridden with any desired behavior
110  /// @return True by default. If it returns false, then traversal ends and the Node::Traverse() that was called is expected to return false.
111  virtual bool OnTraversalBegin(Node& node);
112 
113  /// @brief A Pure Virtual function that is expected to be implemented to create the desired behavior.
114  /// @param node The curren node being trraversed.
115  /// @details This is called on every Node that is traversed except the root node of the traversed subtree. Can be used to perform sophisticated searches
116  /// of a portion of the xml document, alter the document on a large scale, gather statistics, or just about any other behavior that requires touching
117  /// many nodes.
118  /// @return if true Traversal is expected to continue, if false, then traversal ends and the Node::Traverse() that was called is expected to return false.
119  virtual bool OnEachNode(Node& node) = 0;
120 
121  /// @brief Called on the root Node of the xml subtree when traversal ends.
122  /// @param node The last node the Tree to traverse
123  /// @details By default this simply returns true, but is expected to be overridden with any desired behavior
124  /// @return True by default. If it returns false, then traversal ends and the Node::Traverse() that was called is expected to return false.
125  virtual bool OnTraversalEnd(Node& node);
126  };
127  }
128 }// /namespace Mezzanine
129 
130 
131 
132 #endif // Include guard
133 
134 /*
135  *
136  * Software, Files, Libraries and all other items referenced in this clause refers only
137  * to the contents of this file and associated documentation.
138  *
139  * Copyright © 2006-2012 Arseny Kapoulkine
140  *
141  * Permission is hereby granted, free of charge, to any person
142  * obtaining a copy of this software and associated documentation
143  * files (the "Software"), to deal in the Software without
144  * restriction, including without limitation the rights to use,
145  * copy, modify, merge, publish, distribute, sublicense, and/or sell
146  * copies of the Software, and to permit persons to whom the
147  * Software is furnished to do so, subject to the following
148  * conditions:
149  *
150  * The above copyright notice and this permission notice shall be
151  * included in all copies or substantial portions of the Software.
152  *
153  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
154  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
155  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
156  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
157  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
158  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
159  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
160  * OTHER DEALINGS IN THE SOFTWARE.
161  */