yake::node<>

What is it?

node<> is a generic, templated class that can be used to represent nodes in and the n-ary trees themselves. Data is associated with nodes, also they can be indexed by keys.

Included are various algorithms for traversal (shallow, deep, top-down, bottom-up …) and finding similar to xpath (e.g. findByPath(“scene/nodes/scenenode/entity”)) and others.

Can it be used outside of Yake?

Yes. It's a self-contained set of a few files.

  • The node template itself depends on the STL.
  • The algorithms partly depend on boost.
  • The TinyXML based XML DOM wrapper depends on, well, TinyXML.

The license is (for the sake of simplicity) currently the same as for YAKE: LGPL, even though the LGPL isn't really ideal for template code.

If you need the code under a different license or have suggestions please send an E-Mail to info AT yake.org or use the contact form on http://www.yake.org.

Files

Files for client use:

  • node.h - declaration/implementation of node<>

Optional files for client use:

  • node_algorithms.h - various algorithms (traversal, finders …)
  • node_xml.h - XML DOM wrapper (backend can be configured/selected)

Files for internal use:

  • node_detail.h - implementation details (e.g. key creators)
  • node_xml_tiny.h - TinyXML based XML DOM wrapper

Can I derive from node<>?

Yes. Use this feature with care. Also the read the section about callbacks/notifications.

Configuration / Callbacks / Notifications

node<> can optionally provide callbacks and notifications for certain events (like “child node added”). You can subscribe to them either via signals (using boost::signals and boost::bind) or via listeners or via inheritance and notification methods.

All the three options can be configured seperately.

Have a look at node.h for more details on all the possible options. For example, you can also select the policy for default implementation of notification methods.

In node_xml.h you can select the XML parsing provider for the XML DOM wrapper.

Examples

XML

Loading a XML file and performing basic operations
#include <tree/node.h>
#include <tree/node_xml.h>
#include <tree/node_xml_algorithm.h>
 
using namespace tree;
 
XmlDom doc;
doc.replace("data/test.scene.xml");
 
// You can replace the currently loaded data:
doc.replace("data/anotherWorld.scene.xml");
 
// Dump the tree to the console (or any other stream, if you want to):
doc.print( std::cout );
 
// Explicitely clear the DOM:
doc.clear();
Demo XML file we'll be using for the next few examples
<scene>
  <nodes>
    <scenenode id="1">
      <position x="1" y="1" z="0" />
    </scenenode>
    <scenenode id="2">
      <position x="100" y="0" z="0" />
    </scenenode>
  </nodes>
</scene>
Finding a scene node in a hierarchy by path
XmlNode* foundNode = find_path(doc.getDocumentNode(), "scene/nodes/scenenode/position");
// "foundNode" now points to the XML DOM node for the "scenenode" tag with id "1".
 
std::cout << *foundNode; // dump to console if you want :)
Accessing an attribute of the "scenenode" node
double x = foundNode->value().getAttributeAs<double>("x");
Alternative querying mechanism (shorter)
// In case the node and/or attribute cannot be found we let x default to 0.0.
double x = getXmlAttributeByPathAs<double>( doc.getDocumentNode(),
                        "scene/nodes/scenenode/position/x", 0.0 );
// x is now 1. as the first "scenenode" found has been used. See below for filtering options.
Applying filters...

… to select a different than the first scenenode:

// Let's select the scene node whose "id" attribute is "2":
double x = getXmlAttributeByPathAs<double>( doc.getDocumentNode(),
                        "scene/nodes/scenenode|id=2/position/x", 0.0 );
// x is now 100.
 
component/yakenode.txt · Last modified: 2008/02/21 21:54 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki