From: Egor C. <eg...@us...> - 2003-01-28 22:37:03
|
Update of /cvsroot/eas-dev/eas-dev/libs/libsxmlstream/include In directory sc8-pr-cvs1:/tmp/cvs-serv6701/libs/libsxmlstream/include Modified Files: sxml.hxx sxmlstream.hxx Log Message: Major code refactoring. _SXml struct --> SXmlNode class. Small performance optimization. *Warning* Code compiled but not tested! Index: sxml.hxx =================================================================== RCS file: /cvsroot/eas-dev/eas-dev/libs/libsxmlstream/include/sxml.hxx,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- sxml.hxx 25 Jan 2003 21:51:28 -0000 1.2 +++ sxml.hxx 28 Jan 2003 22:36:24 -0000 1.3 @@ -7,6 +7,7 @@ /* */ /* Author(s): */ /* Yurii A. Rashkovskii <yr...@op...> */ +/* Egor Cheshkov <eg...@ip...> */ /* */ /* */ /* This program is free software; you can redistribute */ @@ -22,58 +23,55 @@ #define _LIBSXMLSTREAM_SXML_HXX_ #include <string> -#include <vector> +#include <list> using namespace std; -typedef enum -{ - SXml_Element_t, - SXml_Attribute_t, - SXml_Char_t, - SXml_Namespace_t -} SXml_t; - - -struct _SXml { - SXml_t type; - string data; - vector<_SXml> * childs; +enum SXmlNode_t { + SXmlNode_Element_t, + SXmlNode_Attribute_t, + SXmlNode_Char_t, + SXmlNode_Namespace_t }; -typedef _SXml SXml; - - - -/* Type checks */ -#define Is_SXml_t(x,y) ( x.type == y ) - -#define Is_SXml_Element(x) Is_SXml_t(x,SXml_Element_t) -#define Is_SXml_Attribute(x) Is_SXml_t(x,SXml_Attribute_t) -#define Is_SXml_Char(x) Is_SXml_t(x,SXml_Char_t) -#define Is_SXml_Namespace(x) Is_SXml_t(x,SXml_Namespace_t) - -/* Tests */ - -#define SXml_has_property(x) ( ((x.childs)->size() == 1) && \ - (x.type == ((SXml)((x.childs)->at(0))).type) ) - - -/* SXml creation */ +struct SXmlNode { + SXmlNode_t type; + string data; + list<SXmlNode> *descendants; -SXml SXml_create(SXml_t aType, string aData); -SXml SXml_Element_create(string aName); -SXml SXml_Attribute_create(string aName, string aValue); -SXml SXml_Char_create(string aData); -SXml SXml_Namespace_create(string aPrefix, string aURI); + SXmlNode() {descendants = new list<SXmlNode>; type = SXmlNode_Element_t;} + SXmlNode(const SXmlNode& v); + SXmlNode& operator=(const SXmlNode& v); + ~SXmlNode() { delete descendants; } + + SXmlNode(const SXmlNode_t aType, const string aData) {data = aData; + type = aType;} -/* SXml destruction */ + bool isElement() const { return type == SXmlNode_Element_t; } + bool isAttribute() const { return type == SXmlNode_Attribute_t; } + bool isChar() const { return type == SXmlNode_Char_t; } + bool isNamespace() const { return type == SXmlNode_Namespace_t; } + + // FIXME: is this correct? copied from previous realization + bool hasProperty() const { return descendants->size() == 1 && + type == descendants->front().type; } + + // We do not copy the child element for better performance + void addChild(const SXmlNode& e) { descendants->push_back(e); } + string getProperty() const { return hasProperty() ? + descendants->front().data : 0; } -void SXml_delete(SXml e); -/* Childs operations */ -void SXml_create_child(SXml e, SXml c); + // FIXME: Should I create subclasses for Element, Attributr, etc? + + /* Factory functions */ + static SXmlNode Element(const string aName) { return + SXmlNode(SXmlNode_Element_t, aName); } + static SXmlNode Attribute(const string aName, const string aValue); + static SXmlNode Char(const string aData) { return SXmlNode(SXmlNode_Char_t, + aData); } + static SXmlNode Namespace(const string aPrefix, const string aURI); -string SXml_get_property(SXml e); +}; #endif /* _LIBSXMLSTREAM_SXML_HXX_ */ Index: sxmlstream.hxx =================================================================== RCS file: /cvsroot/eas-dev/eas-dev/libs/libsxmlstream/include/sxmlstream.hxx,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- sxmlstream.hxx 28 Jan 2003 06:44:32 -0000 1.6 +++ sxmlstream.hxx 28 Jan 2003 22:36:25 -0000 1.7 @@ -30,15 +30,22 @@ { protected: - queue<SXml> * m_queue; + queue<SXmlNode> * m_queue; public: SXmlStream(); ~SXmlStream(); bool queueIsEmpty(); - SXml pop(); - void push(SXml e); + + // Do not copy an element, instead return by reference. + // Copying it might be expensive if we have a lot of + // descendants. + // FIXME: Is it ok? + SXmlNode& pop(); + + // FIXME: Pass by reference, is it ok? + void push(const SXmlNode& e) {m_queue->push(e);} }; |