New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
attribute_map.cpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/attribute_map.cpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.9 KB
Line 
1#include "attribute_map.hpp"
2#include "indent.hpp"
3
4namespace xios
5{
6      /// ////////////////////// Définitions ////////////////////// ///
7      CAttributeMap * CAttributeMap::Current = NULL;
8
9      CAttributeMap::CAttributeMap(void)
10         : xios_map<StdString, CAttribute*>()
11      { CAttributeMap::Current = this; }
12
13      CAttributeMap::~CAttributeMap(void)
14      { /* Ne rien faire de plus */ }
15     
16      ///--------------------------------------------------------------
17
18      void CAttributeMap::clearAllAttributes(void)
19      {
20         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
21         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
22         for (; it != end; it++)
23         {
24            const StdStrAttPair & att = *it;
25            if (!att.second->isEmpty())
26               att.second->clear();
27         }
28      }
29
30      //---------------------------------------------------------------
31
32      bool CAttributeMap::hasAttribute(const StdString & key) const
33      { 
34         return (this->find(key) != this->end()); 
35      }
36     
37      //---------------------------------------------------------------
38     
39      void CAttributeMap::setAttribute(const StdString & key, CAttribute * const attr)
40      {
41         if (!this->hasAttribute(key))
42            ERROR("CAttributeMap::setAttribute(key, attr)",
43                   << "[ key = " << key << "] key not found !");
44         if (attr == NULL)
45            ERROR("CAttributeMap::setAttribute(key, attr)",
46                   << "[ key = " << key << "] attr is null !");
47         this->find(key)->second->setAnyValue(attr->getAnyValue());
48      }
49     
50      //---------------------------------------------------------------
51     
52      CAttribute * CAttributeMap::operator[](const StdString & key)
53      {
54         if (!this->hasAttribute(key))
55            ERROR("CAttributeMap::operator[](const StdString & key)",
56                  << "[ key = " << key << "] key not found !");
57         return (SuperClassMap::operator[](key));
58      }
59     
60      //---------------------------------------------------------------
61     
62      StdString CAttributeMap::toString(void) const
63      {
64         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
65         StdOStringStream oss;
66         
67         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
68         for (; it != end; it++)
69         {
70            const StdStrAttPair & att = *it;
71            if (!att.second->isEmpty())
72               oss << *att.second << " ";
73         }
74         return (oss.str());
75      }
76     
77      //---------------------------------------------------------------
78     
79      void CAttributeMap::fromString(const StdString & str)
80      { 
81         ERROR("CAttributeMap::fromString(const StdString & str)",
82               << "[ str = " << str << "] Not implemented yet !"); 
83      }
84     
85      //---------------------------------------------------------------
86
87      //StdOStream & operator << (StdOStream & os, const CAttributeMap & attributmap)
88      //{ os << attributmap.toString(); return (os); }
89     
90      //---------------------------------------------------------------
91     
92      void CAttributeMap::setAttributes(const xml::THashAttributes & attributes)
93      {
94         for (xml::THashAttributes::const_iterator it  = attributes.begin();
95                                                   it != attributes.end();
96                                                   it ++)
97         {
98            if ((*it).first.compare(StdString("id")) != 0 &&
99                (*it).first.compare(StdString("src"))!= 0)
100            {
101               //if (CAttributeMap::operator[]((*it).first)->isEmpty())
102               CAttributeMap::operator[]((*it).first)->fromString((*it).second);
103            }
104         }
105      }
106     
107      //---------------------------------------------------------------
108     
109      void CAttributeMap::setAttributes(const CAttributeMap * const _parent)
110      {
111         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
112         
113         SuperClassMap::const_iterator it = _parent->begin(), end = _parent->end();
114         for (; it != end; it++)
115         {
116            const StdStrAttPair & el = *it;
117            if (this->hasAttribute(el.first))
118            {
119               CAttribute * currAtt = CAttributeMap::operator[](el.first);
120               if (currAtt->isEmpty() && !el.second->isEmpty())
121               {
122                  this->setAttribute(el.first, el.second);
123               }
124            }
125         }
126      }
127     
128      //---------------------------------------------------------------
129     
130      void CAttributeMap::toBinary(StdOStream & os) const
131      {
132         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
133         SuperClassMap::const_iterator it = this->begin(), end = this->end();
134         
135         const StdSize nbatt = SuperClassMap::size();
136         os.write (reinterpret_cast<const char*>(&nbatt) , sizeof(StdSize));
137         
138         for (; it != end; it++)
139         {
140            const StdString & key   = it->first;
141            const CAttribute* value = it->second;           
142            const StdSize size = key.size();
143           
144            os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
145            os.write (key.data(), size * sizeof(char));
146           
147            if (!value->isEmpty())
148            {
149               bool b = true;
150               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
151               value->toBinary(os);
152            }
153            else 
154            {
155               bool b = false;
156               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
157            }
158         }
159      }
160     
161      //---------------------------------------------------------------
162     
163      void CAttributeMap::fromBinary(StdIStream & is)
164      {
165         StdSize nbatt = 0;
166         is.read (reinterpret_cast<char*>(&nbatt), sizeof(StdSize));
167         
168         for (StdSize i = 0; i < nbatt; i++)
169         {
170            bool hasValue = false;
171            StdSize size  = 0;
172            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
173            StdString key(size, ' ');
174            is.read (const_cast<char *>(key.data()), size * sizeof(char));
175           
176            if (!this->hasAttribute(key))
177               ERROR("CAttributeMap::fromBinary(StdIStream & is)",
178                     << "[ key = " << key << "] key not found !");
179                                       
180            is.read (reinterpret_cast<char*>(&hasValue), sizeof(bool));
181           
182            if (hasValue)         
183               this->operator[](key)->fromBinary(is);
184         }
185      }
186     
187      void CAttributeMap::generateCInterface(ostream& oss, const string& className)
188      {
189         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
190         for (; it != end; it++)
191         {
192           it->second->generateCInterface(oss,className) ;
193           oss<<iendl<<iendl ;
194         }
195      }
196
197      void CAttributeMap::generateFortran2003Interface(ostream& oss, const string& className)
198      {
199         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
200         for (; it != end; it++)
201         {
202           it->second->generateFortran2003Interface(oss,className) ;
203           oss<<iendl<<iendl ;
204         }
205      }     
206     
207      ///--------------------------------------------------------------
208
209      void CAttributeMap::generateFortranInterface_hdl_(ostream& oss, const string& className)
210      {
211         oss<<"SUBROUTINE xios(set_"<<className<<"_attr_hdl_)   &"<<iendl++ ;
212         ostringstream* oss2 ;
213         SuperClassMap::const_iterator it ;
214         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
215         
216         oss2=new ostringstream ;
217         
218         *oss2<<"( "<<className<<"_hdl"  ;
219         
220         for ( it=begin ; it != end; it++) 
221         {
222           *oss2<<", "<<it->second->getName()<<"_" ;
223           if (oss2->str().size()>90) 
224           {
225             oss<<oss2->str()<<"  &"<<iendl ;
226             delete oss2 ;
227             oss2=new ostringstream ;
228           }
229         }
230         *oss2<<" )" ;
231         oss<<oss2->str()<<iendl ;
232         oss<<iendl ;
233         delete oss2 ; 
234         
235         oss<<"IMPLICIT NONE"<<iendl++ ;
236         oss<<"TYPE(txios("<<className<<")) , INTENT(IN) :: "<<className<<"_hdl"<<iendl ;
237         
238         for (it=begin; it != end; it++)
239         {
240           it->second->generateFortranInterfaceDeclaration_(oss,className) ;
241         }
242         
243         oss<<iendl ;
244         
245         for (it=begin; it != end; it++)
246         {
247           it->second->generateFortranInterfaceBody_(oss,className) ;
248           oss<<iendl ;
249         }
250         
251         oss<<iendl--<<iendl-- ;
252         oss<<"END SUBROUTINE xios(set_"<<className<<"_attr_hdl_)"<<iendl ;
253         
254      }     
255
256      void CAttributeMap::generateFortranInterfaceGet_hdl_(ostream& oss, const string& className)
257      {
258         oss<<"SUBROUTINE xios(get_"<<className<<"_attr_hdl_)   &"<<iendl++ ;
259         ostringstream* oss2 ;
260         SuperClassMap::const_iterator it ;
261         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
262         
263         oss2=new ostringstream ;
264         
265         *oss2<<"( "<<className<<"_hdl"  ;
266         
267         for ( it=begin ; it != end; it++) 
268         {
269           *oss2<<", "<<it->second->getName()<<"_" ;
270           if (oss2->str().size()>90) 
271           {
272             oss<<oss2->str()<<"  &"<<iendl ;
273             delete oss2 ;
274             oss2=new ostringstream ;
275           }
276         }
277         *oss2<<" )" ;
278         oss<<oss2->str()<<iendl ;
279         oss<<iendl ;
280         delete oss2 ; 
281         
282         oss<<"IMPLICIT NONE"<<iendl++ ;
283         oss<<"TYPE(txios("<<className<<")) , INTENT(IN) :: "<<className<<"_hdl"<<iendl ;
284         
285         for (it=begin; it != end; it++)
286         {
287           it->second->generateFortranInterfaceGetDeclaration_(oss,className) ;
288         }
289         
290         oss<<iendl ;
291         
292         for (it=begin; it != end; it++)
293         {
294           it->second->generateFortranInterfaceGetBody_(oss,className) ;
295           oss<<iendl ;
296         }
297         
298         oss<<iendl--<<iendl-- ;
299         oss<<"END SUBROUTINE xios(get_"<<className<<"_attr_hdl_)"<<iendl ;
300         
301      }     
302
303      void CAttributeMap::generateFortranInterface_hdl(ostream& oss, const string& className)
304      {
305         oss<<"SUBROUTINE xios(set_"<<className<<"_attr_hdl)  &"<<iendl++ ;
306         ostringstream* oss2 ;
307         SuperClassMap::const_iterator it ;
308         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
309         
310         oss2=new ostringstream ;
311         *oss2<<"( "<<className<<"_hdl"  ;
312         for ( it=begin ; it != end; it++) 
313         {
314           *oss2<<", "<<it->second->getName() ;
315           if (oss2->str().size()>90) 
316           {
317             oss<<oss2->str()<<"  &"<<iendl ;
318             delete oss2 ;
319             oss2=new ostringstream ;
320           }
321         }
322         *oss2<<" )" ;
323         oss<<oss2->str()<<iendl ;
324         oss<<iendl ;
325         delete oss2 ; 
326         oss2=new ostringstream ;
327         
328         oss<<"IMPLICIT NONE"<<iendl++ ;
329         oss<<"TYPE(txios("<<className<<")) , INTENT(IN) :: "<<className<<"_hdl"<<iendl ;
330         
331         for (it=begin; it != end; it++)
332         {
333           it->second->generateFortranInterfaceDeclaration(oss,className) ;
334         }
335         
336         oss<<iendl ;
337         
338         oss<<"CALL xios(set_"<<className<<"_attr_hdl_)  &"<<iendl ;
339         
340         *oss2<<"( "<<className<<"_hdl"  ;
341         for ( it=begin ; it != end; it++) 
342         {
343           *oss2<<", "<<it->second->getName() ;
344           if (oss2->str().size()>90) 
345           {
346             oss<<oss2->str()<<"  &"<<iendl ;
347             delete oss2 ;
348             oss2=new ostringstream ;
349           }
350         }
351         *oss2<<" )" ;
352         oss<<oss2->str() ;
353         delete oss2 ; 
354         
355         oss<<iendl--<<iendl-- ;
356         oss<<"END SUBROUTINE xios(set_"<<className<<"_attr_hdl)"<<iendl ;
357      }     
358     
359 
360      void CAttributeMap::generateFortranInterfaceGet_hdl(ostream& oss, const string& className)
361      {
362         oss<<"SUBROUTINE xios(get_"<<className<<"_attr_hdl)  &"<<iendl++ ;
363         ostringstream* oss2 ;
364         SuperClassMap::const_iterator it ;
365         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
366         
367         oss2=new ostringstream ;
368         *oss2<<"( "<<className<<"_hdl"  ;
369         for ( it=begin ; it != end; it++) 
370         {
371           *oss2<<", "<<it->second->getName() ;
372           if (oss2->str().size()>90) 
373           {
374             oss<<oss2->str()<<"  &"<<iendl ;
375             delete oss2 ;
376             oss2=new ostringstream ;
377           }
378         }
379         *oss2<<" )" ;
380         oss<<oss2->str()<<iendl ;
381         oss<<iendl ;
382         delete oss2 ; 
383         oss2=new ostringstream ;
384         
385         oss<<"IMPLICIT NONE"<<iendl++ ;
386         oss<<"TYPE(txios("<<className<<")) , INTENT(IN) :: "<<className<<"_hdl"<<iendl ;
387         
388         for (it=begin; it != end; it++)
389         {
390           it->second->generateFortranInterfaceGetDeclaration(oss,className) ;
391         }
392         
393         oss<<iendl ;
394         
395         oss<<"CALL xios(get_"<<className<<"_attr_hdl_)  &"<<iendl ;
396         
397         *oss2<<"( "<<className<<"_hdl"  ;
398         for ( it=begin ; it != end; it++) 
399         {
400           *oss2<<", "<<it->second->getName() ;
401           if (oss2->str().size()>90) 
402           {
403             oss<<oss2->str()<<"  &"<<iendl ;
404             delete oss2 ;
405             oss2=new ostringstream ;
406           }
407         }
408         *oss2<<" )" ;
409         oss<<oss2->str() ;
410         delete oss2 ; 
411         
412         oss<<iendl--<<iendl-- ;
413         oss<<"END SUBROUTINE xios(get_"<<className<<"_attr_hdl)"<<iendl ;
414      }     
415     
416      void CAttributeMap::generateFortranInterface_id(ostream& oss, const string& className)
417      {
418         oss<<"SUBROUTINE xios(set_"<<className<<"_attr)  &"<<iendl++ ;
419         ostringstream* oss2 ;
420         SuperClassMap::const_iterator it ;
421         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
422         
423         oss2=new ostringstream ;
424         *oss2<<"( "<<className<<"_id"  ;
425         for ( it=begin ; it != end; it++) 
426         {
427           *oss2<<", "<<it->second->getName() ;
428           if (oss2->str().size()>90) 
429           {
430             oss<<oss2->str()<<"  &"<<iendl ;
431             delete oss2 ;
432             oss2=new ostringstream ;
433           }
434         }
435         *oss2<<" )" ;
436         oss<<oss2->str()<<iendl ;
437         oss<<iendl ;
438         delete oss2 ; 
439         oss2=new ostringstream ;
440         
441         oss<<"IMPLICIT NONE"<<iendl++ ;
442
443         oss<<"TYPE(txios("<<className<<"))  :: "<<className<<"_hdl"<<iendl ;
444         oss<<"CHARACTER(LEN=*), INTENT(IN) ::"<<className<<"_id"<<iendl ;
445         
446         for (it=begin; it != end; it++)
447         {
448           it->second->generateFortranInterfaceDeclaration(oss,className) ;
449         }
450         
451         oss<<iendl ;
452         oss<<"CALL xios(get_"<<className<<"_handle)("<<className<<"_id,"<<className<<"_hdl)"<<iendl ; 
453         oss<<"CALL xios(set_"<<className<<"_attr_hdl_)   &"<<iendl ;
454         *oss2<<"( "<<className<<"_hdl"  ;
455         for ( it=begin ; it != end; it++) 
456         {
457           *oss2<<", "<<it->second->getName() ;
458           if (oss2->str().size()>90) 
459           {
460             oss<<oss2->str()<<"  &"<<iendl ;
461             delete oss2 ;
462             oss2=new ostringstream ;
463           }
464         }
465         *oss2<<" )" ;
466         oss<<oss2->str() ;
467         delete oss2 ; 
468         
469         oss<<iendl--<<iendl-- ;
470         oss<<"END SUBROUTINE xios(set_"<<className<<"_attr)"<<iendl ;
471         
472      }     
473     
474      void CAttributeMap::generateFortranInterfaceGet_id(ostream& oss, const string& className)
475      {
476         oss<<"SUBROUTINE xios(get_"<<className<<"_attr)  &"<<iendl++ ;
477         ostringstream* oss2 ;
478         SuperClassMap::const_iterator it ;
479         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
480         
481         oss2=new ostringstream ;
482         *oss2<<"( "<<className<<"_id"  ;
483         for ( it=begin ; it != end; it++) 
484         {
485           *oss2<<", "<<it->second->getName() ;
486           if (oss2->str().size()>90) 
487           {
488             oss<<oss2->str()<<"  &"<<iendl ;
489             delete oss2 ;
490             oss2=new ostringstream ;
491           }
492         }
493         *oss2<<" )" ;
494         oss<<oss2->str()<<iendl ;
495         oss<<iendl ;
496         delete oss2 ; 
497         oss2=new ostringstream ;
498         
499         oss<<"IMPLICIT NONE"<<iendl++ ;
500
501         oss<<"TYPE(txios("<<className<<"))  :: "<<className<<"_hdl"<<iendl ;
502         oss<<"CHARACTER(LEN=*), INTENT(IN) ::"<<className<<"_id"<<iendl ;
503         
504         for (it=begin; it != end; it++)
505         {
506           it->second->generateFortranInterfaceGetDeclaration(oss,className) ;
507         }
508         
509         oss<<iendl ;
510         oss<<"CALL xios(get_"<<className<<"_handle)("<<className<<"_id,"<<className<<"_hdl)"<<iendl ; 
511         oss<<"CALL xios(get_"<<className<<"_attr_hdl_)   &"<<iendl ;
512         *oss2<<"( "<<className<<"_hdl"  ;
513         for ( it=begin ; it != end; it++) 
514         {
515           *oss2<<", "<<it->second->getName() ;
516           if (oss2->str().size()>90) 
517           {
518             oss<<oss2->str()<<"  &"<<iendl ;
519             delete oss2 ;
520             oss2=new ostringstream ;
521           }
522         }
523         *oss2<<" )" ;
524         oss<<oss2->str() ;
525         delete oss2 ; 
526         
527         oss<<iendl--<<iendl-- ;
528         oss<<"END SUBROUTINE xios(get_"<<className<<"_attr)"<<iendl ;
529         
530      }     
531      ///--------------------------------------------------------------
532 
533
534} // namespace xmlioser
Note: See TracBrowser for help on using the repository browser.