XMLTree.h

00001 /****************************************************************************
00002  * Copyright 2007 Evan Drumwright
00003  * This library is distributed under the terms of the GNU General Public 
00004  * License (found in COPYING).
00005  ****************************************************************************/
00006 
00007 #ifndef _PHYSSIM_XML_TREE_H
00008 #define _PHYSSIM_XML_TREE_H
00009 
00010 #include <boost/enable_shared_from_this.hpp>
00011 #include <list>
00012 #include <string>
00013 #include <set>
00014 #include <Physsim/VectorN.h>
00015 #include <Physsim/MatrixN.h>
00016 #include <Physsim/Types.h>
00017 
00018 namespace Physsim {
00019 
00021 class XMLAttrib
00022 {
00023         public:
00024                 XMLAttrib(const std::string& name, const std::string& string_value);
00025                 XMLAttrib(const std::string& name, Real real_value);
00026                 XMLAttrib(const std::string& name, int int_value);
00027                 XMLAttrib(const std::string& name, unsigned unsigned_value);
00028                 XMLAttrib(const std::string& name, const VectorN& vector_value);
00029                 XMLAttrib(const std::string& name, const MatrixN& matrix_value);
00030                 XMLAttrib(const std::string& name, bool bool_value);
00031                 XMLAttrib(const std::string& name, long long_value);
00032                 const std::string& name() const { return _name; }
00033                 void name(const std::string& name) { _name = name; }
00034                 const std::string& get_string_value() const { return _value; }
00035                 Real get_real_value() const { return (Real) atof(_value.c_str()); }
00036                 int get_int_value() const { return atoi(_value.c_str()); }
00037                 unsigned get_unsigned_value() const { return (unsigned) atoi(_value.c_str()); }
00038                 bool get_bool_value() const;
00039                 long get_long_value() const { return atol(_value.c_str()); }
00040                 std::list<std::string> get_strings_value() const;
00041                 VectorN get_vector_value() const;
00042                 MatrixN get_matrix_value() const;
00043                 bool operator==(const XMLAttrib& a) const { return _name == a.name(); }
00044                 bool operator<(const XMLAttrib& a) const { return _name < a.name(); }
00045 
00046         private:
00047                 std::string _name;
00048                 std::string _value;
00049 };
00050 
00052 class XMLTree : public boost::enable_shared_from_this<XMLTree>
00053 {
00054         public:
00055                 XMLTree(const std::string& name);
00056                 XMLTree(const std::string& name, const std::list<XMLAttrib>& attributes);
00057                 const XMLAttrib* get_attrib(const std::string& attrib_name) const;
00058                 std::list<XMLTreeConstPtr> find_child_nodes(const std::string& name) const;
00059                 std::list<XMLTreeConstPtr> find_child_nodes(const std::list<std::string>& name) const;
00060                 std::list<XMLTreeConstPtr> find_descendant_nodes(const std::string& name) const;
00061                 const std::list<XMLTreeConstPtr>& get_child_nodes() const;
00062 
00064                 const std::set<XMLAttrib>& get_attributes() const { return _attribs; }
00065 
00067                 const std::string& name() const { return _name; }
00068 
00070                 void name(const std::string name) { _name = name; }
00071 
00073                 boost::shared_ptr<void> get_object() const { return _object; }
00074 
00076                 void set_object(boost::shared_ptr<void> object) { _object = object; }
00077 
00079                 void add_child(XMLTreePtr child) { _children.push_back(child); child->set_parent(shared_from_this()); }
00080 
00082                 void set_parent(XMLTreePtr parent) { _parent = parent; }
00083 
00085                 boost::weak_ptr<XMLTree> get_parent() const { return _parent; }
00086 
00088                 void add_attrib(const XMLAttrib& attrib) { _attribs.insert(attrib); }
00089 
00090         private:
00091                 std::string _name;
00092                 std::string _id;
00093                 std::set<XMLAttrib> _attribs;
00094                 std::list<XMLTreePtr> _children;
00095                 boost::weak_ptr<XMLTree> _parent;
00096                 boost::shared_ptr<void> _object;
00097 }; // end class
00098 
00099 std::ostream& operator<<(std::ostream& out, const XMLTree& tree);
00100 std::ostream& operator<<(std::ostream& out, const XMLAttrib& attr);
00101 
00102 } // end namespace
00103 
00104 #endif
00105 

Generated on Wed Oct 24 14:54:22 2007 for Physsim by  doxygen 1.5.1