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 "ppersist.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 | #if defined(__KCC__)
|
---|
19 | using std::string ;
|
---|
20 | #include <list.h>
|
---|
21 | #include <map.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | // Classe utilitaire pour manipuler des variables typees
|
---|
25 | class MuTyV {
|
---|
26 | public:
|
---|
27 | union {
|
---|
28 | int_4 iv;
|
---|
29 | double dv;
|
---|
30 | char strv[31];
|
---|
31 | } mtv;
|
---|
32 | char typ;
|
---|
33 |
|
---|
34 | static char myStrBuf[64];
|
---|
35 |
|
---|
36 | inline MuTyV() { typ = 'I'; mtv.iv = 0; mtv.dv = 0.; mtv.strv[0] ='\0'; }
|
---|
37 | inline MuTyV(int_4 i) { typ = 'I'; mtv.iv = i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
|
---|
38 | inline MuTyV(float f) { typ = 'D'; mtv.dv = (double)f; mtv.iv = 0; mtv.strv[0] ='\0'; }
|
---|
39 | inline MuTyV(double d) { typ = 'D'; mtv.dv = d; mtv.iv = 0; mtv.strv[0] ='\0'; }
|
---|
40 | inline MuTyV(char const* s) { typ = 'S'; strncpy(mtv.strv, s, 31); mtv.strv[30] = '\0'; mtv.iv = 0; mtv.dv = 0.; }
|
---|
41 | inline MuTyV(string const& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31); mtv.strv[30] = '\0'; mtv.iv = 0; mtv.dv = 0.; }
|
---|
42 | inline int_4 operator= (int_4 v) { typ = 'I'; mtv.iv = v; return(v); }
|
---|
43 | inline float operator= (float v) { typ = 'D'; mtv.dv = (double)v; return(v); }
|
---|
44 | inline double operator= (double v) { typ = 'D'; mtv.dv = v; return(v); }
|
---|
45 | inline char* operator= (char* s) { typ = 'S'; strncpy(mtv.strv, s, 31);
|
---|
46 | mtv.strv[30] = '\0'; return(s); }
|
---|
47 | inline string& operator= (string& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31);
|
---|
48 | mtv.strv[30] = '\0'; return(s); }
|
---|
49 |
|
---|
50 | inline operator int_4() { if (typ == 'I') return(mtv.iv);
|
---|
51 | else if (typ == 'D') return((int_4)mtv.dv);
|
---|
52 | else return(atol(mtv.strv)); }
|
---|
53 | inline operator float() { if (typ == 'I') return((float)mtv.iv);
|
---|
54 | else if (typ == 'D') return((float)mtv.dv);
|
---|
55 | else return((float)atof(mtv.strv)); }
|
---|
56 | inline operator double() { if (typ == 'I') return((double)mtv.iv);
|
---|
57 | else if (typ == 'D') return(mtv.dv);
|
---|
58 | else return(atof(mtv.strv)); }
|
---|
59 | inline operator string() { char *ss=myStrBuf;
|
---|
60 | if (typ == 'I') sprintf(ss,"%d", mtv.iv);
|
---|
61 | else if (typ == 'D') sprintf(ss,"%.20g", mtv.dv);
|
---|
62 | else ss = mtv.strv;
|
---|
63 | return(ss); }
|
---|
64 | };
|
---|
65 |
|
---|
66 | // Classe liste de variables Dynamic Variable List
|
---|
67 |
|
---|
68 | class DVList : public PPersist {
|
---|
69 | public:
|
---|
70 | enum {classId = ClassId_DVList };
|
---|
71 |
|
---|
72 | DVList();
|
---|
73 | DVList(DVList&);
|
---|
74 | DVList(char *flnm);
|
---|
75 |
|
---|
76 | virtual ~DVList();
|
---|
77 |
|
---|
78 | DVList& operator= (const DVList&);
|
---|
79 |
|
---|
80 | void Clear();
|
---|
81 | DVList& Merge(const DVList&);
|
---|
82 |
|
---|
83 | int_4 GetI(string const& key, int_4 def=-1);
|
---|
84 | double GetD(string const& key, double def=-9.e19);
|
---|
85 | string GetS(string const& key, char* def="");
|
---|
86 |
|
---|
87 | void SetI(string const& key, int_4 val);
|
---|
88 | void SetD(string const& key, double val);
|
---|
89 | void SetS(string const& key, char const* val);
|
---|
90 | void SetS(string const& key, string val);
|
---|
91 |
|
---|
92 | MuTyV& Get(string const& key);
|
---|
93 | inline MuTyV& operator() (string const& key) { return Get(key); }
|
---|
94 | inline MuTyV& operator[] (string const& key) { return Get(key); }
|
---|
95 | inline string& Comment() { return(comment); }
|
---|
96 |
|
---|
97 | inline void Print() const { Print(cout); }
|
---|
98 | virtual void Print(ostream& os) const;
|
---|
99 |
|
---|
100 | int_4 ClassId() const { return classId; }
|
---|
101 | static PPersist* Create() { return new DVList;}
|
---|
102 |
|
---|
103 | virtual void WriteSelf(POutPersist&) const;
|
---|
104 | virtual void ReadSelf(PInPersist&);
|
---|
105 |
|
---|
106 | private:
|
---|
107 | typedef map<string, MuTyV, less<string> > ValList;
|
---|
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 | #endif /* DVLIST_H__SEEN */
|
---|
117 |
|
---|
118 |
|
---|