source: XMLIO_V2/dev/dev_rv/src/attribute_template.cpp @ 141

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

Mise à jour depuis un autre dépôt

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