Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
DefaultHepRepAttValue.cc
Go to the documentation of this file.
1// Copyright FreeHEP, 2005.
2
3#include "cheprep/config.h"
4
5#include <cstdlib>
6#include <cstring>
7#include <cctype>
8#include <cstdio>
9#include <iostream>
10#include <algorithm>
11
13
15
16using namespace std;
17using namespace HEPREP;
18
19/**
20 * @author Mark Donszelmann
21 */
22namespace cheprep {
23
24std::string DefaultHepRepAttValue::labelStrings[LABELSTRINGS_LEN];
25
26DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, string aValue, int aShowLabel)
27 : name(aName)
28 , type(HepRepConstants::TYPE_STRING)
29 , stringValue(aValue)
30 , longValue(0)
31 , doubleValue(0.)
32 , booleanValue(false)
33 , showLabelValue(aShowLabel)
34 {
35 init();
36 }
37
38DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int64 aValue, int aShowLabel)
39 : name(aName)
40 , type(HepRepConstants::TYPE_LONG)
41 , longValue(aValue)
42 , doubleValue(0.)
43 , booleanValue(false)
44 , showLabelValue(aShowLabel)
45 {
46 init();
47 }
48
49DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int aValue, int aShowLabel)
50 : name(aName)
51 , type(HepRepConstants::TYPE_INT)
52 , longValue(aValue)
53 , doubleValue(0.)
54 , booleanValue(false)
55 , showLabelValue(aShowLabel)
56 {
57 init();
58 }
59
60DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, double aValue, int aShowLabel)
61 : name(aName)
62 , type(HepRepConstants::TYPE_DOUBLE)
63 , longValue(0)
64 , doubleValue(aValue)
65 , booleanValue(false)
66 , showLabelValue(aShowLabel)
67 {
68 init();
69 }
70
71DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, bool aValue, int aShowLabel)
72 : name(aName)
73 , type(HepRepConstants::TYPE_BOOLEAN)
74 , longValue(0)
75 , doubleValue(0.)
76 , booleanValue(aValue)
77 , showLabelValue(aShowLabel)
78 {
79 init();
80 }
81
82DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, vector<double> aValue, int aShowLabel)
83 : name(aName)
84 , type(HepRepConstants::TYPE_COLOR)
85 , longValue(0)
86 , doubleValue(0.)
87 , booleanValue(false)
88 , colorValue(aValue)
89 , showLabelValue(aShowLabel)
90 {
91 init();
92 }
93
95}
96
97void DefaultHepRepAttValue::init() {
98 labelStrings[0] = "NAME";
99 labelStrings[1] = "DESC";
100 labelStrings[2] = "VALUE";
101 labelStrings[3] = "EXTRA";
102}
103
105 // Coverity issues warnings here. It looks to me (JA) that "return" is
106 // missing on severeal lines.
107 switch(type) {
108// case HepRepConstants::TYPE_COLOR: new DefaultHepRepAttValue(name, colorValue, showLabelValue);
109// case HepRepConstants::TYPE_STRING: new DefaultHepRepAttValue(name, stringValue, showLabelValue);
110// case HepRepConstants::TYPE_LONG: new DefaultHepRepAttValue(name, longValue, showLabelValue);
111// case HepRepConstants::TYPE_INT: new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
112// case HepRepConstants::TYPE_DOUBLE: new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
113// case HepRepConstants::TYPE_BOOLEAN: new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
114 case HepRepConstants::TYPE_COLOR: return new DefaultHepRepAttValue(name, colorValue, showLabelValue);
115 case HepRepConstants::TYPE_STRING: return new DefaultHepRepAttValue(name, stringValue, showLabelValue);
116 case HepRepConstants::TYPE_LONG: return new DefaultHepRepAttValue(name, longValue, showLabelValue);
117 case HepRepConstants::TYPE_INT: return new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
118 case HepRepConstants::TYPE_DOUBLE: return new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
119 case HepRepConstants::TYPE_BOOLEAN: return new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
120 default: return new DefaultHepRepAttValue(name, "Unknown type stored in HepRepAttDef", showLabelValue);
121 }
122}
123
125 return name;
126}
127
129 string s = name;
130 transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
131 return s;
132}
133
135 return type;
136}
137
139 switch(type) {
140 case HepRepConstants::TYPE_COLOR: return("Color");
141 case HepRepConstants::TYPE_STRING: return("String");
142 case HepRepConstants::TYPE_LONG: return("long");
143 case HepRepConstants::TYPE_INT: return("int");
144 case HepRepConstants::TYPE_DOUBLE: return("double");
145 case HepRepConstants::TYPE_BOOLEAN: return("boolean");
146 default: return "Unknown type stored in HepRepAttDef";
147 }
148}
149
151 return showLabelValue;
152}
153
155 if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
156 return stringValue;
157}
158
160 if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
161 string s = stringValue;
162 transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
163 return s;
164}
165
167 if (type != HepRepConstants::TYPE_LONG) cerr << "Trying to access AttValue '" << getName() << "' as 'long'" << endl;
168 return longValue;
169}
170
172 if (type != HepRepConstants::TYPE_INT) cerr << "Trying to access AttValue '" << getName() << "' as 'int'" << endl;
173 return (int64)longValue;
174}
175
177 if (type != HepRepConstants::TYPE_DOUBLE) cerr << "Trying to access AttValue '" << getName() << "' as 'double'" << endl;
178 return doubleValue;
179}
180
182 if (type != HepRepConstants::TYPE_BOOLEAN) cerr << "Trying to access AttValue '" << getName() << "' as 'boolean'" << endl;
183 return booleanValue;
184}
185
187 if (type != HepRepConstants::TYPE_COLOR) cerr << "Trying to access AttValue '" << getName() << "' as 'color'" << endl;
188 return colorValue;
189}
190
191
193 switch(type) {
200 default: return "Unknown typecode";
201 }
202}
203
204string DefaultHepRepAttValue::getAsString(vector<double> c) {
205 char buffer[40];
206 sprintf(buffer, "%4.2f, %4.2f, %4.2f, %4.2f",
207 c[0],
208 c[1],
209 c[2],
210 (c.size() > 3) ? c[3] : 1.0);
211 return buffer;
212}
213
215 char buffer[40];
216 sprintf(buffer, "%d", i);
217 return buffer;
218}
219
221 char buffer[40];
222 sprintf(buffer, CHEPREP_INT64_FORMAT, i);
223 return buffer;
224}
225
226// NOTE: keep at %g rather than %lg for backward compatibility
228 char buffer[40];
229 sprintf(buffer, "%g", d);
230 return buffer;
231}
232
234 return b ? "true" : "false";
235}
236
237
238
239
241 return toShowLabel(showLabel());
242}
243
244
246 string label = "";
247 bool first = true;
249 label = "NONE";
250 } else {
251 for (int i=0; i<16; i++) {
252 if (((showLabel >> i) & 0x0001) == 0x0001) {
253 if (first) {
254 first = false;
255 } else {
256 label.append(", ");
257 }
258 if (i < LABELSTRINGS_LEN) {
259 label.append(labelStrings[i]);
260 } else {
261 char hex[20];
262 sprintf(hex, "%0x", 1 << i);
263 label.append(hex);
264 }
265 }
266 }
267 }
268 return label;
269}
270
271} // cheprep
HEPREP::HepRepAttValue * copy()
DefaultHepRepAttValue(std::string name, std::string value, int showLabel)
#define CHEPREP_INT64_FORMAT
Definition: config.h:32
long long int64
Definition: config.h:30
#define buffer
Definition: xmlparse.cc:628