source: XMLIO_V2/dev/dev_rv/src/xmlio/attribute_template.cpp @ 180

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