source: XMLIO_V2/dev/dev_rv/src/xmlio/attribute_map.cpp @ 171

Last change on this file since 171 was 171, checked in by hozdoba, 13 years ago
File size: 6.3 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      bool CAttributeMap::hasAttribute(const StdString & key) const
20      { 
21         return (this->find(key) != this->end()); 
22      }
23     
24      //---------------------------------------------------------------
25     
26      void CAttributeMap::setAttribute(const StdString & key, CAttribute * const attr)
27      {
28         if (!this->hasAttribute(key))
29            ERROR("CAttributeMap::setAttribute(key, attr)",
30                   << "[ key = " << key << "] key not found !");
31         if (attr == NULL)
32            ERROR("CAttributeMap::setAttribute(key, attr)",
33                   << "[ key = " << key << "] attr is null !");
34         this->find(key)->second->setAnyValue(attr->getAnyValue());
35      }
36     
37      //---------------------------------------------------------------
38     
39      CAttribute * CAttributeMap::operator[](const StdString & key)
40      {
41         if (!this->hasAttribute(key))
42            ERROR("CAttributeMap::operator[](const StdString & key)",
43                  << "[ key = " << key << "] key not found !");
44         return (SuperClassMap::operator[](key));
45      }
46     
47      //---------------------------------------------------------------
48     
49      StdString CAttributeMap::toString(void) const
50      {
51         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
52         StdOStringStream oss;
53         
54         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
55         for (; it != end; it++)
56         {
57            const StdStrAttPair & att = *it;
58            if (!att.second->isEmpty())
59               oss << *att.second << " ";
60         }
61         return (oss.str());
62      }
63     
64      //---------------------------------------------------------------
65     
66      void CAttributeMap::fromString(const StdString & str)
67      { 
68         ERROR("CAttributeMap::fromString(const StdString & str)",
69               << "[ str = " << str << "] Not implemented yet !"); 
70      }
71     
72      //---------------------------------------------------------------
73
74      //StdOStream & operator << (StdOStream & os, const CAttributeMap & attributmap)
75      //{ os << attributmap.toString(); return (os); }
76     
77      //---------------------------------------------------------------
78     
79      void CAttributeMap::setAttributes(const xml::THashAttributes & attributes)
80      {
81         for (xml::THashAttributes::const_iterator it  = attributes.begin();
82                                                   it != attributes.end();
83                                                   it ++)
84            if ((*it).first.compare(StdString("id")) != 0 &&
85                (*it).first.compare(StdString("src"))!= 0)
86            {
87               //if (CAttributeMap::operator[]((*it).first)->isEmpty())
88               CAttributeMap::operator[]((*it).first)->fromString((*it).second);
89            }
90      }
91     
92      //---------------------------------------------------------------
93     
94      void CAttributeMap::setAttributes(const CAttributeMap * const _parent)
95      {
96         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
97         
98         SuperClassMap::const_iterator it = _parent->begin(), end = _parent->end();
99         for (; it != end; it++)
100         {
101            const StdStrAttPair & el = *it;
102            if (this->hasAttribute(el.first))
103            {
104               CAttribute * currAtt = CAttributeMap::operator[](el.first);
105               if (currAtt->isEmpty() && !el.second->isEmpty())
106               {
107                  this->setAttribute(el.first, el.second);
108               }
109            }
110         }
111      }
112     
113      //---------------------------------------------------------------
114     
115      void CAttributeMap::toBinary(StdOStream & os) const
116      {
117         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
118         SuperClassMap::const_iterator it = this->begin(), end = this->end();
119         
120         const StdSize nbatt = SuperClassMap::size();
121         os.write (reinterpret_cast<const char*>(&nbatt) , sizeof(StdSize));
122         
123         for (; it != end; it++)
124         {
125            const StdString & key   = it->first;
126            const CAttribute* value = it->second;           
127            const StdSize size = key.size();
128           
129            os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
130            os.write (key.data(), size * sizeof(char));
131           
132            if (!value->isEmpty())
133            {
134               bool b = true;
135               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
136               value->toBinary(os);
137            }
138            else 
139            {
140               bool b = false;
141               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
142            }
143         }
144      }
145     
146      //---------------------------------------------------------------
147     
148      void CAttributeMap::fromBinary(StdIStream & is)
149      {
150         StdSize nbatt = 0;
151         is.read (reinterpret_cast<char*>(&nbatt), sizeof(StdSize));
152         
153         for (StdSize i = 0; i < nbatt; i++)
154         {
155            bool hasValue = false;
156            StdSize size  = 0;
157            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
158            StdString key(size, ' ');
159            is.read (const_cast<char *>(key.data()), size * sizeof(char));
160           
161            if (!this->hasAttribute(key))
162               ERROR("CAttributeMap::fromBinary(StdIStream & is)",
163                     << "[ key = " << key << "] key not found !"); 
164                     
165            is.read (reinterpret_cast<char*>(&hasValue), sizeof(bool));
166           
167            if (hasValue)         
168               this->operator[](key)->fromBinary(is);
169         }
170      }
171     
172      ///--------------------------------------------------------------
173
174   } // namespace tree
175} // namespace xmlioser
Note: See TracBrowser for help on using the repository browser.