source: XMLIO_V2/dev/dev_rv/src4/xmlio/attribute/attribute_map.cpp @ 257

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

Ajout de classes pour la version 4

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