Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
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), type(HepRepConstants::TYPE_STRING), stringValue(aValue), showLabelValue(aShowLabel) {
29 
30  init();
31 }
32 
33 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int64 aValue, int aShowLabel)
34  : name(aName), type(HepRepConstants::TYPE_LONG), longValue(aValue), showLabelValue(aShowLabel) {
35 
36  init();
37 }
38 
39 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int aValue, int aShowLabel)
40  : name(aName), type(HepRepConstants::TYPE_INT), longValue(aValue), showLabelValue(aShowLabel) {
41 
42  init();
43 }
44 
45 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, double aValue, int aShowLabel)
46  : name(aName), type(HepRepConstants::TYPE_DOUBLE), doubleValue(aValue), showLabelValue(aShowLabel) {
47 
48  init();
49 }
50 
51 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, bool aValue, int aShowLabel)
52  : name(aName), type(HepRepConstants::TYPE_BOOLEAN), booleanValue(aValue), showLabelValue(aShowLabel) {
53 
54  init();
55 }
56 
57 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, vector<double> aValue, int aShowLabel)
58  : name(aName), type(HepRepConstants::TYPE_COLOR), colorValue(aValue), showLabelValue(aShowLabel) {
59 
60  init();
61 }
62 
64 }
65 
66 void DefaultHepRepAttValue::init() {
67  labelStrings[0] = "NAME";
68  labelStrings[1] = "DESC";
69  labelStrings[2] = "VALUE";
70  labelStrings[3] = "EXTRA";
71 }
72 
74  switch(type) {
75  case HepRepConstants::TYPE_COLOR: new DefaultHepRepAttValue(name, colorValue, showLabelValue);
76  case HepRepConstants::TYPE_STRING: new DefaultHepRepAttValue(name, stringValue, showLabelValue);
77  case HepRepConstants::TYPE_LONG: new DefaultHepRepAttValue(name, longValue, showLabelValue);
78  case HepRepConstants::TYPE_INT: new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
79  case HepRepConstants::TYPE_DOUBLE: new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
80  case HepRepConstants::TYPE_BOOLEAN: new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
81  default: return new DefaultHepRepAttValue(name, "Unknown type stored in HepRepAttDef", showLabelValue);
82  }
83 }
84 
86  return name;
87 }
88 
90  string s = name;
91  transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
92  return s;
93 }
94 
96  return type;
97 }
98 
100  switch(type) {
101  case HepRepConstants::TYPE_COLOR: return("Color");
102  case HepRepConstants::TYPE_STRING: return("String");
103  case HepRepConstants::TYPE_LONG: return("long");
104  case HepRepConstants::TYPE_INT: return("int");
105  case HepRepConstants::TYPE_DOUBLE: return("double");
106  case HepRepConstants::TYPE_BOOLEAN: return("boolean");
107  default: return "Unknown type stored in HepRepAttDef";
108  }
109 }
110 
112  return showLabelValue;
113 }
114 
116  if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
117  return stringValue;
118 }
119 
121  if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
122  string s = stringValue;
123  transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
124  return s;
125 }
126 
128  if (type != HepRepConstants::TYPE_LONG) cerr << "Trying to access AttValue '" << getName() << "' as 'long'" << endl;
129  return longValue;
130 }
131 
133  if (type != HepRepConstants::TYPE_INT) cerr << "Trying to access AttValue '" << getName() << "' as 'int'" << endl;
134  return (int64)longValue;
135 }
136 
138  if (type != HepRepConstants::TYPE_DOUBLE) cerr << "Trying to access AttValue '" << getName() << "' as 'double'" << endl;
139  return doubleValue;
140 }
141 
143  if (type != HepRepConstants::TYPE_BOOLEAN) cerr << "Trying to access AttValue '" << getName() << "' as 'boolean'" << endl;
144  return booleanValue;
145 }
146 
148  if (type != HepRepConstants::TYPE_COLOR) cerr << "Trying to access AttValue '" << getName() << "' as 'color'" << endl;
149  return colorValue;
150 }
151 
152 
154  switch(type) {
155  case HepRepConstants::TYPE_COLOR: return getAsString(getColor());
156  case HepRepConstants::TYPE_STRING: return getString();
157  case HepRepConstants::TYPE_LONG: return getAsString(getLong());
158  case HepRepConstants::TYPE_INT: return getAsString(getInteger());
159  case HepRepConstants::TYPE_DOUBLE: return getAsString(getDouble());
160  case HepRepConstants::TYPE_BOOLEAN: return getAsString(getBoolean());
161  default: return "Unknown typecode";
162  }
163 }
164 
165 string DefaultHepRepAttValue::getAsString(vector<double> c) {
166  char buffer[40];
167  sprintf(buffer, "%4.2f, %4.2f, %4.2f, %4.2f",
168  c[0],
169  c[1],
170  c[2],
171  (c.size() > 3) ? c[3] : 1.0);
172  return buffer;
173 }
174 
176  char buffer[40];
177  sprintf(buffer, "%d", i);
178  return buffer;
179 }
180 
182  char buffer[40];
183  sprintf(buffer, CHEPREP_INT64_FORMAT, i);
184  return buffer;
185 }
186 
187 // NOTE: keep at %g rather than %lg for backward compatibility
189  char buffer[40];
190  sprintf(buffer, "%g", d);
191  return buffer;
192 }
193 
195  return b ? "true" : "false";
196 }
197 
198 
199 
200 
202  return toShowLabel(showLabel());
203 }
204 
205 
206 string DefaultHepRepAttValue::toShowLabel(int showLabel) {
207  string label = "";
208  bool first = true;
209  if (showLabel == HepRepConstants::SHOW_NONE) {
210  label = "NONE";
211  } else {
212  for (int i=0; i<16; i++) {
213  if (((showLabel >> i) & 0x0001) == 0x0001) {
214  if (first) {
215  first = false;
216  } else {
217  label.append(", ");
218  }
219  if (i < LABELSTRINGS_LEN) {
220  label.append(labelStrings[i]);
221  } else {
222  char hex[20];
223  sprintf(hex, "%0x", 1 << i);
224  label.append(hex);
225  }
226  }
227  }
228  }
229  return label;
230 }
231 
232 } // cheprep