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

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

Préparation nouvelle arborescence

File size: 7.1 KB
Line 
1#include "attribute_template.hpp"
2#include "attribute_template_impl.hpp"
3
4#include <cfloat>
5
6namespace xmlioserver
7{
8   namespace tree
9   {
10      /// ////////////////////// Définitions ////////////////////// ///
11
12      /** Spécialisations des templates pour la fonction [toString] **/
13     
14      template <>
15         StdString CAttributeTemplate<bool>::toString(void) const
16      {
17         StdOStringStream oss;
18         if (!this->isEmpty() && this->hasId())
19         {
20            if (this->getValue())
21               oss << this->getName() << "=\".TRUE.\"";
22            else
23               oss << this->getName() << "=\".FALSE.\"";
24         }
25         return (oss.str());
26      }
27
28      //---------------------------------------------------------------
29
30      /** Spécialisations des templates pour la fonction [fromString] **/
31
32      template <> // Chaîne de caractÚres.
33         void CAttributeTemplate<StdString>::fromString(const StdString & str)
34      { 
35         this->setValue(str); 
36      }
37
38      template <> // Entier
39         void CAttributeTemplate<int>::fromString(const StdString & str)
40      {
41         try
42         {
43            this->setValue(boost::lexical_cast<int>(str));
44         }
45         catch(boost::bad_lexical_cast &)
46         {
47            ERROR("void CAttributeTemplate<int>::fromString(const StdString & str)",
48                  << "[ str = " << str << " ] Bad cast !");
49         }
50      }
51
52      template <> // Double
53         void CAttributeTemplate<double>::fromString(const StdString & str)
54      {
55         if (str.find("max") != StdString::npos)
56         {
57            this->setValue(DBL_MAX);
58            return;
59         }
60         if (str.find("min") != StdString::npos)
61         {
62            this->setValue(DBL_MIN);
63            return;
64         }
65         
66         try
67         {
68            this->setValue(boost::lexical_cast<double>(str));
69         }
70         catch(boost::bad_lexical_cast &)
71         {
72            ERROR("void CAttributeTemplate<double>::fromString(const StdString & str)",
73                  << "[ str = " << str << " ] Bad cast !");
74         }
75      }
76
77      template <> // Booléen
78         void CAttributeTemplate<bool>::fromString(const StdString & str)
79      {
80         if (str.find(".TRUE.") != StdString::npos)
81            this->setValue(true);
82         else
83            this->setValue(false);
84      }
85
86      //---------------------------------------------------------------
87
88      template<> // Tableau
89         void CAttributeTemplate<ARRAY(double, 1)>::fromString(const StdString & str)
90      {
91         ARRAY_CREATE(array_sptr, double, 1, [1]);
92         CArray<double, 1> & array = *array_sptr;
93         this->setValue(array_sptr);
94
95         StdIStringStream iss(str) ;
96         char c = '\0'; int size = 0;
97         double d = 0.,valsup = 0., valinf = 0.;
98         std::vector<double> vect;
99
100         iss >> d; vect.push_back(d);
101         if (!iss.eof ())
102         {
103            iss >> c;
104            switch (c)
105            {
106               case ',' : // Le tableau est généré valeur par valeur.
107                  iss.unget();
108                  while(!iss.eof ())
109                  { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide.
110                     iss >> c >> d;
111                     if (c != ',')
112                        ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
113                              << "[ str = " << str << " ] bad definition of array !");
114                     vect.push_back(d);
115                  }
116                  size = vect.size();
117                  break;
118               case '(' : // Le tableau est généré automatiquement.
119                  if (!iss.eof ())
120                  { // on récupÚre la borne supérieure
121                     valinf = d;
122                     iss >> size >> c >> d;
123                     if ((c != ')') || (size <= 0))
124                        ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
125                              << "[ str = " << str << " ] bad definition of array !");
126                     valsup = d;
127                  }
128                  d = (valsup - valinf) / (double)(size - 1);
129                  for (int j = 1; j <= size; j++)
130                     vect.push_back(valinf + j * d);
131                  break;
132               default :
133                  ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
134                        << "[ str = " << str << " ] bad definition of array !");
135            }
136         }
137
138         array.resize(boost::extents[size]);
139         for (int i = 0; i < size; i++)
140            array[i] = vect[i];
141
142      }
143
144      //---------------------------------------------------------------
145
146      /** Spécialisations des templates pour la fonction [toBinary] **/
147
148      template <> // Chaîne de caractÚres.
149         void CAttributeTemplate<StdString>::toBinary (StdOStream & os) const
150      {
151         StdString str = this->getValue();
152         StdSize size = str.size();
153         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
154         os.write (str.data(), size * sizeof(char));
155      }
156
157      template <> // Entier
158         void CAttributeTemplate<int>::toBinary(StdOStream & os) const
159      {
160         int value = this->getValue();
161         os.write (reinterpret_cast<const char*>(&value) , sizeof(int));
162      }
163
164      template <> // Booléen
165         void CAttributeTemplate<bool>::toBinary(StdOStream & os) const
166      {
167         bool value = this->getValue();
168         os.write (reinterpret_cast<const char*>(&value) , sizeof(bool));
169      }
170
171      template <> // Double
172         void CAttributeTemplate<double>::toBinary(StdOStream & os) const
173      {
174         double value = this->getValue();
175         os.write (reinterpret_cast<const char*>(&value) , sizeof(double));
176      }
177
178      //---------------------------------------------------------------
179
180      /** Spécialisations des templates pour la fonction [fromBinary] **/
181
182      template <> // Chaîne de caractÚres.
183         void CAttributeTemplate<StdString>::fromBinary(StdIStream & is)
184      {
185         StdSize size = 0;
186         is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
187         StdString value(size, ' ');
188         is.read (const_cast<char *>(value.data()), size * sizeof(char));
189         this->setValue(value);
190      }
191
192      template <> // Entier
193         void CAttributeTemplate<int>::fromBinary(StdIStream & is)
194      {
195         int value = 0;
196         is.read (reinterpret_cast<char*>(&value), sizeof(int));
197         this->setValue(value);
198      }
199
200      template <> // Booléen
201         void CAttributeTemplate<bool>::fromBinary(StdIStream & is)
202      {
203         bool value = false;
204         is.read (reinterpret_cast<char*>(&value), sizeof(bool));
205         this->setValue(value);
206      }
207
208      template <> // Double
209         void CAttributeTemplate<double>::fromBinary(StdIStream & is)
210      {
211         double value = 0.;
212         is.read (reinterpret_cast<char*>(&value), sizeof(double));
213         this->setValue(value);
214      }
215
216      ///--------------------------------------------------------------
217   } // namespace tree
218} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.