source: XIOS/trunk/src/node/variable.hpp @ 527

Last change on this file since 527 was 527, checked in by rlacroix, 9 years ago

Make the variable typing stricter.

The type must now be one of:

  • bool
  • int or int32
  • int16
  • int64
  • float
  • double
  • string

WARNING: The variable type was previously not checked and using an unsupported type did not lead to any error message. Be aware that this change can make your existing configuration files invalid. However the adjustments required to adapt existing files should be minor.

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 4.0 KB
Line 
1#ifndef __XMLIO_CVariable__
2#define __XMLIO_CVariable__
3
4/// xios headers ///
5#include "xmlioserver_spl.hpp"
6#include "declare_group.hpp"
7#include "group_template.hpp"
8#include "array_new.hpp"
9#include "attribute_enum.hpp"
10#include "attribute_enum_impl.hpp"
11
12namespace xios
13{
14      /// ////////////////////// Déclarations ////////////////////// ///
15
16      class CVariableGroup;
17      class CVariableAttributes;
18      class CVariable;
19      class CContext;
20      ///--------------------------------------------------------------
21
22      // Declare/Define CVarAttribute
23      BEGIN_DECLARE_ATTRIBUTE_MAP(CVariable)
24#include "var_attribute.conf"
25      END_DECLARE_ATTRIBUTE_MAP(CVariable)
26
27      ///--------------------------------------------------------------
28
29      class CVariable
30         : public CObjectTemplate<CVariable>
31         , public CVariableAttributes
32      {
33            enum EEventId
34            {
35             EVENT_ID_VARIABLE_VALUE
36            };
37
38            /// typedef ///
39            typedef CObjectTemplate<CVariable>   SuperClass;
40            typedef CVariableAttributes SuperClassAttribute;
41
42            friend class CVariableGroup;
43
44         public :
45
46            typedef CVariableAttributes RelAttributes;
47            typedef CVariableGroup      RelGroup;
48
49            /// Constructeurs ///
50            CVariable(void);
51            explicit CVariable(const StdString & id);
52            CVariable(const CVariable & var);       // Not implemented yet.
53            CVariable(const CVariable * const var); // Not implemented yet.
54
55            /// Destructeur ///
56            virtual ~CVariable(void);
57
58         public :
59            /// Autres ///
60            virtual void parse(xml::CXMLNode & node);
61            virtual StdString toString(void) const;
62
63            /// Accesseur ///
64            const StdString & getContent (void) const;
65
66            void setContent(const StdString& content);
67
68
69            template <typename T> inline T getData(void) const;
70            template <typename T> inline void setData(T data);
71
72            template <typename T, StdSize N>
73            inline void getData(CArray<T, N>& _data_array) const;
74
75            static bool dispatchEvent(CEventServer& event) ;
76
77            //! Sending a request to set up variable data
78            void sendValue();
79
80            static void recvValue(CEventServer& event) ;
81            void recvValue(CBufferIn& buffer) ;
82
83         public :
84
85            /// Accesseurs statiques ///
86            static StdString GetName(void);
87            static StdString GetDefName(void);
88            static ENodeType GetType(void);
89
90         private :
91
92            StdString content;
93
94      }; // class CVar
95
96      template<>
97      inline bool CVariable::getData(void) const
98      {
99         if (content.compare("true")==0 || content.compare(".true.")==0 || content.compare(".TRUE.")==0) return true ;
100         else if (content.compare("false")==0 || content.compare(".false.")==0 || content.compare(".FALSE.")==0) return false ;
101         else ERROR("CVariable::getdata()",
102               << "Cannot convert string <" << content << "> into type required" );
103         return false ;
104      }
105
106      template <typename T>
107      inline T CVariable::getData(void) const
108      {
109         T retval ;
110         std::stringstream sstr(std::stringstream::in | std::stringstream::out);
111         sstr<<content ;
112         sstr>>retval ;
113         if (sstr.fail()) ERROR("CVariable::getdata()",
114               << "Cannot convert string <" << content << "> into type required" );
115         return retval ;
116      }
117
118      template<>
119      inline void CVariable::setData(bool data)
120      {
121        if (true == data) content.assign("true");
122        else content.assign("false");
123      }
124
125      template <typename T>
126      inline void CVariable::setData(T data)
127      {
128        std::stringstream sstr;
129        sstr<<data;
130        content = sstr.str();
131      }
132
133      ///--------------------------------------------------------------
134
135      // Declare/Define CVarGroup and CVarDefinition
136      DECLARE_GROUP_PARSE_REDEF(CVariable);
137} // namespace xios
138
139#endif // __XMLIO_CVariable__
Note: See TracBrowser for help on using the repository browser.