diff options
author | Willem Jan Palenstijn <wjp@usecode.org> | 2015-05-01 17:48:32 +0200 |
---|---|---|
committer | Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl> | 2015-05-06 15:18:03 +0200 |
commit | 47fe3421585302f2101691a685ab99b0e1ad5cfc (patch) | |
tree | 5f7ff204c5d19f83313487a840748724e04ad8e5 /src/XMLNode.cpp | |
parent | bf31003d74f538a9096ef5999b31b0daa58c38c9 (diff) | |
download | astra-47fe3421585302f2101691a685ab99b0e1ad5cfc.tar.gz astra-47fe3421585302f2101691a685ab99b0e1ad5cfc.tar.bz2 astra-47fe3421585302f2101691a685ab99b0e1ad5cfc.tar.xz astra-47fe3421585302f2101691a685ab99b0e1ad5cfc.zip |
Change XMLNode* to XMLNode
An XMLNode object is already simply a pointer, so no need to dynamically allocate XMLNodes.
Diffstat (limited to 'src/XMLNode.cpp')
-rw-r--r-- | src/XMLNode.cpp | 155 |
1 files changed, 66 insertions, 89 deletions
diff --git a/src/XMLNode.cpp b/src/XMLNode.cpp index 5e9d927..75985cc 100644 --- a/src/XMLNode.cpp +++ b/src/XMLNode.cpp @@ -39,21 +39,10 @@ using namespace std; //----------------------------------------------------------------------------- -// Utility function to delete a list of nodes -static void deleteNodes(list<XMLNode*>& nodes) -{ - for (list<XMLNode*>::iterator i = nodes.begin(); i != nodes.end(); ++i) - delete (*i); - - nodes.clear(); -} - - -//----------------------------------------------------------------------------- // default constructor XMLNode::XMLNode() { - + fDOMElement = 0; } //----------------------------------------------------------------------------- @@ -79,14 +68,14 @@ void XMLNode::setDOMNode(xml_node<>* n) //----------------------------------------------------------------------------- // print XML Node -void XMLNode::print() +void XMLNode::print() const { std::cout << fDOMElement; } //----------------------------------------------------------------------------- // print XML Node -std::string XMLNode::toString() +std::string XMLNode::toString() const { std::string s; ::print(std::back_inserter(s), *fDOMElement, 0); @@ -95,64 +84,61 @@ std::string XMLNode::toString() //----------------------------------------------------------------------------- // Get single node -XMLNode* XMLNode::getSingleNode(string name) +XMLNode XMLNode::getSingleNode(string name) const { xml_node<> *node = fDOMElement->first_node(name.c_str()); - if (node) - return new XMLNode(node); - else - return 0; + return XMLNode(node); } //----------------------------------------------------------------------------- // Get list of nodes -list<XMLNode*> XMLNode::getNodes(string name) +list<XMLNode> XMLNode::getNodes(string name) const { - list<XMLNode*> result; + list<XMLNode> result; xml_node<> *iter; for (iter = fDOMElement->first_node(name.c_str()); iter; iter = iter->next_sibling(name.c_str())) { - result.push_back(new XMLNode(iter)); + result.push_back(XMLNode(iter)); } return result; } //----------------------------------------------------------------------------- // Get list of nodes -list<XMLNode*> XMLNode::getNodes() +list<XMLNode> XMLNode::getNodes() const { - list<XMLNode*> result; + list<XMLNode> result; xml_node<> *iter; for (iter = fDOMElement->first_node(); iter; iter = iter->next_sibling()) { - result.push_back(new XMLNode(iter)); + result.push_back(XMLNode(iter)); } return result; } //----------------------------------------------------------------------------- // Get name of this node -std::string XMLNode::getName() +std::string XMLNode::getName() const { return fDOMElement->name(); } //----------------------------------------------------------------------------- // Get node content - STRING -string XMLNode::getContent() +string XMLNode::getContent() const { return fDOMElement->value(); } //----------------------------------------------------------------------------- // Get node content - NUMERICAL -float32 XMLNode::getContentNumerical() +float32 XMLNode::getContentNumerical() const { return boost::lexical_cast<float32>(getContent()); } //----------------------------------------------------------------------------- // Get node content - BOOLEAN -bool XMLNode::getContentBool() +bool XMLNode::getContentBool() const { string res = getContent(); return ((res == "1") || (res == "yes") || (res == "true") || (res == "on")); @@ -160,21 +146,20 @@ bool XMLNode::getContentBool() //----------------------------------------------------------------------------- // Get node content - STRING LIST -vector<string> XMLNode::getContentArray() +vector<string> XMLNode::getContentArray() const { // get listsize int iSize = boost::lexical_cast<int>(getAttribute("listsize")); // create result array vector<string> res(iSize); // loop all list item nodes - list<XMLNode*> nodes = getNodes("ListItem"); - for (list<XMLNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - string sValue = (*it)->getAttribute("value"); + list<XMLNode> nodes = getNodes("ListItem"); + for (list<XMLNode>::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + string sValue = it->getAttribute("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = sValue; } - deleteNodes(nodes); // return return res; @@ -182,7 +167,7 @@ vector<string> XMLNode::getContentArray() //----------------------------------------------------------------------------- // Get node content - NUMERICAL LIST -vector<float32> XMLNode::getContentNumericalArray() +vector<float32> XMLNode::getContentNumericalArray() const { // is scalar if (!hasAttribute("listsize")) { @@ -195,19 +180,18 @@ vector<float32> XMLNode::getContentNumericalArray() // create result array vector<float32> res(iSize); // loop all list item nodes - list<XMLNode*> nodes = getNodes("ListItem"); - for (list<XMLNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - float32 fValue = (*it)->getAttributeNumerical("value"); + list<XMLNode> nodes = getNodes("ListItem"); + for (list<XMLNode>::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + float32 fValue = it->getAttributeNumerical("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = fValue; } - deleteNodes(nodes); // return return res; } -vector<double> XMLNode::getContentNumericalArrayDouble() +vector<double> XMLNode::getContentNumericalArrayDouble() const { // is scalar if (!hasAttribute("listsize")) { @@ -220,21 +204,20 @@ vector<double> XMLNode::getContentNumericalArrayDouble() // create result array vector<double> res(iSize); // loop all list item nodes - list<XMLNode*> nodes = getNodes("ListItem"); - for (list<XMLNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - double fValue = (*it)->getAttributeNumericalDouble("value"); + list<XMLNode> nodes = getNodes("ListItem"); + for (list<XMLNode>::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + double fValue = it->getAttributeNumericalDouble("value"); ASTRA_ASSERT(iIndex < iSize); res[iIndex] = fValue; } - deleteNodes(nodes); // return return res; } //----------------------------------------------------------------------------- // Get node content - NUMERICAL LIST 2 -void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) +void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) const { // is scalar if (!hasAttribute("listsize")) { @@ -248,19 +231,18 @@ void XMLNode::getContentNumericalArray(float32*& _pfData, int& _iSize) // create result array _pfData = new float32[_iSize]; // loop all list item nodes - list<XMLNode*> nodes = getNodes("ListItem"); - for (list<XMLNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) { - int iIndex = (*it)->getAttributeNumerical("index"); - float32 fValue = (*it)->getAttributeNumerical("value"); + list<XMLNode> nodes = getNodes("ListItem"); + for (list<XMLNode>::iterator it = nodes.begin(); it != nodes.end(); it++) { + int iIndex = it->getAttributeNumerical("index"); + float32 fValue = it->getAttributeNumerical("value"); ASTRA_ASSERT(iIndex < _iSize); _pfData[iIndex] = fValue; } - deleteNodes(nodes); } //----------------------------------------------------------------------------- // Is attribute? -bool XMLNode::hasAttribute(string _sName) +bool XMLNode::hasAttribute(string _sName) const { xml_attribute<> *attr = fDOMElement->first_attribute(_sName.c_str()); return (attr != 0); @@ -268,7 +250,7 @@ bool XMLNode::hasAttribute(string _sName) //----------------------------------------------------------------------------- // Get attribute - STRING -string XMLNode::getAttribute(string _sName, string _sDefaultValue) +string XMLNode::getAttribute(string _sName, string _sDefaultValue) const { xml_attribute<> *attr = fDOMElement->first_attribute(_sName.c_str()); @@ -279,12 +261,12 @@ string XMLNode::getAttribute(string _sName, string _sDefaultValue) //----------------------------------------------------------------------------- // Get attribute - NUMERICAL -float32 XMLNode::getAttributeNumerical(string _sName, float32 _fDefaultValue) +float32 XMLNode::getAttributeNumerical(string _sName, float32 _fDefaultValue) const { if (!hasAttribute(_sName)) return _fDefaultValue; return boost::lexical_cast<float32>(getAttribute(_sName)); } -double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue) +double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue) const { if (!hasAttribute(_sName)) return _fDefaultValue; return boost::lexical_cast<double>(getAttribute(_sName)); @@ -292,7 +274,7 @@ double XMLNode::getAttributeNumericalDouble(string _sName, double _fDefaultValue //----------------------------------------------------------------------------- // Get attribute - BOOLEAN -bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) +bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) const { if (!hasAttribute(_sName)) return _bDefaultValue; string res = getAttribute(_sName); @@ -301,7 +283,7 @@ bool XMLNode::getAttributeBool(string _sName, bool _bDefaultValue) //----------------------------------------------------------------------------- // Has option? -bool XMLNode::hasOption(string _sKey) +bool XMLNode::hasOption(string _sKey) const { xml_node<> *iter; for (iter = fDOMElement->first_node("Option"); iter; iter = iter->next_sibling("Option")) { @@ -314,7 +296,7 @@ bool XMLNode::hasOption(string _sKey) //----------------------------------------------------------------------------- // Get option - STRING -string XMLNode::getOption(string _sKey, string _sDefaultValue) +string XMLNode::getOption(string _sKey, string _sDefaultValue) const { xml_node<> *iter; for (iter = fDOMElement->first_node("Option"); iter; iter = iter->next_sibling("Option")) { @@ -331,7 +313,7 @@ string XMLNode::getOption(string _sKey, string _sDefaultValue) //----------------------------------------------------------------------------- // Get option - NUMERICAL -float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) +float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) const { if (!hasOption(_sKey)) return _fDefaultValue; return boost::lexical_cast<float32>(getOption(_sKey)); @@ -339,7 +321,7 @@ float32 XMLNode::getOptionNumerical(string _sKey, float32 _fDefaultValue) //----------------------------------------------------------------------------- // Get option - BOOL -bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) +bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) const { bool bHasOption = hasOption(_sKey); if (!bHasOption) return _bDefaultValue; @@ -349,20 +331,18 @@ bool XMLNode::getOptionBool(string _sKey, bool _bDefaultValue) //----------------------------------------------------------------------------- // Get option - NUMERICAL ARRAY -vector<float32> XMLNode::getOptionNumericalArray(string _sKey) +vector<float32> XMLNode::getOptionNumericalArray(string _sKey) const { if (!hasOption(_sKey)) return vector<float32>(); - list<XMLNode*> nodes = getNodes("Option"); - for (list<XMLNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) { - if ((*it)->getAttribute("key") == _sKey) { - vector<float32> vals = (*it)->getContentNumericalArray(); - deleteNodes(nodes); + list<XMLNode> nodes = getNodes("Option"); + for (list<XMLNode>::iterator it = nodes.begin(); it != nodes.end(); it++) { + if (it->getAttribute("key") == _sKey) { + vector<float32> vals = it->getContentNumericalArray(); return vals; } } - deleteNodes(nodes); return vector<float32>(); } @@ -385,41 +365,40 @@ vector<float32> XMLNode::getOptionNumericalArray(string _sKey) //----------------------------------------------------------------------------- // Add child node - EMPTY -XMLNode* XMLNode::addChildNode(string _sNodeName) +XMLNode XMLNode::addChildNode(string _sNodeName) { xml_document<> *doc = fDOMElement->document(); char *node_name = doc->allocate_string(_sNodeName.c_str()); xml_node<> *node = doc->allocate_node(node_element, node_name); fDOMElement->append_node(node); - // TODO: clean up: this 'new' requires callers to do memory management - return new XMLNode(node); + return XMLNode(node); } //----------------------------------------------------------------------------- // Add child node - STRING -XMLNode* XMLNode::addChildNode(string _sNodeName, string _sText) +XMLNode XMLNode::addChildNode(string _sNodeName, string _sText) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_sText); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_sText); return res; } //----------------------------------------------------------------------------- // Add child node - FLOAT -XMLNode* XMLNode::addChildNode(string _sNodeName, float32 _fValue) +XMLNode XMLNode::addChildNode(string _sNodeName, float32 _fValue) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_fValue); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_fValue); return res; } //----------------------------------------------------------------------------- // Add child node - LIST -XMLNode* XMLNode::addChildNode(string _sNodeName, float32* _pfList, int _iSize) +XMLNode XMLNode::addChildNode(string _sNodeName, float32* _pfList, int _iSize) { - XMLNode* res = addChildNode(_sNodeName); - res->setContent(_pfList, _iSize); + XMLNode res = addChildNode(_sNodeName); + res.setContent(_pfList, _iSize); return res; } @@ -472,20 +451,18 @@ void XMLNode::addAttribute(string _sName, float32 _fValue) // Add option - STRING void XMLNode::addOption(string _sName, string _sText) { - XMLNode* node = addChildNode("Option"); - node->addAttribute("key", _sName); - node->addAttribute("value", _sText); - delete node; + XMLNode node = addChildNode("Option"); + node.addAttribute("key", _sName); + node.addAttribute("value", _sText); } //----------------------------------------------------------------------------- // Add option - FLOAT void XMLNode::addOption(string _sName, float32 _sText) { - XMLNode* node = addChildNode("Option"); - node->addAttribute("key", _sName); - node->addAttribute("value", _sText); - delete node; + XMLNode node = addChildNode("Option"); + node.addAttribute("key", _sName); + node.addAttribute("value", _sText); } //----------------------------------------------------------------------------- |