source: Sophya/trunk/SophyaLib/NTools/dvlist.h@ 433

Last change on this file since 433 was 404, checked in by ansari, 26 years ago

Bug ds DVList corrige ds PEIDA Reza 10/9/99

File size: 4.6 KB
Line 
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#if defined(__KCC__)
19using std::string ;
20#include <list.h>
21#include <map.h>
22#endif
23
24namespace PlanckDPC {
25
26// Classe utilitaire pour manipuler des variables typees
27class MuTyV {
28public:
29 union {
30 int_4 iv;
31 double dv;
32 char strv[31];
33 } mtv;
34 char typ;
35
36 static char myStrBuf[64];
37
38 inline MuTyV() { typ = 'I'; mtv.iv = 0; mtv.dv = 0.; mtv.strv[0] ='\0'; }
39 inline MuTyV(int_4 i) { typ = 'I'; mtv.iv = i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
40 inline MuTyV(float f) { typ = 'D'; mtv.dv = (double)f; mtv.iv = 0; mtv.strv[0] ='\0'; }
41 inline MuTyV(double d) { typ = 'D'; mtv.dv = d; mtv.iv = 0; mtv.strv[0] ='\0'; }
42 inline MuTyV(char const* s) { typ = 'S'; strncpy(mtv.strv, s, 31); mtv.strv[30] = '\0'; }
43 inline MuTyV(string const& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31); mtv.strv[30] = '\0'; }
44 inline int_4 operator= (int_4 v) { typ = 'I'; mtv.iv = v; return(v); }
45 inline float operator= (float v) { typ = 'D'; mtv.dv = (double)v; return(v); }
46 inline double operator= (double v) { typ = 'D'; mtv.dv = v; return(v); }
47 inline char* operator= (char* s) { typ = 'S'; strncpy(mtv.strv, s, 31);
48 mtv.strv[30] = '\0'; return(s); }
49 inline string& operator= (string& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31);
50 mtv.strv[30] = '\0'; return(s); }
51
52 inline operator int_4() { if (typ == 'I') return(mtv.iv);
53 else if (typ == 'D') return((int_4)mtv.dv);
54 else return(atol(mtv.strv)); }
55 inline operator float() { if (typ == 'I') return((float)mtv.iv);
56 else if (typ == 'D') return((float)mtv.dv);
57 else return((float)atof(mtv.strv)); }
58 inline operator double() { if (typ == 'I') return((double)mtv.iv);
59 else if (typ == 'D') return(mtv.dv);
60 else return(atof(mtv.strv)); }
61 inline operator string() { char *ss=myStrBuf;
62 if (typ == 'I') sprintf(ss,"%d", mtv.iv);
63 else if (typ == 'D') sprintf(ss,"%.20g", mtv.dv);
64 else ss = mtv.strv;
65 return(ss); }
66};
67
68// Classe liste de variables Dynamic Variable List
69
70class DVList : public AnyDataObj {
71public:
72// enum {classId = ClassId_DVList };
73
74 DVList();
75 DVList(DVList&);
76 DVList(char *flnm);
77
78 virtual ~DVList();
79
80 DVList& operator= (const DVList&);
81
82 void Clear();
83 DVList& Merge(const DVList&);
84
85 int_4 GetI(string const& key, int_4 def=-1);
86 double GetD(string const& key, double def=-9.e19);
87 string GetS(string const& key, char* def="");
88
89 void SetI(string const& key, int_4 val);
90 void SetD(string const& key, double val);
91 void SetS(string const& key, char const* val);
92 void SetS(string const& key, string val);
93
94 MuTyV& Get(string const& key);
95 inline MuTyV& operator() (string const& key) { return Get(key); }
96 inline MuTyV& operator[] (string const& key) { return Get(key); }
97 inline string& Comment() { return(comment); }
98
99 inline void Print() const { Print(cout); }
100 virtual void Print(ostream& os) const;
101
102 typedef map<string, MuTyV, less<string> > ValList;
103 inline ValList::const_iterator Begin() { return(mvlist.begin()); }
104 inline ValList::const_iterator End() { return(mvlist.end()); }
105
106// int_4 ClassId() const { return classId; }
107// static PPersist* Create() { return new DVList;}
108
109// virtual void WriteSelf(POutPersist&) const;
110// virtual void ReadSelf(PInPersist&);
111
112private:
113
114 ValList mvlist;
115 string comment;
116};
117
118inline ostream& operator << (ostream& s, DVList const & dvl)
119 { dvl.Print(s); return(s); }
120
121inline POutPersist& operator << (POutPersist& os, DVList & obj)
122{ ObjFileIO<DVList> fio(&obj); fio.Write(os); return(os); }
123inline PInPersist& operator >> (PInPersist& is, DVList & obj)
124{ ObjFileIO<DVList> fio(&obj); fio.Read(is); return(is); }
125
126// Classe pour la gestion de persistance
127// ObjFileIO<DVList>
128
129} // namespace PlanckDPC
130
131#endif /* DVLIST_H__SEEN */
132
133
Note: See TracBrowser for help on using the repository browser.