source: XMLIO_V2/dev/common/src/xmlio/attribute_map.cpp @ 278

Last change on this file since 278 was 278, checked in by hozdoba, 13 years ago

Corrections

File size: 6.8 KB
Line 
1#include "attribute_map.hpp"
2
3namespace xmlioserver
4{
5   namespace tree
6   {
7      /// ////////////////////// Définitions ////////////////////// ///
8      CAttributeMap * CAttributeMap::Current = NULL;
9
10      CAttributeMap::CAttributeMap(void)
11         : xios_map<StdString, CAttribute*>()
12      { CAttributeMap::Current = this; }
13
14      CAttributeMap::~CAttributeMap(void)
15      { /* Ne rien faire de plus */ }
16     
17      ///--------------------------------------------------------------
18
19      void CAttributeMap::clearAllAttributes(void)
20      {
21         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
22         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
23         for (; it != end; it++)
24         {
25            const StdStrAttPair & att = *it;
26            if (!att.second->isEmpty())
27               att.second->clear();
28         }
29      }
30
31      //---------------------------------------------------------------
32
33      bool CAttributeMap::hasAttribute(const StdString & key) const
34      { 
35         return (this->find(key) != this->end()); 
36      }
37     
38      //---------------------------------------------------------------
39     
40      void CAttributeMap::setAttribute(const StdString & key, CAttribute * const attr)
41      {
42         if (!this->hasAttribute(key))
43            ERROR("CAttributeMap::setAttribute(key, attr)",
44                   << "[ key = " << key << "] key not found !");
45         if (attr == NULL)
46            ERROR("CAttributeMap::setAttribute(key, attr)",
47                   << "[ key = " << key << "] attr is null !");
48         this->find(key)->second->setAnyValue(attr->getAnyValue());
49      }
50     
51      //---------------------------------------------------------------
52     
53      CAttribute * CAttributeMap::operator[](const StdString & key)
54      {
55         if (!this->hasAttribute(key))
56            ERROR("CAttributeMap::operator[](const StdString & key)",
57                  << "[ key = " << key << "] key not found !");
58         return (SuperClassMap::operator[](key));
59      }
60     
61      //---------------------------------------------------------------
62     
63      StdString CAttributeMap::toString(void) const
64      {
65         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
66         StdOStringStream oss;
67         
68         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
69         for (; it != end; it++)
70         {
71            const StdStrAttPair & att = *it;
72            if (!att.second->isEmpty())
73               oss << *att.second << " ";
74         }
75         return (oss.str());
76      }
77     
78      //---------------------------------------------------------------
79     
80      void CAttributeMap::fromString(const StdString & str)
81      { 
82         ERROR("CAttributeMap::fromString(const StdString & str)",
83               << "[ str = " << str << "] Not implemented yet !"); 
84      }
85     
86      //---------------------------------------------------------------
87
88      //StdOStream & operator << (StdOStream & os, const CAttributeMap & attributmap)
89      //{ os << attributmap.toString(); return (os); }
90     
91      //---------------------------------------------------------------
92     
93      void CAttributeMap::setAttributes(const xml::THashAttributes & attributes)
94      {
95         for (xml::THashAttributes::const_iterator it  = attributes.begin();
96                                                   it != attributes.end();
97                                                   it ++)
98         {
99            if ((*it).first.compare(StdString("id")) != 0 &&
100                (*it).first.compare(StdString("src"))!= 0)
101            {
102               //if (CAttributeMap::operator[]((*it).first)->isEmpty())
103               CAttributeMap::operator[]((*it).first)->fromString((*it).second);
104            }
105         }
106      }
107     
108      //---------------------------------------------------------------
109     
110      void CAttributeMap::setAttributes(const CAttributeMap * const _parent)
111      {
112         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
113         
114         SuperClassMap::const_iterator it = _parent->begin(), end = _parent->end();
115         for (; it != end; it++)
116         {
117            const StdStrAttPair & el = *it;
118            if (this->hasAttribute(el.first))
119            {
120               CAttribute * currAtt = CAttributeMap::operator[](el.first);
121               if (currAtt->isEmpty() && !el.second->isEmpty())
122               {
123                  this->setAttribute(el.first, el.second);
124               }
125            }
126         }
127      }
128     
129      //---------------------------------------------------------------
130     
131      void CAttributeMap::toBinary(StdOStream & os) const
132      {
133         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
134         SuperClassMap::const_iterator it = this->begin(), end = this->end();
135         
136         const StdSize nbatt = SuperClassMap::size();
137         os.write (reinterpret_cast<const char*>(&nbatt) , sizeof(StdSize));
138         
139         for (; it != end; it++)
140         {
141            const StdString & key   = it->first;
142            const CAttribute* value = it->second;           
143            const StdSize size = key.size();
144           
145            os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
146            os.write (key.data(), size * sizeof(char));
147           
148            if (!value->isEmpty())
149            {
150               bool b = true;
151               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
152               value->toBinary(os);
153            }
154            else 
155            {
156               bool b = false;
157               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
158            }
159         }
160      }
161     
162      //---------------------------------------------------------------
163     
164      void CAttributeMap::fromBinary(StdIStream & is)
165      {
166         StdSize nbatt = 0;
167         is.read (reinterpret_cast<char*>(&nbatt), sizeof(StdSize));
168         
169         for (StdSize i = 0; i < nbatt; i++)
170         {
171            bool hasValue = false;
172            StdSize size  = 0;
173            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
174            StdString key(size, ' ');
175            is.read (const_cast<char *>(key.data()), size * sizeof(char));
176           
177            if (!this->hasAttribute(key))
178               ERROR("CAttributeMap::fromBinary(StdIStream & is)",
179                     << "[ key = " << key << "] key not found !");
180                                       
181            is.read (reinterpret_cast<char*>(&hasValue), sizeof(bool));
182           
183            if (hasValue)         
184               this->operator[](key)->fromBinary(is);
185         }
186      }
187     
188      ///--------------------------------------------------------------
189
190   } // namespace tree
191} // namespace xmlioser
Note: See TracBrowser for help on using the repository browser.