1 | // Classe MuTyV : Variable multi-type numerique
|
---|
2 | // R. Ansari 1997-2000
|
---|
3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
4 |
|
---|
5 |
|
---|
6 | #include "mutyv.h"
|
---|
7 | #include <stdio.h>
|
---|
8 |
|
---|
9 | #include <iostream.h>
|
---|
10 | #include <string.h>
|
---|
11 |
|
---|
12 | /*!
|
---|
13 | \class SOPHYA::MuTyV
|
---|
14 | \ingroup SysTools
|
---|
15 | Simple utility class which can be used to hold values of type
|
---|
16 | string, integer (\b int_8) or float (\b r_8) as well as complex,
|
---|
17 | It provides also easy conversion methods between these types.
|
---|
18 |
|
---|
19 | \code
|
---|
20 | // ------- Using MuTyV objects -------
|
---|
21 | MuTyV mvu; // MuTyV variable declaration
|
---|
22 | mvu = 60; // mvu contains the integer value 60
|
---|
23 | mvu = 66.6; // and now the double value 66.6
|
---|
24 | string ds = mvu; // ds contains the string "66.6"
|
---|
25 | MuTyV mvi(14); // New MuTyV variable containing integer value 14
|
---|
26 | r_4 x = mvi; // x has the value 14.0
|
---|
27 | MuTyV mvs("Bonjour !"); // mvs contains the string "Bonjour !"
|
---|
28 | string s = mvs; // s contains "Bonjour !"
|
---|
29 |
|
---|
30 | \endcode
|
---|
31 | */
|
---|
32 |
|
---|
33 |
|
---|
34 | /* --Methode-- */
|
---|
35 | MuTyV::MuTyV(MuTyV const & a)
|
---|
36 | {
|
---|
37 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
38 | if (typ == 'S') strv = new string(*(a.strv));
|
---|
39 | else strv = NULL;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /* --Methode-- */
|
---|
43 | MuTyV::~MuTyV()
|
---|
44 | {
|
---|
45 | if (strv) delete strv;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /* --Methode-- */
|
---|
49 | MuTyV::MuTyV(char const* s)
|
---|
50 | {
|
---|
51 | typ = 'S';
|
---|
52 | strv = new string(s);
|
---|
53 | dv = dv_im = 0.;
|
---|
54 | sscanf(s, "%lg %lg", &dv, &dv_im);
|
---|
55 | iv = (int_8)dv;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /* --Methode-- */
|
---|
59 | MuTyV::MuTyV(string const& s)
|
---|
60 | {
|
---|
61 | typ = 'S';
|
---|
62 | strv = new string(s);
|
---|
63 | dv = dv_im = 0.;
|
---|
64 | sscanf(s.c_str(), "%lg %lg", &dv, &dv_im);
|
---|
65 | iv = (int_8)dv;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* --Methode-- */
|
---|
69 | MuTyV & MuTyV::operator= (MuTyV const & a)
|
---|
70 | {
|
---|
71 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
72 | if (typ == 'S') strv = new string(*(a.strv));
|
---|
73 | else strv = NULL;
|
---|
74 | return(*this);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* --Methode-- */
|
---|
78 | char * MuTyV::operator= (char* s)
|
---|
79 | {
|
---|
80 | typ = 'S';
|
---|
81 | strv = new string(s);
|
---|
82 | dv = dv_im = 0.;
|
---|
83 | sscanf(s, "%lg %lg", &dv, &dv_im);
|
---|
84 | iv = (int_8)dv;
|
---|
85 | return(s);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* --Methode-- */
|
---|
89 | string & MuTyV::operator= (string& s)
|
---|
90 | {
|
---|
91 | typ = 'S';
|
---|
92 | strv = new string(s);
|
---|
93 | dv = dv_im = 0.;
|
---|
94 | sscanf(s.c_str(), "%lg %lg", &dv, &dv_im);
|
---|
95 | iv = (int_8)dv;
|
---|
96 | return(s);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* --Methode-- */
|
---|
100 | MuTyV::operator string() const
|
---|
101 | {
|
---|
102 | if (typ == 'S') return(*strv);
|
---|
103 | else {
|
---|
104 | char buff[96];
|
---|
105 | if (typ == 'I') sprintf(buff,"%ld", (long)iv);
|
---|
106 | else if (typ == 'D') sprintf(buff,"%.20g", dv);
|
---|
107 | else if (typ == 'Z') sprintf(buff,"%.20g %.20g i", dv, dv_im);
|
---|
108 | else buff[0] = '\0';
|
---|
109 | return(string(buff));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|