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

Last change on this file since 335 was 335, checked in by ymipsl, 12 years ago

Change namespace xmlioserver -> xios

YM

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