source: XIOS/trunk/src/object_template_impl.hpp @ 549

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

Revised calendar functionalities:

  • the calendar is now configured from a specific calendar child node of the context in the XML configuration file. Example: <calendar type="Gregorian" start_date="2012-03-01 15:00:00" time_origin="2012-02-29 15:00:00" timestep="1h" />
  • the calendar type should now be configured when defining the start time and/or the time origin.
  • the start time and the time origin are now optional, 0000-01-01 00:00:00 will be used by default. It is also possible to define them partially. For example, 2015 and 2014-12 are valid dates corresponding respectively to 2015-01-01 00:00:00 and 2014-12-01 00:00:00.
  • an optional duration offset can be added to the start date and time origin. For example, it's possible to define the date 2015-01-12 12:00:00 as 2015-01-11 + 36h or 2015-01-11 12:00:00 + 1d. The duration format is the same as the time step. Being that the date is optional, it is possible to only use a duration (for example + 42s is the same as 0000-01-01 00:00:00 + 42s). An error will be raised if a duration based on the time step is used before the time step was configured. For example, the following would cause an error: <calendar type="Gregorian" start_date="+ 1ts" /> but <calendar type="Gregorian" start_date="+ 1ts" timestep="0.5h" /> would not.
  • new Fortran interface to define the calendar:
    • xios_define_calendar(type[, timestep, start_date, time_origin]) will create a calendar when none had previously been defined. Only the type argument is mandatory, the rest is optional. Calendar operations on dates and durations are possible as soon as the calendar is created (either using this procedure or directly from the XML configuration file).
    • the following getter and setter procedures are available: xios_set_timestep, xios_set_start_date, xios_set_time_origin, xios_get_calendar_type, xios_get_timestep, xios_get_start_date, xios_get_time_origin.
  • new Fortran interface to interact with the calendar: xios_update_calendar, xios_get_current_date, xios_get_year_length_in_seconds, xios_get_day_length_in_seconds.
  • new Fortran interface for date conversion: xios_date_get_second_of_year, xios_date_get_day_of_year, xios_date_get_fraction_of_year, xios_date_get_second_of_day, xios_date_get_fraction_of_day.
  • two new placeholders are available to format the file name when splitting the output (split_freq_format attribute):
    • %S the number of seconds since the time origin
    • %D the integral number of days since the time origin
  • 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: 12.8 KB
Line 
1#ifndef __XMLIO_CObjectTemplate_impl__
2#define __XMLIO_CObjectTemplate_impl__
3
4#include "xmlioserver_spl.hpp"
5#include "context_client.hpp"
6#include "object_factory.hpp"
7#include "context.hpp"
8#include "buffer_in.hpp"
9#include "attribute.hpp"
10#include "event_client.hpp"
11#include "object_template.hpp"
12#include "context_client.hpp"
13#include "indent.hpp"
14#include "type_util.hpp"
15#include "message.hpp"
16#include "type.hpp"
17#include "type_util.hpp"
18#include "group_template.hpp"
19
20namespace xios
21{
22   /// ////////////////////// Définitions ////////////////////// ///
23   template <class T>
24      xios_map<StdString,
25      xios_map<StdString,
26      boost::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj;
27
28   template <class T>
29      xios_map<StdString,
30      std::vector<boost::shared_ptr<T> > > CObjectTemplate<T>::AllVectObj;
31
32   template <class T>
33      xios_map<StdString,long int> CObjectTemplate<T>::GenId;
34
35   template <class T>
36      CObjectTemplate<T>::CObjectTemplate(void)
37         : CAttributeMap()
38         , CObject()
39   { /* Ne rien faire de plus */ }
40
41   template <class T>
42      CObjectTemplate<T>::CObjectTemplate(const StdString & id)
43         : CAttributeMap()
44         , CObject(id)
45   { /* Ne rien faire de plus */ }
46
47   template <class T>
48      CObjectTemplate<T>::CObjectTemplate
49         (const CObjectTemplate<T> & object, bool withAttrList, bool withId)
50         : CAttributeMap()
51         , CObject()
52   {
53      if (object.hasId() && withId)
54         this->setId(object.getId());
55      ERROR("CObjectTemplate<T> construtor 3", << "Not completly implemented yet !");
56   }
57
58   template <class T>
59      CObjectTemplate<T>::~CObjectTemplate(void)
60   { /* Ne rien faire de plus */ }
61
62   ///--------------------------------------------------------------
63
64   template <class T>
65      std::vector<boost::shared_ptr<T> > &
66         CObjectTemplate<T>::GetAllVectobject(const StdString & contextId)
67   {
68      return (CObjectTemplate<T>::AllVectObj[contextId]);
69   }
70
71   //---------------------------------------------------------------
72
73   template <class T>
74      StdString CObjectTemplate<T>::toString(void) const
75   {
76      StdOStringStream oss;
77      oss << "<" << T::GetName();
78      if (this->hasId())
79         oss << " id=\"" << this->getId() << "\"";
80      oss << " " << SuperClassMap::toString() << "/>";
81      return (oss.str());
82   }
83
84   template <class T>
85      void CObjectTemplate<T>::fromString(const StdString & str)
86   {
87      ERROR("CObjectTemplate<T>::fromString(str)",
88            << "[ str = " << str << "] Not implemented yet !");
89   }
90
91   //---------------------------------------------------------------
92
93/*
94   template <class T>
95      void CObjectTemplate<T>::toBinary(StdOStream & os) const
96   {
97      SuperClassMap::toBinary(os);
98   }
99
100   template <class T>
101      void CObjectTemplate<T>::fromBinary(StdIStream & is)
102   {
103      SuperClassMap::fromBinary(is);
104   }
105*/
106
107   //---------------------------------------------------------------
108
109   template <class T>
110      void CObjectTemplate<T>::parse(xml::CXMLNode & node)
111   {
112      xml::THashAttributes attributes = node.getAttributes();
113      CAttributeMap::setAttributes(attributes);
114   }
115
116   //---------------------------------------------------------------
117
118   template <class T>
119      ENodeType CObjectTemplate<T>::getType(void) const
120   {
121      return (T::GetType());
122   }
123
124   template <class T>
125   string CObjectTemplate<T>::getName(void) const
126   {
127      return (T::GetName());
128   }
129
130   //---------------------------------------------------------------
131
132   template <class T>
133      bool CObjectTemplate<T>::hasChild(void) const
134   {
135      return (false);
136   }
137
138   //---------------------------------------------------------------
139
140   template <class T>
141      void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
142   {
143      if (parent != NULL)
144         SuperClassMap::setAttributes(parent, apply);
145   }
146
147   //---------------------------------------------------------------
148
149   template <class T>
150      void CObjectTemplate<T>::ClearAllAttributes(void)
151   {
152      vector<T*> avect = CObjectTemplate<T>::getAll();
153      typename vector<T*>::iterator
154            it = avect.begin(), end = avect.end();
155
156      for (;it != end; it++)
157      {
158         CAttributeMap & amap = **it;
159         amap.clearAllAttributes();
160      }
161   }
162
163   template<typename T>
164   void CObjectTemplate<T>::sendAllAttributesToServer()
165   {
166     CAttributeMap& attrMap = *this;
167     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
168     for (; it != itE; ++it)
169     {
170       if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second));
171     }
172
173   }
174
175   template <class T>
176   void CObjectTemplate<T>::sendAttributToServer(const string& id)
177   {
178      CAttributeMap & attrMap = *this;
179      CAttribute* attr=attrMap[id];
180      sendAttributToServer(*attr);
181   }
182
183  template <class T>
184  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
185  {
186    CContext* context=CContext::getCurrent();
187
188    if (!context->hasServer)
189    {
190       CContextClient* client=context->client;
191
192       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
193       if (client->isServerLeader())
194       {
195         CMessage msg;
196//         msg<<this->getId();
197         msg<<this->getIdServer();
198         msg<<attr.getName();
199         msg<<attr;
200         event.push(client->getServerLeader(),1,msg);
201         client->sendEvent(event);
202       }
203       else client->sendEvent(event);
204    }
205
206  }
207
208  template <class T>
209  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
210  {
211
212    CBufferIn* buffer=event.subEvents.begin()->buffer;
213    string id,attrId;
214    *buffer>>id;
215    CAttributeMap & attrMap = *get(id);
216    *buffer>>attrId;
217    CAttribute* attr=attrMap[attrId];
218    info(50)<<"attribut recu "<<attrId<<"  ";
219    if (attr->isEmpty()) info(50)<<"--> empty"<<endl;
220    else info(50) /*<attr->getValue()*/<<endl;
221    *buffer>>*attr;
222     info(50)<<"attribut recu "<<attrId<<"  ";
223    if (attr->isEmpty()) info(50)<<"--> empty"<<endl;
224    else info(50) /*attr->getValue()*/<<endl;
225  }
226
227   template <class T>
228   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
229   {
230      switch(event.type)
231      {
232         case EVENT_ID_SEND_ATTRIBUTE :
233           recvAttributFromClient(event);
234           return true;
235           break;
236
237         default :
238         return false;
239//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)",
240//                 <<"Unknown Event");
241      }
242   }
243
244   template <typename T>
245   bool CObjectTemplate<T>::has(const string & id)
246   {
247     return CObjectFactory::HasObject<T>(id);
248   }
249
250   template <typename T>
251   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
252   {
253     return CObjectFactory::HasObject<T>(contextId,id);
254   }
255
256   template <typename T>
257   T* CObjectTemplate<T>::get(const string & id)
258   {
259     return CObjectFactory::GetObject<T>(id).get();
260   }
261
262   template <typename T>
263   T* CObjectTemplate<T>::get(const T* ptr)
264   {
265     return CObjectFactory::GetObject<T>(ptr).get();
266   }
267
268   template <typename T>
269   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
270   {
271     return CObjectFactory::GetObject<T>(ptr);
272   }
273
274   template <typename T>
275   shared_ptr<T> CObjectTemplate<T>::getShared(void)
276   {
277     return CObjectFactory::GetObject<T>((T*)this);
278   }
279
280   template <typename T>
281   const vector<T*> CObjectTemplate<T>::getAll()
282   {
283     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
284     vector<T*> vect;
285
286     typename vector<shared_ptr<T> >::const_iterator it;
287     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
288     return vect;
289   }
290
291   template <typename T>
292   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
293   {
294     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
295     vector<T*> vect;
296
297     typename vector<shared_ptr<T> >::const_iterator it;
298     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
299     return vect;
300   }
301
302   template <typename T>
303   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
304   {
305     return CObjectFactory::GetObject<T>(contextId,id).get();
306   }
307
308   template <typename T>
309   T* CObjectTemplate<T>::create(const string & id)
310   {
311     return CObjectFactory::CreateObject<T>(id).get();
312   }   ///--------------------------------------------------------------
313
314  template <typename T>
315  T* CObjectTemplate<T>::get(void)
316  {
317    return CObjectFactory::GetObject<T>((T*)this).get();
318  }
319
320   template <typename T>
321   void CObjectTemplate<T>::generateCInterface(ostream& oss)
322   {
323     string className=getName();
324     int found=className.rfind("_group");
325     if (found!=string::npos) className.replace(found,1,0,'x');
326
327     oss<<"/* ************************************************************************** *"<<iendl;
328     oss<<" *               Interface auto generated - do not modify                   *"<<iendl;
329     oss<<" * ************************************************************************** */"<<iendl;
330     oss<<iendl;
331     oss<<"#include <boost/multi_array.hpp>"<<iendl;
332     oss<<"#include <boost/shared_ptr.hpp>"<<iendl;
333     oss<<"#include \"xmlioserver.hpp\""<<iendl;
334     oss<<"#include \"attribute_template.hpp\""<<iendl;
335     oss<<"#include \"object_template.hpp\""<<iendl;
336     oss<<"#include \"group_template.hpp\""<<iendl;
337     oss<<"#include \"icutil.hpp\""<<iendl;
338     oss<<"#include \"icdate.hpp\""<<iendl;
339     oss<<"#include \"timer.hpp\""<<iendl;
340     oss<<"#include \"node_type.hpp\""<<iendl;
341     oss<<iendl;
342     oss<<"extern \"C\""<<iendl;
343     oss<<"{"<<iendl++;
344     oss<<"typedef xios::"<<getStrType<T>()<<"*  "<<className<<"_Ptr;"<<iendl;
345     oss<<iendl;
346     SuperClassMap::generateCInterface(oss,className);
347     oss<<iendl--;
348     oss<<"}"<<iendl;
349   }
350
351   template <typename T>
352   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
353   {
354     string className=getName();
355     int found=className.rfind("_group");
356     if (found!=string::npos) className.replace(found,1,0,'x');
357
358     oss<<"! * ************************************************************************** *"<<iendl;
359     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl;
360     oss<<"! * ************************************************************************** *"<<iendl;
361     oss<<"#include \"../fortran/xios_fortran_prefix.hpp\""<<iendl;
362     oss<<iendl;
363     oss<<"MODULE "<<className<<"_interface_attr"<<iendl++;
364     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl;
365     oss<<iendl;
366     oss<<"INTERFACE ! Do not call directly / interface FORTRAN 2003 <-> C99"<<iendl++;
367     oss<<iendl;
368     oss<<iendl;
369     SuperClassMap::generateFortran2003Interface(oss,className);
370     oss<<"END INTERFACE"<<iendl--;
371     oss<<iendl--;
372     oss<<"END MODULE "<<className<<"_interface_attr"<<iendl;
373   }
374
375   template <typename T>
376   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
377   {
378     string className=getName();
379     int found=className.rfind("_group");
380     if (found!=string::npos) className.erase(found,1);
381     string superClassName=getName();
382     found=superClassName.find("_group");
383     if (found!=string::npos) superClassName.erase(found,6);
384
385     oss<<"! * ************************************************************************** *"<<iendl;
386     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl;
387     oss<<"! * ************************************************************************** *"<<iendl;
388     oss<<"#include \"xios_fortran_prefix.hpp\""<<iendl;
389     oss<<iendl;
390     oss<<"MODULE i"<<className<<"_attr"<<iendl++;
391     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl;
392     oss<<"USE i"<<superClassName<<iendl;
393     oss<<"USE "<<className<<"_interface_attr"<<iendl;
394//     oss<<"TYPE txios("<<className<<")"<<iendl;
395//     oss<<"  INTEGER(kind = C_INTPTR_T) :: daddr"<<iendl;
396//     oss<<"END TYPE txios("<<className<<")"<<iendl;
397     oss<<iendl--;
398     oss<<"CONTAINS"<<iendl++;
399     oss<<iendl;
400     SuperClassMap::generateFortranInterface_id(oss,className);
401     oss<<iendl;
402     SuperClassMap::generateFortranInterface_hdl(oss,className);
403     oss<<iendl;
404     SuperClassMap::generateFortranInterface_hdl_(oss,className);
405     oss<<iendl;
406     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
407     oss<<iendl;
408     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
409     oss<<iendl;
410     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
411     oss<<iendl;
412     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
413     oss<<iendl;
414     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
415     oss<<iendl;
416     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
417     oss<<iendl--;
418     oss<<"END MODULE i"<<className<<"_attr"<<iendl;
419   }
420
421
422} // namespace xios
423
424#endif // __XMLIO_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.