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_8 iv;
|
---|
26 | r_8 dv;
|
---|
27 | char strv[39];
|
---|
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(MuTyV const & a) { typ = a.typ; mtv = a.mtv; }
|
---|
35 | inline MuTyV(int_4 i) { typ = 'I'; mtv.iv = (int_8)i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
|
---|
36 | inline MuTyV(int_8 i) { typ = 'I'; mtv.iv = i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
|
---|
37 | inline MuTyV(r_4 f) { typ = 'D'; mtv.dv = (r_8)f; mtv.iv = 0; mtv.strv[0] ='\0'; }
|
---|
38 | inline MuTyV(r_8 d) { typ = 'D'; mtv.dv = d; mtv.iv = 0; mtv.strv[0] ='\0'; }
|
---|
39 | inline MuTyV(char const* s) { typ = 'S'; strncpy(mtv.strv, s, 39); mtv.strv[38] = '\0'; }
|
---|
40 | inline MuTyV(string const& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 39); mtv.strv[38] = '\0'; }
|
---|
41 | inline MuTyV & operator= (MuTyV const & a) { typ = a.typ; mtv = a.mtv; return(*this); }
|
---|
42 | inline int_4 operator= (int_4 v) { typ = 'I'; mtv.iv = (int_8)v; return(v); }
|
---|
43 | inline int_8 operator= (int_8 v) { typ = 'I'; mtv.iv = v; return(v); }
|
---|
44 | inline r_4 operator= (r_4 v) { typ = 'D'; mtv.dv = (r_8)v; return(v); }
|
---|
45 | inline r_8 operator= (r_8 v) { typ = 'D'; mtv.dv = v; return(v); }
|
---|
46 | inline char* operator= (char* s) { typ = 'S'; strncpy(mtv.strv, s, 39);
|
---|
47 | mtv.strv[38] = '\0'; return(s); }
|
---|
48 | inline string& operator= (string& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 39);
|
---|
49 | mtv.strv[38] = '\0'; return(s); }
|
---|
50 |
|
---|
51 | inline operator int_4() { if (typ == 'I') return((int_4)mtv.iv);
|
---|
52 | else if (typ == 'D') return((int_4)mtv.dv);
|
---|
53 | else return(atol(mtv.strv)); }
|
---|
54 | inline operator int_8() { if (typ == 'I') return((int_8)mtv.iv);
|
---|
55 | else if (typ == 'D') return((int_8)mtv.dv);
|
---|
56 | else return(atol(mtv.strv)); }
|
---|
57 | inline operator r_4() { if (typ == 'I') return((r_4)mtv.iv);
|
---|
58 | else if (typ == 'D') return((r_4)mtv.dv);
|
---|
59 | else return((r_4)atof(mtv.strv)); }
|
---|
60 | inline operator r_8() { if (typ == 'I') return((r_8)mtv.iv);
|
---|
61 | else if (typ == 'D') return(mtv.dv);
|
---|
62 | else return(atof(mtv.strv)); }
|
---|
63 | inline operator string() { char *ss=myStrBuf;
|
---|
64 | if (typ == 'I') sprintf(ss,"%ld", (long)mtv.iv);
|
---|
65 | else if (typ == 'D') sprintf(ss,"%.20g", mtv.dv);
|
---|
66 | else ss = mtv.strv;
|
---|
67 | return(ss); }
|
---|
68 | };
|
---|
69 |
|
---|
70 | // Classe liste de variables Dynamic Variable List
|
---|
71 |
|
---|
72 | //! Dynamic Variable List class.
|
---|
73 | class DVList : public AnyDataObj {
|
---|
74 | public:
|
---|
75 | // enum {classId = ClassId_DVList };
|
---|
76 |
|
---|
77 | DVList();
|
---|
78 | DVList(const DVList&);
|
---|
79 | DVList(char *flnm);
|
---|
80 |
|
---|
81 | virtual ~DVList();
|
---|
82 |
|
---|
83 | DVList& operator= (const DVList&);
|
---|
84 |
|
---|
85 | void Clear();
|
---|
86 | DVList& Merge(const DVList&);
|
---|
87 |
|
---|
88 | int_8 GetI(string const& key, int_8 def=-1);
|
---|
89 | r_8 GetD(string const& key, r_8 def=-9.e19);
|
---|
90 | string GetS(string const& key, char* def="");
|
---|
91 | string GetComment(string const& key);
|
---|
92 |
|
---|
93 | void SetI(string const& key, int_8 val);
|
---|
94 | void SetD(string const& key, r_8 val);
|
---|
95 | void SetS(string const& key, char const* val);
|
---|
96 | void SetS(string const& key, string val);
|
---|
97 | void SetComment(string const& key, string const& comm);
|
---|
98 |
|
---|
99 | MuTyV& Get(string const& key);
|
---|
100 | /*! Returns the value associated with the name \b key */
|
---|
101 | inline MuTyV& operator() (string const& key) { return Get(key); }
|
---|
102 | /*! Returns the value associated with the name \b key */
|
---|
103 | inline MuTyV& operator[] (string const& key) { return Get(key); }
|
---|
104 | /*! Returns the global comment string associated with the object */
|
---|
105 | inline string& Comment() { return(comment); }
|
---|
106 |
|
---|
107 | /*! Prints a brief description of object on \b cout */
|
---|
108 | inline void Show() const { Show(cout); }
|
---|
109 | virtual void Show(ostream& os) const;
|
---|
110 | /*! Prints the list of variables on \b cout */
|
---|
111 | inline void Print() const { Print(cout); }
|
---|
112 | virtual void Print(ostream& os) const;
|
---|
113 |
|
---|
114 | // Chaque element dans un DVList est constitue desormais d'un MuTyV
|
---|
115 | // et d'une chaine de caracteres (commentaire) regroupe dans la structure
|
---|
116 | // dvlElement. Ces elements sont associes aux noms de variables dans un
|
---|
117 | // map<...> ValList. Reza 02/2000
|
---|
118 |
|
---|
119 | struct dvlElement {MuTyV elval; string elcomm; } ;
|
---|
120 | typedef map<string, dvlElement, less<string> > ValList;
|
---|
121 | /*! Returns an iterator pointing on the first variable in the list */
|
---|
122 | inline ValList::const_iterator Begin() { return(mvlist.begin()); }
|
---|
123 | /*! Returns the iterator end value */
|
---|
124 | inline ValList::const_iterator End() { return(mvlist.end()); }
|
---|
125 |
|
---|
126 |
|
---|
127 | private:
|
---|
128 |
|
---|
129 | ValList mvlist;
|
---|
130 | string comment;
|
---|
131 | };
|
---|
132 |
|
---|
133 | /*! operator << overloading - Prints the list on the stream \b s */
|
---|
134 | inline ostream& operator << (ostream& s, DVList const & dvl)
|
---|
135 | { dvl.Print(s); return(s); }
|
---|
136 |
|
---|
137 | /*! Writes the object in the POutPersist stream \b os */
|
---|
138 | inline POutPersist& operator << (POutPersist& os, DVList & obj)
|
---|
139 | { ObjFileIO<DVList> fio(&obj); fio.Write(os); return(os); }
|
---|
140 | /*! Reads the object from the PInPersist stream \b is */
|
---|
141 | inline PInPersist& operator >> (PInPersist& is, DVList & obj)
|
---|
142 | { ObjFileIO<DVList> fio(&obj); fio.Read(is); return(is); }
|
---|
143 |
|
---|
144 | // Classe pour la gestion de persistance
|
---|
145 | // ObjFileIO<DVList>
|
---|
146 |
|
---|
147 | } // namespace SOPHYA
|
---|
148 |
|
---|
149 | #endif /* DVLIST_H__SEEN */
|
---|
150 |
|
---|
151 |
|
---|