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

Last change on this file since 773 was 773, checked in by rlacroix, 8 years ago

File/Variable?: Add an helper function to get the output name.

  • 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.2 KB
Line 
1#ifndef __XIOS_CVariable__
2#define __XIOS_CVariable__
3
4/// XIOS headers ///
5#include "xios_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& getVariableOutputName(void) const;
65            const StdString & getContent (void) const;
66
67            void setContent(const StdString& content);
68
69
70            template <typename T> inline T getData(void) const;
71            template <typename T> inline void setData(T data);
72
73            template <typename T, StdSize N>
74            inline void getData(CArray<T, N>& _data_array) const;
75
76            static bool dispatchEvent(CEventServer& event) ;
77
78            //! Sending a request to set up variable data
79            void sendValue();
80
81            static void recvValue(CEventServer& event) ;
82            void recvValue(CBufferIn& buffer) ;
83
84         public :
85
86            /// Accesseurs statiques ///
87            static StdString GetName(void);
88            static StdString GetDefName(void);
89            static ENodeType GetType(void);
90
91         private :
92
93            StdString content;
94
95      }; // class CVar
96
97      template <typename T>
98      inline T CVariable::getData(void) const
99      {
100         T retval ;
101         std::stringstream sstr(std::stringstream::in | std::stringstream::out);
102         sstr<<content ;
103         sstr>>retval ;
104         if (sstr.fail()) ERROR("CVariable::getdata()",
105               << "Cannot convert string <" << content << "> into type required" );
106         return retval ;
107      }
108
109      template<>
110      inline bool CVariable::getData(void) const
111      {
112         if (content.compare("true")==0 || content.compare(".true.")==0 || content.compare(".TRUE.")==0) return true ;
113         else if (content.compare("false")==0 || content.compare(".false.")==0 || content.compare(".FALSE.")==0) return false ;
114         else ERROR("CVariable::getdata()",
115               << "Cannot convert string <" << content << "> into type required" );
116         return false ;
117      }
118
119      template<>
120      inline std::string CVariable::getData(void) const
121      { return content; }
122
123      template<>
124      inline void CVariable::setData(bool data)
125      {
126        if (true == data) content.assign("true");
127        else content.assign("false");
128      }
129
130      template <typename T>
131      inline void CVariable::setData(T data)
132      {
133        std::stringstream sstr;
134        sstr<<data;
135        content = sstr.str();
136      }
137
138      ///--------------------------------------------------------------
139
140      // Declare/Define CVarGroup and CVarDefinition
141      DECLARE_GROUP_PARSE_REDEF(CVariable);
142} // namespace xios
143
144#endif // __XIOS_CVariable__
Note: See TracBrowser for help on using the repository browser.