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

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

modif mineure ds DVList suite au mail D. Yvon - Reza 22/02/2000

File size: 5.1 KB
RevLine 
[220]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
[278]8#include "objfio.h"
[220]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
[552]19namespace SOPHYA {
[278]20
[220]21// Classe utilitaire pour manipuler des variables typees
22class MuTyV {
23public:
24 union {
[718]25 int_8 iv;
26 r_8 dv;
[220]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'; }
[718]34 inline MuTyV(int_4 i) { typ = 'I'; mtv.iv = (int_8)i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
35 inline MuTyV(int_8 i) { typ = 'I'; mtv.iv = i; mtv.dv = 0.; mtv.strv[0] ='\0'; }
36 inline MuTyV(r_4 f) { typ = 'D'; mtv.dv = (r_8)f; mtv.iv = 0; mtv.strv[0] ='\0'; }
37 inline MuTyV(r_8 d) { typ = 'D'; mtv.dv = d; mtv.iv = 0; mtv.strv[0] ='\0'; }
[404]38 inline MuTyV(char const* s) { typ = 'S'; strncpy(mtv.strv, s, 31); mtv.strv[30] = '\0'; }
39 inline MuTyV(string const& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31); mtv.strv[30] = '\0'; }
[718]40 inline int_4 operator= (int_4 v) { typ = 'I'; mtv.iv = (int_8)v; return(v); }
41 inline int_8 operator= (int_8 v) { typ = 'I'; mtv.iv = v; return(v); }
42 inline r_4 operator= (r_4 v) { typ = 'D'; mtv.dv = (r_8)v; return(v); }
43 inline r_8 operator= (r_8 v) { typ = 'D'; mtv.dv = v; return(v); }
[220]44 inline char* operator= (char* s) { typ = 'S'; strncpy(mtv.strv, s, 31);
45 mtv.strv[30] = '\0'; return(s); }
46 inline string& operator= (string& s) { typ = 'S'; strncpy(mtv.strv, s.c_str(), 31);
47 mtv.strv[30] = '\0'; return(s); }
48
[718]49 inline operator int_4() { if (typ == 'I') return((int_4)mtv.iv);
[220]50 else if (typ == 'D') return((int_4)mtv.dv);
51 else return(atol(mtv.strv)); }
[718]52 inline operator int_8() { if (typ == 'I') return((int_8)mtv.iv);
53 else if (typ == 'D') return((int_8)mtv.dv);
54 else return(atol(mtv.strv)); }
55 inline operator r_4() { if (typ == 'I') return((r_4)mtv.iv);
56 else if (typ == 'D') return((r_4)mtv.dv);
57 else return((r_4)atof(mtv.strv)); }
58 inline operator r_8() { if (typ == 'I') return((r_8)mtv.iv);
[220]59 else if (typ == 'D') return(mtv.dv);
60 else return(atof(mtv.strv)); }
61 inline operator string() { char *ss=myStrBuf;
[735]62 if (typ == 'I') sprintf(ss,"%ld", mtv.iv);
[220]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
[278]70class DVList : public AnyDataObj {
[220]71public:
[278]72// enum {classId = ClassId_DVList };
[220]73
74 DVList();
[515]75 DVList(const DVList&);
[220]76 DVList(char *flnm);
77
78 virtual ~DVList();
79
80 DVList& operator= (const DVList&);
81
82 void Clear();
83 DVList& Merge(const DVList&);
84
[718]85 int_8 GetI(string const& key, int_8 def=-1);
86 r_8 GetD(string const& key, r_8 def=-9.e19);
[220]87 string GetS(string const& key, char* def="");
[718]88 string GetComment(string const& key);
[220]89
[718]90 void SetI(string const& key, int_8 val);
91 void SetD(string const& key, r_8 val);
[220]92 void SetS(string const& key, char const* val);
93 void SetS(string const& key, string val);
[718]94 void SetComment(string const& key, string const& comm);
[220]95
96 MuTyV& Get(string const& key);
97 inline MuTyV& operator() (string const& key) { return Get(key); }
98 inline MuTyV& operator[] (string const& key) { return Get(key); }
99 inline string& Comment() { return(comment); }
100
101 inline void Print() const { Print(cout); }
102 virtual void Print(ostream& os) const;
[278]103
[718]104// Chaque element dans un DVList est constitue desormais d'un MuTyV
105// et d'une chaine de caracteres (commentaire) regroupe dans la structure
106// dvlElement. Ces elements sont associes aux noms de variables dans un
107// map<...> ValList. Reza 02/2000
108
109 struct dvlElement {MuTyV elval; string elcomm; } ;
110 typedef map<string, dvlElement, less<string> > ValList;
[278]111 inline ValList::const_iterator Begin() { return(mvlist.begin()); }
112 inline ValList::const_iterator End() { return(mvlist.end()); }
[220]113
114
115private:
116
117 ValList mvlist;
118 string comment;
119};
120
121inline ostream& operator << (ostream& s, DVList const & dvl)
122 { dvl.Print(s); return(s); }
123
[278]124inline POutPersist& operator << (POutPersist& os, DVList & obj)
125{ ObjFileIO<DVList> fio(&obj); fio.Write(os); return(os); }
126inline PInPersist& operator >> (PInPersist& is, DVList & obj)
127{ ObjFileIO<DVList> fio(&obj); fio.Read(is); return(is); }
128
129// Classe pour la gestion de persistance
130// ObjFileIO<DVList>
131
[552]132} // namespace SOPHYA
[278]133
[220]134#endif /* DVLIST_H__SEEN */
135
136
Note: See TracBrowser for help on using the repository browser.