Geant4  10.01.p02
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 
12 #include "HEPREP/HepRepConstants.h"
13 
14 #include "cheprep/DefaultHepRepAttValue.h"
15 
16 using namespace std;
17 using namespace HEPREP;
18 
23 namespace cheprep {
24 
25 std::string DefaultHepRepAttValue::labelStrings[LABELSTRINGS_LEN];
26 
27 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, string aValue, int aShowLabel)
28  : name(aName)
29  , type(HepRepConstants::TYPE_STRING)
30  , stringValue(aValue)
31  , longValue(0)
32  , doubleValue(0.)
33  , booleanValue(false)
34  , showLabelValue(aShowLabel)
35  {
36  init();
37  }
38 
39 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int64 aValue, int aShowLabel)
40  : name(aName)
41  , type(HepRepConstants::TYPE_LONG)
42  , longValue(aValue)
43  , doubleValue(0.)
44  , booleanValue(false)
45  , showLabelValue(aShowLabel)
46  {
47  init();
48  }
49 
50 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int aValue, int aShowLabel)
51  : name(aName)
52  , type(HepRepConstants::TYPE_INT)
53  , longValue(aValue)
54  , doubleValue(0.)
55  , booleanValue(false)
56  , showLabelValue(aShowLabel)
57  {
58  init();
59  }
60 
61 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, double aValue, int aShowLabel)
62  : name(aName)
63  , type(HepRepConstants::TYPE_DOUBLE)
64  , longValue(0)
65  , doubleValue(aValue)
66  , booleanValue(false)
67  , showLabelValue(aShowLabel)
68  {
69  init();
70  }
71 
72 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, bool aValue, int aShowLabel)
73  : name(aName)
74  , type(HepRepConstants::TYPE_BOOLEAN)
75  , longValue(0)
76  , doubleValue(0.)
77  , booleanValue(aValue)
78  , showLabelValue(aShowLabel)
79  {
80  init();
81  }
82 
83 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, vector<double> aValue, int aShowLabel)
84  : name(aName)
85  , type(HepRepConstants::TYPE_COLOR)
86  , longValue(0)
87  , doubleValue(0.)
88  , booleanValue(false)
89  , colorValue(aValue)
90  , showLabelValue(aShowLabel)
91  {
92  init();
93  }
94 
95 DefaultHepRepAttValue::~DefaultHepRepAttValue() {
96 }
97 
98 void DefaultHepRepAttValue::init() {
99  labelStrings[0] = "NAME";
100  labelStrings[1] = "DESC";
101  labelStrings[2] = "VALUE";
102  labelStrings[3] = "EXTRA";
103 }
104 
105 HepRepAttValue* DefaultHepRepAttValue::copy() {
106  // Coverity issues warnings here. It looks to me (JA) that "return" is
107  // missing on severeal lines.
108  switch(type) {
109 // case HepRepConstants::TYPE_COLOR: new DefaultHepRepAttValue(name, colorValue, showLabelValue);
110 // case HepRepConstants::TYPE_STRING: new DefaultHepRepAttValue(name, stringValue, showLabelValue);
111 // case HepRepConstants::TYPE_LONG: new DefaultHepRepAttValue(name, longValue, showLabelValue);
112 // case HepRepConstants::TYPE_INT: new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
113 // case HepRepConstants::TYPE_DOUBLE: new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
114 // case HepRepConstants::TYPE_BOOLEAN: new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
115  case HepRepConstants::TYPE_COLOR: return new DefaultHepRepAttValue(name, colorValue, showLabelValue);
116  case HepRepConstants::TYPE_STRING: return new DefaultHepRepAttValue(name, stringValue, showLabelValue);
117  case HepRepConstants::TYPE_LONG: return new DefaultHepRepAttValue(name, longValue, showLabelValue);
118  case HepRepConstants::TYPE_INT: return new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
119  case HepRepConstants::TYPE_DOUBLE: return new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
120  case HepRepConstants::TYPE_BOOLEAN: return new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
121  default: return new DefaultHepRepAttValue(name, "Unknown type stored in HepRepAttDef", showLabelValue);
122  }
123 }
124 
126  return name;
127 }
128 
129 string DefaultHepRepAttValue::getLowerCaseName() {
130  string s = name;
131  transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
132  return s;
133 }
134 
135 int DefaultHepRepAttValue::getType() {
136  return type;
137 }
138 
139 string DefaultHepRepAttValue::getTypeName() {
140  switch(type) {
141  case HepRepConstants::TYPE_COLOR: return("Color");
142  case HepRepConstants::TYPE_STRING: return("String");
143  case HepRepConstants::TYPE_LONG: return("long");
144  case HepRepConstants::TYPE_INT: return("int");
145  case HepRepConstants::TYPE_DOUBLE: return("double");
146  case HepRepConstants::TYPE_BOOLEAN: return("boolean");
147  default: return "Unknown type stored in HepRepAttDef";
148  }
149 }
150 
151 int DefaultHepRepAttValue::showLabel() {
152  return showLabelValue;
153 }
154 
155 string DefaultHepRepAttValue::getString() {
156  if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
157  return stringValue;
158 }
159 
160 string DefaultHepRepAttValue::getLowerCaseString() {
161  if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
162  string s = stringValue;
163  transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
164  return s;
165 }
166 
167 int64 DefaultHepRepAttValue::getLong() {
168  if (type != HepRepConstants::TYPE_LONG) cerr << "Trying to access AttValue '" << getName() << "' as 'long'" << endl;
169  return longValue;
170 }
171 
172 int DefaultHepRepAttValue::getInteger() {
173  if (type != HepRepConstants::TYPE_INT) cerr << "Trying to access AttValue '" << getName() << "' as 'int'" << endl;
174  return (int64)longValue;
175 }
176 
177 double DefaultHepRepAttValue::getDouble() {
178  if (type != HepRepConstants::TYPE_DOUBLE) cerr << "Trying to access AttValue '" << getName() << "' as 'double'" << endl;
179  return doubleValue;
180 }
181 
182 bool DefaultHepRepAttValue::getBoolean() {
183  if (type != HepRepConstants::TYPE_BOOLEAN) cerr << "Trying to access AttValue '" << getName() << "' as 'boolean'" << endl;
184  return booleanValue;
185 }
186 
187 vector<double> DefaultHepRepAttValue::getColor() {
188  if (type != HepRepConstants::TYPE_COLOR) cerr << "Trying to access AttValue '" << getName() << "' as 'color'" << endl;
189  return colorValue;
190 }
191 
192 
193 string DefaultHepRepAttValue::getAsString() {
194  switch(type) {
195  case HepRepConstants::TYPE_COLOR: return getAsString(getColor());
196  case HepRepConstants::TYPE_STRING: return getString();
197  case HepRepConstants::TYPE_LONG: return getAsString(getLong());
198  case HepRepConstants::TYPE_INT: return getAsString(getInteger());
199  case HepRepConstants::TYPE_DOUBLE: return getAsString(getDouble());
200  case HepRepConstants::TYPE_BOOLEAN: return getAsString(getBoolean());
201  default: return "Unknown typecode";
202  }
203 }
204 
205 string DefaultHepRepAttValue::getAsString(vector<double> c) {
206  char buffer[40];
207  sprintf(buffer, "%4.2f, %4.2f, %4.2f, %4.2f",
208  c[0],
209  c[1],
210  c[2],
211  (c.size() > 3) ? c[3] : 1.0);
212  return buffer;
213 }
214 
215 string DefaultHepRepAttValue::getAsString(int i) {
216  char buffer[40];
217  sprintf(buffer, "%d", i);
218  return buffer;
219 }
220 
221 string DefaultHepRepAttValue::getAsString(int64 i) {
222  char buffer[40];
223  sprintf(buffer, CHEPREP_INT64_FORMAT, i);
224  return buffer;
225 }
226 
227 // NOTE: keep at %g rather than %lg for backward compatibility
228 string DefaultHepRepAttValue::getAsString(double d) {
229  char buffer[40];
230  sprintf(buffer, "%g", d);
231  return buffer;
232 }
233 
234 string DefaultHepRepAttValue::getAsString(bool b) {
235  return b ? "true" : "false";
236 }
237 
238 
239 
240 
241 string DefaultHepRepAttValue::toShowLabel() {
242  return toShowLabel(showLabel());
243 }
244 
245 
246 string DefaultHepRepAttValue::toShowLabel(int showLabel) {
247  string label = "";
248  bool first = true;
249  if (showLabel == HepRepConstants::SHOW_NONE) {
250  label = "NONE";
251  } else {
252  for (int i=0; i<16; i++) {
253  if (((showLabel >> i) & 0x0001) == 0x0001) {
254  if (first) {
255  first = false;
256  } else {
257  label.append(", ");
258  }
259  if (i < LABELSTRINGS_LEN) {
260  label.append(labelStrings[i]);
261  } else {
262  char hex[20];
263  sprintf(hex, "%0x", 1 << i);
264  label.append(hex);
265  }
266  }
267  }
268  }
269  return label;
270 }
271 
272 } // cheprep
G4String name
Definition: TRTMaterials.hh:40
#define buffer
Definition: xmlparse.cc:611
static const double s
Definition: G4SIunits.hh:150
void copy(std::vector< T > &main, const std::vector< T > &data)
Definition: DicomRun.hh:91
std::string getName(const ParticleType t)
Get the native INCL name of the particle.