source: XIOS/trunk/src/attribute_map.cpp @ 649

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

Simplify a bit the generation of Fortran interfaces.

  • 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: 23.2 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            att.second->reset();
26         }
27      }
28
29      //---------------------------------------------------------------
30
31      bool CAttributeMap::hasAttribute(const StdString& key) const
32      {
33         return (this->find(key) != this->end());
34      }
35
36      void CAttributeMap::clearAttribute(const StdString& key)
37      {
38        if (hasAttribute(key)) this->find(key)->second->reset();
39      }
40
41      //---------------------------------------------------------------
42
43      void CAttributeMap::setAttribute(const StdString& key, CAttribute* const attr)
44      {
45         if (!this->hasAttribute(key))
46            ERROR("CAttributeMap::setAttribute(key, attr)",
47                   << "[ key = " << key << "] key not found !");
48         if (attr == NULL)
49            ERROR("CAttributeMap::setAttribute(key, attr)",
50                   << "[ key = " << key << "] attr is null !");
51         this->find(key)->second->set(*attr);
52//       this->find(key)->second->setAnyValue(attr->getAnyValue());
53      }
54
55      //---------------------------------------------------------------
56
57      CAttribute* CAttributeMap::operator[](const StdString& key)
58      {
59         if (!this->hasAttribute(key))
60            ERROR("CAttributeMap::operator[](const StdString& key)",
61                  << "[ key = " << key << "] key not found !");
62         return SuperClassMap::operator[](key);
63      }
64
65      //---------------------------------------------------------------
66
67      StdString CAttributeMap::toString(void) const
68      {
69         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
70         StdOStringStream oss;
71
72         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
73         for (; it != end; it++)
74         {
75            const StdStrAttPair& att = *it;
76            if (!att.second->isEmpty())
77               oss << *att.second << " ";
78         }
79         return oss.str();
80      }
81
82      //---------------------------------------------------------------
83
84      void CAttributeMap::fromString(const StdString& str)
85      {
86         ERROR("CAttributeMap::fromString(const StdString& str)",
87               << "[ str = " << str << "] Not implemented yet !");
88      }
89
90      //---------------------------------------------------------------
91
92      //StdOStream& operator << (StdOStream& os, const CAttributeMap& attributmap)
93      //{ os << attributmap.toString(); return (os); }
94
95      //---------------------------------------------------------------
96
97      void CAttributeMap::setAttributes(const xml::THashAttributes& attributes)
98      {
99         for (xml::THashAttributes::const_iterator it  = attributes.begin();
100                                                   it != attributes.end();
101                                                   it++)
102         {
103            if ((*it).first.compare(StdString("id")) != 0 && (*it).first.compare(StdString("src")) != 0)
104            {
105               //if (CAttributeMap::operator[]((*it).first)->isEmpty())
106               CAttributeMap::operator[]((*it).first)->fromString((*it).second);
107            }
108         }
109      }
110
111      //---------------------------------------------------------------
112
113      /*!
114      \brief Set attributes from a specific attributemap, considered parent.
115         The child attribute map will insert the attributes of its parent into its current attribute map.
116      The existing attributes can be filled with the values of the parent map if they are empty or
117      simply replaced by these values depending on choice of user.
118      \param [in] _parent Attribute map from which the current map gets attributes.
119      \param [in] apply Specify if current attribute map is replaced by the attributes of parent (false)
120                    or filled in in case of emptyp (true)
121      */
122      void CAttributeMap::setAttributes(const CAttributeMap* const _parent, bool apply)
123      {
124         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
125
126         SuperClassMap::const_iterator it = _parent->begin(), end = _parent->end();
127         for (; it != end; it++)
128         {
129            const StdStrAttPair& el = *it;
130            if (this->hasAttribute(el.first))
131            {
132               CAttribute* currentAtt = CAttributeMap::operator[](el.first);
133               CAttribute* parentAtt = el.second;
134               if (apply)
135               {
136                 if (currentAtt->isEmpty() && !el.second->isEmpty())
137                 {
138                    this->setAttribute(el.first, el.second);
139                 }
140               }
141               else currentAtt->setInheritedValue(*parentAtt);
142            }
143         }
144      }
145
146      void CAttributeMap::duplicateAttributes(const CAttributeMap* const srcAttr)
147      {
148         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
149
150         SuperClassMap::const_iterator it = srcAttr->begin(), end = srcAttr->end();
151         for (; it != end; it++)
152         {
153            const StdStrAttPair& el = *it;
154            if (this->hasAttribute(el.first))
155            {
156               if (!el.second->isEmpty())
157               {
158                 this->setAttribute(el.first, el.second);
159               }
160            }
161         }
162      }
163
164      //---------------------------------------------------------------
165/*
166      void CAttributeMap::toBinary(StdOStream& os) const
167      {
168         typedef std::pair<StdString, CAttribute*> StdStrAttPair;
169         SuperClassMap::const_iterator it = this->begin(), end = this->end();
170
171         const StdSize nbatt = SuperClassMap::size();
172         os.write (reinterpret_cast<const char*>(&nbatt) , sizeof(StdSize));
173
174         for (; it != end; it++)
175         {
176            const StdString& key   = it->first;
177            const CAttribute* value = it->second;
178            const StdSize size = key.size();
179
180            os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
181            os.write (key.data(), size* sizeof(char));
182
183            if (!value->isEmpty())
184            {
185               bool b = true;
186               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
187               value->toBinary(os);
188            }
189            else
190            {
191               bool b = false;
192               os.write (reinterpret_cast<const char*>(&b) , sizeof(bool));
193            }
194         }
195      }
196
197      //---------------------------------------------------------------
198
199      void CAttributeMap::fromBinary(StdIStream& is)
200      {
201         StdSize nbatt = 0;
202         is.read (reinterpret_cast<char*>(&nbatt), sizeof(StdSize));
203
204         for (StdSize i = 0; i < nbatt; i++)
205         {
206            bool hasValue = false;
207            StdSize size  = 0;
208            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
209            StdString key(size, ' ');
210            is.read (const_cast<char *>(key.data()), size* sizeof(char));
211
212            if (!this->hasAttribute(key))
213               ERROR("CAttributeMap::fromBinary(StdIStream& is)",
214                     << "[ key = " << key << "] key not found !");
215
216            is.read (reinterpret_cast<char*>(&hasValue), sizeof(bool));
217
218            if (hasValue)
219               this->operator[](key)->fromBinary(is);
220         }
221      }
222 */
223      void CAttributeMap::generateCInterface(ostream& oss, const string& className)
224      {
225         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
226         for (; it != end; it++)
227         {
228           oss << std::endl << iendl;
229           it->second->generateCInterface(oss, className);
230           oss << iendl;
231           it->second->generateCInterfaceIsDefined(oss, className);
232         }
233      }
234
235      void CAttributeMap::generateFortran2003Interface(ostream& oss, const string& className)
236      {
237         SuperClassMap::const_iterator it = SuperClassMap::begin(), end = SuperClassMap::end();
238         for (; it != end; it++)
239         {
240           oss << std::endl << iendl;
241           it->second->generateFortran2003Interface(oss, className);
242           oss << iendl;
243           it->second->generateFortran2003InterfaceIsDefined(oss, className);
244         }
245      }
246
247      ///--------------------------------------------------------------
248
249      void CAttributeMap::generateFortranInterface_hdl_(ostream& oss, const string& className)
250      {
251         oss << "SUBROUTINE xios(set_" << className << "_attr_hdl_)   &" << iendl++;
252         SuperClassMap::const_iterator it;
253         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
254
255         long startPos = oss.tellp();
256
257         oss << "( " << className << "_hdl";
258         for (it = begin; it != end; it++)
259         {
260           oss << ", " << it->second->getName() << "_";
261           if (oss.tellp() - startPos > 90)
262           {
263             oss << "  &" << iendl;
264             startPos = oss.tellp();
265           }
266         }
267         oss << " )";
268         oss << std::endl;
269         oss << iendl;
270
271         oss << "IMPLICIT NONE" << iendl++;
272         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
273
274         for (it = begin; it != end; it++)
275         {
276           oss << iendl;
277           it->second->generateFortranInterfaceDeclaration_(oss, className);
278         }
279
280         for (it = begin; it != end; it++)
281         {
282           oss << std::endl << iendl;
283           it->second->generateFortranInterfaceBody_(oss, className);
284         }
285
286         oss << std::endl << (iendl -= 2);
287         oss << "END SUBROUTINE xios(set_" << className << "_attr_hdl_)" << std::endl;
288      }
289
290      void CAttributeMap::generateFortranInterfaceGet_hdl_(ostream& oss, const string& className)
291      {
292         oss << "SUBROUTINE xios(get_" << className << "_attr_hdl_)   &" << iendl++;
293         SuperClassMap::const_iterator it;
294         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
295
296         long startPos = oss.tellp();
297
298         oss << "( " << className << "_hdl";
299         for (it = begin; it != end; it++)
300         {
301           oss << ", " << it->second->getName() << "_";
302           if (oss.tellp() - startPos > 90)
303           {
304             oss << "  &" << iendl;
305             startPos = oss.tellp();
306           }
307         }
308         oss << " )";
309         oss << std::endl;
310         oss << iendl;
311
312         oss << "IMPLICIT NONE" << iendl++;
313         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
314
315         for (it = begin; it != end; it++)
316         {
317           oss << iendl;
318           it->second->generateFortranInterfaceGetDeclaration_(oss, className);
319         }
320
321         for (it = begin; it != end; it++)
322         {
323           oss << std::endl << iendl;
324           it->second->generateFortranInterfaceGetBody_(oss, className);
325         }
326
327         oss << std::endl << (iendl -= 2);
328         oss << "END SUBROUTINE xios(get_" << className << "_attr_hdl_)" << std::endl;
329      }
330
331      void CAttributeMap::generateFortranInterfaceIsDefined_hdl_(ostream& oss, const string& className)
332      {
333         oss << "SUBROUTINE xios(is_defined_" << className << "_attr_hdl_)   &" << iendl++;
334         SuperClassMap::const_iterator it;
335         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
336
337         long startPos = oss.tellp();
338
339         oss << "( " << className << "_hdl";
340         for (it = begin; it != end; it++)
341         {
342           oss << ", " << it->second->getName() << "_";
343           if (oss.tellp() - startPos > 90)
344           {
345             oss << "  &" << iendl;
346             startPos = oss.tellp();
347           }
348         }
349         oss << " )";
350         oss << std::endl;
351         oss << iendl;
352
353         oss << "IMPLICIT NONE" << iendl++;
354         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
355
356         for (it = begin; it != end; it++)
357         {
358           oss << iendl;
359           it->second->generateFortranInterfaceIsDefinedDeclaration_(oss, className);
360         }
361
362         for (it = begin; it != end; it++)
363         {
364           oss << std::endl << iendl;
365           it->second->generateFortranInterfaceIsDefinedBody_(oss, className);
366         }
367
368         oss << std::endl << (iendl -= 2);
369         oss << "END SUBROUTINE xios(is_defined_" << className << "_attr_hdl_)" << std::endl;
370      }
371
372      void CAttributeMap::generateFortranInterface_hdl(ostream& oss, const string& className)
373      {
374         oss << "SUBROUTINE xios(set_" << className << "_attr_hdl)  &" << iendl++;
375         SuperClassMap::const_iterator it;
376         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
377
378         long startPos = oss.tellp();
379
380         oss << "( " << className << "_hdl";
381         for (it = begin; it != end; it++)
382         {
383           oss << ", " << it->second->getName();
384           if (oss.tellp() - startPos > 90)
385           {
386             oss << "  &" << iendl;
387             startPos = oss.tellp();
388           }
389         }
390         oss << " )";
391         oss << std::endl;
392         oss << iendl;
393
394         oss << "IMPLICIT NONE" << iendl++;
395         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
396
397         for (it = begin; it != end; it++)
398         {
399           oss << iendl;
400           it->second->generateFortranInterfaceDeclaration(oss, className);
401         }
402
403         oss << std::endl << iendl;
404
405         oss << "CALL xios(set_" << className << "_attr_hdl_)  &" << iendl;
406
407         startPos = oss.tellp();
408
409         oss << "( " << className << "_hdl";
410         for (it = begin; it != end; it++)
411         {
412           oss << ", " << it->second->getName();
413           if (oss.tellp() - startPos > 90)
414           {
415             oss << "  &" << iendl;
416             startPos = oss.tellp();
417           }
418         }
419         oss << " )";
420
421         oss << std::endl << (iendl -= 2);
422         oss << "END SUBROUTINE xios(set_" << className << "_attr_hdl)" << std::endl;
423      }
424
425      void CAttributeMap::generateFortranInterfaceGet_hdl(ostream& oss, const string& className)
426      {
427         oss << "SUBROUTINE xios(get_" << className << "_attr_hdl)  &" << iendl++;
428         SuperClassMap::const_iterator it;
429         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
430
431         long startPos = oss.tellp();
432
433         oss << "( " << className << "_hdl";
434         for (it = begin; it != end; it++)
435         {
436           oss << ", " << it->second->getName();
437           if (oss.tellp() - startPos > 90)
438           {
439             oss << "  &" << iendl;
440             startPos = oss.tellp();
441           }
442         }
443         oss << " )";
444         oss << std::endl;
445         oss << iendl;
446
447         oss << "IMPLICIT NONE" << iendl++;
448         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
449
450         for (it = begin; it != end; it++)
451         {
452           oss << iendl;
453           it->second->generateFortranInterfaceGetDeclaration(oss, className);
454         }
455
456         oss << std::endl << iendl;
457
458         oss << "CALL xios(get_" << className << "_attr_hdl_)  &" << iendl;
459
460         startPos = oss.tellp();
461
462         oss << "( " << className << "_hdl";
463         for (it = begin; it != end; it++)
464         {
465           oss << ", " << it->second->getName();
466           if (oss.tellp() - startPos > 90)
467           {
468             oss << "  &" << iendl;
469             startPos = oss.tellp();
470           }
471         }
472         oss << " )";
473
474         oss << std::endl << (iendl -= 2);
475         oss << "END SUBROUTINE xios(get_" << className << "_attr_hdl)" << std::endl;
476      }
477
478      void CAttributeMap::generateFortranInterfaceIsDefined_hdl(ostream& oss, const string& className)
479      {
480         oss << "SUBROUTINE xios(is_defined_" << className << "_attr_hdl)  &" << iendl++;
481         SuperClassMap::const_iterator it;
482         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
483
484         long startPos = oss.tellp();
485
486         oss << "( " << className << "_hdl";
487         for (it = begin; it != end; it++)
488         {
489           oss << ", " << it->second->getName();
490           if (oss.tellp() - startPos > 90)
491           {
492             oss << "  &" << iendl;
493             startPos = oss.tellp();
494           }
495         }
496         oss << " )";
497         oss << std::endl;
498         oss << iendl;
499
500         oss << "IMPLICIT NONE" << iendl++;
501         oss << "TYPE(txios(" << className << ")) , INTENT(IN) :: " << className << "_hdl";
502
503         for (it = begin; it != end; it++)
504         {
505           oss << iendl;
506           it->second->generateFortranInterfaceIsDefinedDeclaration(oss, className);
507         }
508
509         oss << std::endl << iendl;
510
511         oss << "CALL xios(is_defined_" << className << "_attr_hdl_)  &" << iendl;
512
513         startPos = oss.tellp();
514
515         oss << "( " << className << "_hdl";
516         for (it = begin; it != end; it++)
517         {
518           oss << ", " << it->second->getName();
519           if (oss.tellp() - startPos > 90)
520           {
521             oss << "  &" << iendl;
522             startPos = oss.tellp();
523           }
524         }
525         oss << " )";
526
527         oss << std::endl << (iendl -= 2);
528         oss << "END SUBROUTINE xios(is_defined_" << className << "_attr_hdl)" << std::endl;
529      }
530
531      void CAttributeMap::generateFortranInterface_id(ostream& oss, const string& className)
532      {
533         oss << "SUBROUTINE xios(set_" << className << "_attr)  &" << iendl++;
534         SuperClassMap::const_iterator it;
535         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
536
537         long startPos = oss.tellp();
538
539         oss << "( " << className << "_id";
540         for (it = begin; it != end; it++)
541         {
542           oss << ", " << it->second->getName();
543           if (oss.tellp() - startPos > 90)
544           {
545             oss << "  &" << iendl;
546             startPos = oss.tellp();
547           }
548         }
549         oss << " )";
550         oss << std::endl;
551         oss << iendl;
552
553         oss << "IMPLICIT NONE" << iendl++;
554
555         oss << "TYPE(txios(" << className << "))  :: " << className << "_hdl" << iendl;
556         oss << "CHARACTER(LEN=*), INTENT(IN) ::" << className << "_id";
557
558         for (it = begin; it != end; it++)
559         {
560           oss << iendl;
561           it->second->generateFortranInterfaceDeclaration(oss, className);
562         }
563
564         oss << std::endl << iendl;
565         oss << "CALL xios(get_" << className << "_handle)(" << className << "_id," << className << "_hdl)" << iendl;
566         oss << "CALL xios(set_" << className << "_attr_hdl_)   &" << iendl;
567
568         startPos = oss.tellp();
569
570         oss << "( " << className << "_hdl";
571         for (it = begin; it != end; it++)
572         {
573           oss << ", " << it->second->getName();
574           if (oss.tellp() - startPos > 90)
575           {
576             oss << "  &" << iendl;
577             startPos = oss.tellp();
578           }
579         }
580         oss << " )";
581
582         oss << std::endl << (iendl -= 2);
583         oss << "END SUBROUTINE xios(set_" << className << "_attr)" << std::endl;
584      }
585
586      void CAttributeMap::generateFortranInterfaceGet_id(ostream& oss, const string& className)
587      {
588         oss << "SUBROUTINE xios(get_" << className << "_attr)  &" << iendl++;
589         SuperClassMap::const_iterator it;
590         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
591
592         long startPos = oss.tellp();
593
594         oss << "( " << className << "_id";
595         for (it = begin; it != end; it++)
596         {
597           oss << ", " << it->second->getName();
598           if (oss.tellp() - startPos > 90)
599           {
600             oss << "  &" << iendl;
601             startPos = oss.tellp();
602           }
603         }
604         oss << " )";
605         oss << std::endl;
606         oss << iendl;
607
608         oss << "IMPLICIT NONE" << iendl++;
609
610         oss << "TYPE(txios(" << className << "))  :: " << className << "_hdl" << iendl;
611         oss << "CHARACTER(LEN=*), INTENT(IN) ::" << className << "_id";
612
613         for (it = begin; it != end; it++)
614         {
615           oss << iendl;
616           it->second->generateFortranInterfaceGetDeclaration(oss, className);
617         }
618
619         oss << std::endl << iendl;
620         oss << "CALL xios(get_" << className << "_handle)(" << className << "_id," << className << "_hdl)" << iendl;
621         oss << "CALL xios(get_" << className << "_attr_hdl_)   &" << iendl;
622
623         startPos = oss.tellp();
624
625         oss << "( " << className << "_hdl";
626         for (it = begin; it != end; it++)
627         {
628           oss << ", " << it->second->getName();
629           if (oss.tellp() - startPos > 90)
630           {
631             oss << "  &" << iendl;
632             startPos = oss.tellp();
633           }
634         }
635         oss << " )";
636
637         oss << std::endl << (iendl -= 2);
638         oss << "END SUBROUTINE xios(get_" << className << "_attr)" << std::endl;
639      }
640
641      void CAttributeMap::generateFortranInterfaceIsDefined_id(ostream& oss, const string& className)
642      {
643         oss << "SUBROUTINE xios(is_defined_" << className << "_attr)  &" << iendl++;
644         SuperClassMap::const_iterator it;
645         SuperClassMap::const_iterator begin = SuperClassMap::begin(), end = SuperClassMap::end();
646
647         long startPos = oss.tellp();
648
649         oss << "( " << className << "_id";
650         for (it = begin; it != end; it++)
651         {
652           oss << ", " << it->second->getName();
653           if (oss.tellp() - startPos > 90)
654           {
655             oss << "  &" << iendl;
656             startPos = oss.tellp();
657           }
658         }
659         oss << " )";
660         oss << std::endl;
661         oss << iendl;
662
663         oss << "IMPLICIT NONE" << iendl++;
664
665         oss << "TYPE(txios(" << className << "))  :: " << className << "_hdl" << iendl;
666         oss << "CHARACTER(LEN=*), INTENT(IN) ::" << className << "_id";
667
668         for (it = begin; it != end; it++)
669         {
670           oss << iendl;
671           it->second->generateFortranInterfaceIsDefinedDeclaration(oss, className);
672         }
673
674         oss << std::endl << iendl;
675         oss << "CALL xios(get_" << className << "_handle)(" << className << "_id," << className << "_hdl)" << iendl;
676         oss << "CALL xios(is_defined_" << className << "_attr_hdl_)   &" << iendl;
677
678         startPos = oss.tellp();
679
680         oss << "( " << className << "_hdl";
681         for (it = begin; it != end; it++)
682         {
683           oss << ", " << it->second->getName();
684           if (oss.tellp() - startPos > 90)
685           {
686             oss << "  &" << iendl;
687             startPos = oss.tellp();
688           }
689         }
690         oss << " )";
691
692         oss << std::endl << (iendl -= 2);
693         oss << "END SUBROUTINE xios(is_defined_" << className << "_attr)" << std::endl;
694      }
695} // namespace xios
Note: See TracBrowser for help on using the repository browser.