Ignore:
Timestamp:
06/04/10 14:55:53 (14 years ago)
Author:
hozdoba
Message:

Suppression de nombreuses fuites mémoires (200kbytes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XMLIO_V2/dev/dev_rv/xmlio_xml_node.hpp

    r91 r96  
    99#include <Poco/DOM/NamedNodeMap.h> 
    1010 
    11 //#include <Poco/DOM/AutoPtr.h> 
     11#include <Poco/DOM/AutoPtr.h> 
    1212 
    1313// Entêtes Poco SAX 
     
    3838using Poco::HashMap; 
    3939 
     40using Poco::XML::AutoPtr; 
     41 
    4042namespace XMLIOSERVER 
    4143{ 
     
    4648          
    4749      // TODO Mettre des auto_ptr ici car gestion de la mémoire lamentable sans. 
    48       typedef Document* PDocument; 
    49       typedef Node*     PNode; 
     50      typedef AutoPtr<Document> PDocument; 
     51      typedef Node*    PNode; 
    5052          
    5153      class XMLNode 
    5254      { 
    53          protected : 
    54              
    55             XMLNode(const string& _rootName) : rootName(_rootName) { this->cNode = NULL ; } 
    56              
    5755         public : 
    5856             
    59             static XMLNode CreateNode(istream& _istr, const string& _rootName = string("simulation")) 
     57            XMLNode(const string& _rootName) : rootName(_rootName)  
     58            { /* Ne rien faire de plus */}           
     59 
     60            static XMLNode& CreateNode(XMLNode& node, istream& _istr, const string& _rootName = string("simulation")) 
    6061            { 
    61                XMLNode node(_rootName); 
    62                    
    6362               if ((_istr.good())) 
    6463               { // S'il est possible de lire le flux en entrée ... 
     
    6867                  // On parse la source XML et on vérifie que le premier noeud (racine) est du type "Element" 
    6968                  // ... et à pour valeur la chaîne rootName. 
    70                   PDocument pDoc = parser.parse(&src); 
    71                   if (!(pDoc->documentElement()->nodeName().compare(_rootName))) 
     69                  node.pDoc = parser.parse(&src); 
     70                  if (!(node.pDoc->documentElement()->nodeName().compare(_rootName))) 
    7271                  { 
    73                      node.cNode = pDoc->documentElement(); 
     72                     node.setCNode(node.pDoc->documentElement()); 
    7473                     node.goToChildElement(); 
    7574                  } 
     
    7776                  { 
    7877                     ostringstream oss; 
    79                      oss << "L'élément racine doit avoir pour valeur <" << _rootName << "> (\"" <<  (pDoc->documentElement()->nodeName()) <<"\" lue)"; 
     78                     oss << "L'élément racine doit avoir pour valeur <" << _rootName << "> (\"" <<  (node.pDoc->documentElement()->nodeName()) <<"\" lue)"; 
    8079                     throw XMLParsingException(oss.str()); 
    8180                  }     
     
    8786            } 
    8887                
    89             string getElementName(void) const {return (this->cNode->nodeName());} 
     88            string getElementName(void) const {return (this->getCNode()->nodeName());} 
    9089                 
    91             bool goToNextElement(Node* nextElement = 0)  
     90            bool goToNextElement(void)  
    9291            { 
    93                nextElement = (!nextElement)? this->cNode->nextSibling() : nextElement; 
     92               PNode nextElement = this->getCNode()->nextSibling(); 
    9493               
    9594               // On parcourt la liste des "siblings" jusqu'à trouver un élément quelconque. 
    9695               for(; ; nextElement = nextElement->nextSibling()) 
    97                if (nextElement == NULL) break; 
    98                else if (nextElement->nodeType() == 1) 
    99                {// Si l'un des noeuds est un élément... 
    100                 this->cNode = nextElement ; 
    101                 return (true); 
    102                }  
    103               return (false); 
    104              } 
     96                  if (IsPtrNull(nextElement)) break; 
     97                  else if (nextElement->nodeType() == 1) 
     98                  {// Si l'un des noeuds est un élément... 
     99                     this->setCNode(nextElement) ; 
     100                     return (true); 
     101                  }  
     102               return (false); 
     103            } 
    105104              
    106              bool goToChildElement(void) 
    107              { 
    108               // On parcourt la liste des enfants jusqu'à trouver un élément quelconque. 
    109               if (this->cNode->firstChild()) 
    110                if (this->goToNextElement(this->cNode->firstChild())) 
    111                 return (true); 
     105            bool goToChildElement(void) 
     106            { 
     107               PNode nextElement = this->getCNode()->firstChild(); 
     108                
     109               // On parcourt la liste des enfants jusqu'à trouver un élément quelconque. 
     110               if (!IsPtrNull(nextElement)) 
     111               { 
     112                  for(; ; nextElement = nextElement->nextSibling()) 
     113                     if (IsPtrNull(nextElement)) break; 
     114                     else if (nextElement->nodeType() == 1) 
     115                     {// Si l'un des noeuds est un élément... 
     116                        this->setCNode(nextElement) ; 
     117                        return (true); 
     118                     } 
     119                  return (false); 
     120               } 
    112121                 
    113               return (false); 
    114              } 
     122               return (false); 
     123            } 
    115124              
    116              bool goToParentElement(void) 
    117              {  
    118               // Pas de retour au parent si on est à la racine. 
    119               if (!(this->getElementName().compare(rootName))) return (false); 
    120               this->cNode = this->cNode->parentNode(); 
    121               return (true); 
    122              } 
     125            bool goToParentElement(void) 
     126            {  
     127               // Pas de retour au parent si on est à la racine. 
     128               if (!(this->getElementName().compare(rootName))) return (false); 
     129               this->setCNode(this->getCNode()->parentNode()); 
     130               return (true); 
     131            } 
    123132              
    124              bool getAttributes(THashAttributes& attributes) const 
    125              { 
    126               if(!this->cNode->hasAttributes()) return (false); 
    127               NamedNodeMap* map = this->cNode->attributes(); 
     133            bool getAttributes(THashAttributes& attributes) const 
     134            { 
     135                 
     136               if(!this->getCNode()->hasAttributes()) return (false); 
     137               AutoPtr<NamedNodeMap> map = this->getCNode()->attributes(); 
    128138               
    129139              for(unsigned int i = 0; i< map->length(); i++) 
     
    131141 
    132142              return (true); 
    133              } 
     143            } 
    134144             
    135145            ~XMLNode()  
    136146            { /* Ne rien faire de plus */ } 
    137  
     147             
     148         protected : 
     149          
     150            PNode getCNode(void) const {return (this->cNode); } 
     151            void setCNode(PNode other) { this->cNode = other; } 
     152             
     153            static bool IsPtrNull(PNode ptr) {return (ptr==NULL);} 
     154             
    138155         private : 
    139                    
     156            PDocument pDoc;  
    140157            PNode  cNode; 
     158             
    141159            string rootName;       
    142160          
Note: See TracChangeset for help on using the changeset viewer.