Hydra 0.20
hydra.src/hydra/NodePath.h
00001 
00002 /*
00003  * Copyright (c) 2005 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_HYDRA_NODEPATH_H__
00009 #define __INCLUDED_HYDRA_NODEPATH_H__
00010 
00011 #include <exception>
00012 
00013 #include <QDomDocument>
00014 
00015 namespace hydra
00016 {
00017 
00018 /**
00019  * This is a helper class for parsing wxXml file structures.
00020  *
00021  * @author Aleksander Demko
00022  */ 
00023 class NodePath
00024 {
00025   public:
00026     class error : public std::exception {
00027       virtual const char* what() const throw ()
00028       { return "NodePath::error"; }
00029     };
00030     class xml_error : public error {};
00031     class numeric_error : public error {};
00032     class verify_error : public error {};
00033     class prop_error : public error {};
00034 
00035   public:
00036     /**
00037      * Associate a NodePath based off the root node of the given document.
00038      *
00039      * @author Aleksander Demko
00040      */ 
00041     NodePath(QDomDocument doc)
00042       : dm_node(doc.documentElement()) {}
00043     /**
00044      * Associate a NodePath based off the given node
00045      *
00046      * @author Aleksander Demko
00047      */ 
00048     NodePath(QDomElement node)
00049       : dm_node(node) {}
00050 
00051     /// implicit converter to QDomElement
00052     operator QDomElement(void) const { return dm_node; }
00053 
00054     // Returns true of the node has a sub node with the given name.
00055     bool hasChild(const QString &subpath);
00056     // get and make if needed
00057     NodePath operator[](const QString &subpath);
00058     // get and throw exception if not found
00059     NodePath operator()(const QString &subpath) const;
00060 
00061     // apend a new node with the given name (this always adds new nodes). node names need not be unique
00062     NodePath append(const QString &subpath);
00063     // erase the given subnode, if it exists, returns true of something was deleted
00064     bool erase(const QString &subpath);
00065 
00066     // loads the next sibling. return true if it was loaded, false on failure (on failre, "this" is now undefined
00067     // a sibling is the next sibling node that has the same name as this
00068     bool loadNextSibling(void);
00069 
00070     /// sets the property value
00071     void setPropVal(const QString &key, const QString &val);
00072     /// sets the property value
00073     void setPropVal(const QString &key, double val);
00074 
00075     // gets a prop as a string, throws if not found
00076 
00077     QString getPropAsString(const QString &key);
00078     // gets a prop as a long, throws if not found
00079     long getPropAsLong(const QString &key);
00080     // gets a prop as a double, throws if not found
00081     double getPropAsDouble(const QString &key);
00082 
00083     // in the future, add defaults that never throw/error
00084 
00085     QDomElement * operator->(void) { return &dm_node; }
00086     const QDomElement * operator->(void) const { return &dm_node; }
00087     
00088     // assign to content node
00089     void operator = (const QString &content);
00090     // assign to content node (numeric)
00091     void operator = (double d);
00092 
00093     QString asString(void) const { return dm_node.text(); }
00094     long asLong(void) const;
00095 
00096     /// its like an assert
00097     static void verify(bool b) { if (!b) throw verify_error(); }
00098 
00099   private:
00100     QDomElement dm_node;
00101 };
00102 
00103 }
00104 
00105 #endif
00106 
 All Classes Namespaces Functions Variables