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

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

Préparation nouvelle arborescence

File size: 6.7 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            if ((*it).first.compare(StdString("id")) != 0 &&
99                (*it).first.compare(StdString("src"))!= 0)
100            {
101               //if (CAttributeMap::operator[]((*it).first)->isEmpty())
102               CAttributeMap::operator[]((*it).first)->fromString((*it).second);
103            }
104      }
105     
106      //---------------------------------------------------------------
107     
108      void CAttributeMap::setAttributes(const CAttributeMap * const _parent)
109      {
110         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
111         
112         SuperClassMap::const_iterator it = _parent->begin(), end = _parent->end();
113         for (; it != end; it++)
114         {
115            const StdStrAttPair & el = *it;
116            if (this->hasAttribute(el.first))
117            {
118               CAttribute * currAtt = CAttributeMap::operator[](el.first);
119               if (currAtt->isEmpty() && !el.second->isEmpty())
120               {
121                  this->setAttribute(el.first, el.second);
122               }
123            }
124         }
125      }
126     
127      //---------------------------------------------------------------
128     
129      void CAttributeMap::toBinary(StdOStream & os) const
130      {
131         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
132         SuperClassMap::const_iterator it = this->begin(), end = this->end();
133         
134         const StdSize nbatt = SuperClassMap::size();
135         os.write (reinterpret_cast<const char*>(&nbatt) , sizeof(StdSize));
136         
137         for (; it != end; it++)
138         {
139            const StdString & key   = it->first;
140            const CAttribute* value = it->second;           
141            const StdSize size = key.size();
142           
143            os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
144            os.write (key.data(), size * sizeof(char));
145           
146            if (!value->isEmpty())
147            {
148               bool b = true;
149               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
150               value->toBinary(os);
151            }
152            else 
153            {
154               bool b = false;
155               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
156            }
157         }
158      }
159     
160      //---------------------------------------------------------------
161     
162      void CAttributeMap::fromBinary(StdIStream & is)
163      {
164         StdSize nbatt = 0;
165         is.read (reinterpret_cast<char*>(&nbatt), sizeof(StdSize));
166         
167         for (StdSize i = 0; i < nbatt; i++)
168         {
169            bool hasValue = false;
170            StdSize size  = 0;
171            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
172            StdString key(size, ' ');
173            is.read (const_cast<char *>(key.data()), size * sizeof(char));
174           
175            if (!this->hasAttribute(key))
176               ERROR("CAttributeMap::fromBinary(StdIStream & is)",
177                     << "[ key = " << key << "] key not found !");
178                                       
179            is.read (reinterpret_cast<char*>(&hasValue), sizeof(bool));
180           
181            if (hasValue)         
182               this->operator[](key)->fromBinary(is);
183         }
184      }
185     
186      ///--------------------------------------------------------------
187
188   } // namespace tree
189} // namespace xmlioser
Note: See TracBrowser for help on using the repository browser.