| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | // Classe DVList : Liste dynamique de variables (valeurs) | 
|---|
| 3 | // identifiees par un nom    Reza Ansari - Dec 96 | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef DVLIST_H_SEEN | 
|---|
| 6 | #define DVLIST_H_SEEN | 
|---|
| 7 |  | 
|---|
| 8 | #include "objfio.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include <stdio.h> | 
|---|
| 11 |  | 
|---|
| 12 | #include <iostream.h> | 
|---|
| 13 |  | 
|---|
| 14 | #include <list> | 
|---|
| 15 | #include <map> | 
|---|
| 16 | #include <string.h> | 
|---|
| 17 | #include <string> | 
|---|
| 18 |  | 
|---|
| 19 | namespace SOPHYA { | 
|---|
| 20 |  | 
|---|
| 21 | // Classe utilitaire pour manipuler des variables typees | 
|---|
| 22 | class MuTyV { | 
|---|
| 23 | public: | 
|---|
| 24 | union { | 
|---|
| 25 | int_4 iv; | 
|---|
| 26 | double dv; | 
|---|
| 27 | char strv[31]; | 
|---|
| 28 | } mtv; | 
|---|
| 29 | char typ; | 
|---|
| 30 |  | 
|---|
| 31 | static char myStrBuf[64]; | 
|---|
| 32 |  | 
|---|
| 33 | inline MuTyV() { typ = 'I'; mtv.iv = 0;  mtv.dv = 0.;  mtv.strv[0] ='\0'; } | 
|---|
| 34 | inline MuTyV(int_4 i) { typ = 'I'; mtv.iv = i; mtv.dv = 0.;  mtv.strv[0] ='\0'; } | 
|---|
| 35 | inline MuTyV(float f) { typ = 'D'; mtv.dv = (double)f;  mtv.iv = 0;  mtv.strv[0] ='\0'; } | 
|---|
| 36 | inline MuTyV(double d) { typ = 'D'; mtv.dv = d; mtv.iv = 0;  mtv.strv[0] ='\0'; } | 
|---|
| 37 | inline MuTyV(char const* s)  { typ = 'S'; strncpy(mtv.strv, s, 31); mtv.strv[30] = '\0'; } | 
|---|
| 38 | inline MuTyV(string const& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31); mtv.strv[30] = '\0'; } | 
|---|
| 39 | inline int_4  operator= (int_4 v)  { typ = 'I'; mtv.iv = v; return(v); } | 
|---|
| 40 | inline float  operator= (float v)  { typ = 'D'; mtv.dv = (double)v; return(v); } | 
|---|
| 41 | inline double operator= (double v) { typ = 'D'; mtv.dv = v; return(v); } | 
|---|
| 42 | inline char*  operator= (char* s)  { typ = 'S'; strncpy(mtv.strv, s, 31); | 
|---|
| 43 | mtv.strv[30] = '\0';  return(s); } | 
|---|
| 44 | inline string& operator= (string& s)    { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31); | 
|---|
| 45 | mtv.strv[30] = '\0'; return(s); } | 
|---|
| 46 |  | 
|---|
| 47 | inline operator int_4() { if (typ == 'I')  return(mtv.iv); | 
|---|
| 48 | else if (typ == 'D') return((int_4)mtv.dv); | 
|---|
| 49 | else return(atol(mtv.strv)); } | 
|---|
| 50 | inline operator float() { if (typ == 'I')  return((float)mtv.iv); | 
|---|
| 51 | else if (typ == 'D') return((float)mtv.dv); | 
|---|
| 52 | else return((float)atof(mtv.strv)); } | 
|---|
| 53 | inline operator double() { if (typ == 'I')  return((double)mtv.iv); | 
|---|
| 54 | else if (typ == 'D') return(mtv.dv); | 
|---|
| 55 | else return(atof(mtv.strv)); } | 
|---|
| 56 | inline operator string() { char *ss=myStrBuf; | 
|---|
| 57 | if (typ == 'I')  sprintf(ss,"%d", mtv.iv); | 
|---|
| 58 | else if (typ == 'D') sprintf(ss,"%.20g", mtv.dv); | 
|---|
| 59 | else ss = mtv.strv; | 
|---|
| 60 | return(ss); } | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | //  Classe liste de variables  Dynamic Variable List | 
|---|
| 64 |  | 
|---|
| 65 | class DVList : public AnyDataObj { | 
|---|
| 66 | public: | 
|---|
| 67 | //  enum {classId = ClassId_DVList }; | 
|---|
| 68 |  | 
|---|
| 69 | DVList(); | 
|---|
| 70 | DVList(const DVList&); | 
|---|
| 71 | DVList(char *flnm); | 
|---|
| 72 |  | 
|---|
| 73 | virtual           ~DVList(); | 
|---|
| 74 |  | 
|---|
| 75 | DVList&           operator= (const DVList&); | 
|---|
| 76 |  | 
|---|
| 77 | void              Clear(); | 
|---|
| 78 | DVList&           Merge(const DVList&); | 
|---|
| 79 |  | 
|---|
| 80 | int_4             GetI(string const& key, int_4 def=-1); | 
|---|
| 81 | double            GetD(string const& key, double def=-9.e19); | 
|---|
| 82 | string            GetS(string const& key, char* def=""); | 
|---|
| 83 |  | 
|---|
| 84 | void              SetI(string const& key, int_4 val); | 
|---|
| 85 | void              SetD(string const& key, double val); | 
|---|
| 86 | void              SetS(string const& key, char const*  val); | 
|---|
| 87 | void              SetS(string const& key, string val); | 
|---|
| 88 |  | 
|---|
| 89 | MuTyV&            Get(string const& key); | 
|---|
| 90 | inline MuTyV&     operator()  (string const& key)  { return Get(key); } | 
|---|
| 91 | inline MuTyV&     operator[]  (string const& key)  { return Get(key); } | 
|---|
| 92 | inline string&    Comment() { return(comment); } | 
|---|
| 93 |  | 
|---|
| 94 | inline void       Print() const  { Print(cout); } | 
|---|
| 95 | virtual void      Print(ostream& os)  const; | 
|---|
| 96 |  | 
|---|
| 97 | typedef map<string, MuTyV, less<string> >  ValList; | 
|---|
| 98 | inline ValList::const_iterator Begin() { return(mvlist.begin()); } | 
|---|
| 99 | inline ValList::const_iterator End() { return(mvlist.end()); } | 
|---|
| 100 |  | 
|---|
| 101 | //  int_4             ClassId() const        { return classId; } | 
|---|
| 102 | //  static PPersist*  Create()               { return new DVList;} | 
|---|
| 103 |  | 
|---|
| 104 | //  virtual void      WriteSelf(POutPersist&) const; | 
|---|
| 105 | //  virtual void      ReadSelf(PInPersist&); | 
|---|
| 106 |  | 
|---|
| 107 | private: | 
|---|
| 108 |  | 
|---|
| 109 | ValList mvlist; | 
|---|
| 110 | string comment; | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | inline ostream& operator << (ostream& s, DVList const & dvl) | 
|---|
| 114 | {  dvl.Print(s);  return(s);  } | 
|---|
| 115 |  | 
|---|
| 116 | inline POutPersist& operator << (POutPersist& os, DVList & obj) | 
|---|
| 117 | { ObjFileIO<DVList> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| 118 | inline PInPersist& operator >> (PInPersist& is, DVList & obj) | 
|---|
| 119 | { ObjFileIO<DVList> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
| 120 |  | 
|---|
| 121 | // Classe pour la gestion de persistance | 
|---|
| 122 | // ObjFileIO<DVList> | 
|---|
| 123 |  | 
|---|
| 124 | } // namespace SOPHYA | 
|---|
| 125 |  | 
|---|
| 126 | #endif /* DVLIST_H__SEEN */ | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|