source: XMLIO_V2/dev/common/src/attribute_template.cpp @ 300

Last change on this file since 300 was 300, checked in by ymipsl, 12 years ago

nouvelle version de developpement de xios

  • nouvelle interface fortran
  • recodage complet de la couche de communication
  • et bien d'autres choses...

YM

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         size = vect.size();
102         if (!iss.eof ())
103         {
104            iss >> c;
105            switch (c)
106            {
107               case ',' : // Le tableau est généré valeur par valeur.
108                  iss.unget();
109                  while(!iss.eof ())
110                  { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide.
111                     iss >> c >> d;
112                     if (c != ',')
113                        ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
114                              << "[ str = " << str << " ] bad definition of array !");
115                     vect.push_back(d);
116                  }
117                  size = vect.size();
118                  break;
119               case '(' : // Le tableau est généré automatiquement.
120                  if (!iss.eof ())
121                  { // on récupÚre la borne supérieure
122                     valinf = d;
123                     iss >> size >> c >> d;
124                     if ((c != ')') || (size <= 0))
125                        ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
126                              << "[ str = " << str << " ] bad definition of array !");
127                     valsup = d;
128                  }
129                  d = (valsup - valinf) / (double)(size - 1);
130                  for (int j = 1; j <= size; j++)
131                     vect.push_back(valinf + j * d);
132                  break;
133               default :
134                  ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)",
135                        << "[ str = " << str << " ] bad definition of array !");
136            }
137         }
138
139         array.resize(boost::extents[size]);
140         for (int i = 0; i < size; i++)
141            array[i] = vect[i]; 
142
143      }
144
145      //---------------------------------------------------------------
146
147      /** Spécialisations des templates pour la fonction [toBinary] **/
148
149      template <> // Chaîne de caractÚres.
150         void CAttributeTemplate<StdString>::toBinary (StdOStream & os) const
151      {
152         StdString str = this->getValue();
153         StdSize size = str.size();
154         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
155         os.write (str.data(), size * sizeof(char));
156      }
157
158      template <> // Entier
159         void CAttributeTemplate<int>::toBinary(StdOStream & os) const
160      {
161         int value = this->getValue();
162         os.write (reinterpret_cast<const char*>(&value) , sizeof(int));
163      }
164
165      template <> // Booléen
166         void CAttributeTemplate<bool>::toBinary(StdOStream & os) const
167      {
168         bool value = this->getValue();
169         os.write (reinterpret_cast<const char*>(&value) , sizeof(bool));
170      }
171
172      template <> // Double
173         void CAttributeTemplate<double>::toBinary(StdOStream & os) const
174      {
175         double value = this->getValue();
176         os.write (reinterpret_cast<const char*>(&value) , sizeof(double));
177      }
178
179      //---------------------------------------------------------------
180
181      /** Spécialisations des templates pour la fonction [fromBinary] **/
182
183      template <> // Chaîne de caractÚres.
184         void CAttributeTemplate<StdString>::fromBinary(StdIStream & is)
185      {
186         StdSize size = 0;
187         is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
188         StdString value(size, ' ');
189         is.read (const_cast<char *>(value.data()), size * sizeof(char));
190         this->setValue(value);
191      }
192
193      template <> // Entier
194         void CAttributeTemplate<int>::fromBinary(StdIStream & is)
195      {
196         int value = 0;
197         is.read (reinterpret_cast<char*>(&value), sizeof(int));
198         this->setValue(value);
199      }
200
201      template <> // Booléen
202         void CAttributeTemplate<bool>::fromBinary(StdIStream & is)
203      {
204         bool value = false;
205         is.read (reinterpret_cast<char*>(&value), sizeof(bool));
206         this->setValue(value);
207      }
208
209      template <> // Double
210         void CAttributeTemplate<double>::fromBinary(StdIStream & is)
211      {
212         double value = 0.;
213         is.read (reinterpret_cast<char*>(&value), sizeof(double));
214         this->setValue(value);
215      }
216
217      ///--------------------------------------------------------------
218   } // namespace tree
219} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.