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

Last change on this file since 527 was 527, checked in by rlacroix, 7 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: 5.2 KB
Line 
1#include "variable.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "xmlioserver_spl.hpp"
8#include "type.hpp"
9#include "context.hpp"
10#include "context_client.hpp"
11#include <boost/algorithm/string.hpp>
12
13namespace xios {
14
15   /// ////////////////////// Définitions ////////////////////// ///
16
17   CVariable::CVariable(void)
18      : CObjectTemplate<CVariable>()
19      , CVariableAttributes()
20      , content()
21   { /* Ne rien faire de plus */ }
22
23   CVariable::CVariable(const StdString & id)
24      : CObjectTemplate<CVariable>(id)
25      , CVariableAttributes()
26      , content()
27   { /* Ne rien faire de plus */ }
28
29   CVariable::~CVariable(void)
30   { /* Ne rien faire de plus */ }
31
32   StdString CVariable::GetName(void)   { return (StdString("variable")); }
33   StdString CVariable::GetDefName(void){ return (CVariable::GetName()); }
34   ENodeType CVariable::GetType(void)   { return (eVariable); }
35
36   void CVariable::parse(xml::CXMLNode & node)
37   {
38      SuperClass::parse(node);
39      StdString id = (this->hasId()) ? this->getId() : StdString("undefined");
40      if (!node.getContent(this->content))
41      {
42         ERROR("CVariable::parse(xml::CXMLNode & node)",
43               << "[ variable id = " << id
44               << " ] variable is not defined !");
45      }
46      content = boost::trim_copy(content) ;
47   }
48
49   const StdString & CVariable::getContent (void) const
50   {
51      return (this->content);
52   }
53
54   void CVariable::setContent(const StdString& contentStr)
55   {
56     this->content = contentStr;
57   }
58
59   StdString CVariable::toString(void) const
60   {
61      StdOStringStream oss;
62
63      oss << "<" << CVariable::GetName() << " ";
64      if (this->hasId())
65         oss << " id=\"" << this->getId() << "\" ";
66      oss << SuperClassAttribute::toString() << ">" << std::endl
67          << this->content /*<< std::endl*/;
68      oss << "</" << CVariable::GetName() << " >";
69      return (oss.str());
70   }
71
72   /*
73   *\brief Sending value of a variable with its id from client to server
74   *
75   */
76   void CVariable::sendValue()
77   {
78     CContext* context=CContext::getCurrent() ;
79     if (!context->hasServer)
80     {
81       CContextClient* client=context->client ;
82
83       CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
84       if (client->isServerLeader())
85       {
86         CMessage msg ;
87         msg<<this->getId() ;
88         msg<<content ;
89         event.push(client->getServerLeader(),1,msg) ;
90         client->sendEvent(event) ;
91       }
92       else client->sendEvent(event) ;
93    }
94   }
95
96   /*
97   *\brief Receive value of a variable with its id from client to server
98   *
99   */
100   void CVariable::recvValue(CEventServer& event)
101   {
102      CBufferIn* buffer=event.subEvents.begin()->buffer;
103      string id;
104      *buffer>>id ;
105      get(id)->recvValue(*buffer);
106   }
107
108   /*
109   *\brief Receive value of a variable with its id from client to server
110   *
111   */
112   void CVariable::recvValue(CBufferIn& buffer)
113   {
114      string str ;
115      buffer>>str;
116      setContent(str);
117   }
118
119   bool CVariable::dispatchEvent(CEventServer& event)
120   {
121    if (SuperClass::dispatchEvent(event)) return true ;
122    else
123    {
124      switch(event.type)
125      {
126        case EVENT_ID_VARIABLE_VALUE :
127          recvValue(event) ;
128          return true ;
129          break ;
130
131        default :
132          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
133          return false ;
134      }
135    }
136   }
137
138/*
139   void CVariable::toBinary(StdOStream & os) const
140   {
141     const StdString & content = this->content;
142     const StdSize size        =  content.size();
143     SuperClass::toBinary(os);
144
145     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
146     os.write (content.data(), size * sizeof(char));
147   }
148
149   void CVariable::fromBinary(StdIStream & is)
150   {
151      SuperClass::fromBinary(is);
152      StdSize size  = 0;
153      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
154      this->content.assign(size, ' ');
155      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
156   }
157*/
158   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
159   {
160      CVariableGroup* group_ptr = (this->hasId())
161         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
162
163      StdString content;
164      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
165      {
166        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
167        StdString subdata, subid;
168
169        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
170        {
171           endid   = content.find_first_of ( " \r\n\t=", beginid );
172           subid   = content.substr ( beginid, endid-beginid);
173           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
174
175           begindata = content.find_first_of ( "=", endid ) + 1;
176           enddata   = content.find_first_of ( ";", begindata );
177           subdata   = content.substr ( begindata, enddata-begindata);
178           subdata   = boost::trim_copy(subdata) ;
179           group_ptr->createChild(subid)->content = subdata ;
180        }
181      }
182      else
183      {
184         SuperClass::parse(node, withAttr);
185      }
186      //SuperClass::parse(node, withAttr);
187
188   }
189
190} // namespace xios
Note: See TracBrowser for help on using the repository browser.