[834] | 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 | |
---|
| 19 | /** |
---|
| 20 | * @author Mark Donszelmann |
---|
| 21 | * @version $Id: DefaultHepRepAttValue.cc,v 1.10 2005/06/02 21:28:45 duns Exp $ |
---|
| 22 | */ |
---|
| 23 | namespace cheprep { |
---|
| 24 | |
---|
| 25 | std::string DefaultHepRepAttValue::labelStrings[LABELSTRINGS_LEN]; |
---|
| 26 | |
---|
| 27 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, string value, int showLabel) |
---|
| 28 | : name(name), type(HepRepConstants::TYPE_STRING), stringValue(value), showLabelValue(showLabel) { |
---|
| 29 | |
---|
| 30 | init(); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, int64 value, int showLabel) |
---|
| 34 | : name(name), type(HepRepConstants::TYPE_LONG), longValue(value), showLabelValue(showLabel) { |
---|
| 35 | |
---|
| 36 | init(); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, int value, int showLabel) |
---|
| 40 | : name(name), type(HepRepConstants::TYPE_INT), longValue(value), showLabelValue(showLabel) { |
---|
| 41 | |
---|
| 42 | init(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, double value, int showLabel) |
---|
| 46 | : name(name), type(HepRepConstants::TYPE_DOUBLE), doubleValue(value), showLabelValue(showLabel) { |
---|
| 47 | |
---|
| 48 | init(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, bool value, int showLabel) |
---|
| 52 | : name(name), type(HepRepConstants::TYPE_BOOLEAN), booleanValue(value), showLabelValue(showLabel) { |
---|
| 53 | |
---|
| 54 | init(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | DefaultHepRepAttValue::DefaultHepRepAttValue(string name, vector<double> value, int showLabel) |
---|
| 58 | : name(name), type(HepRepConstants::TYPE_COLOR), colorValue(value), showLabelValue(showLabel) { |
---|
| 59 | |
---|
| 60 | init(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | DefaultHepRepAttValue::~DefaultHepRepAttValue() { |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void DefaultHepRepAttValue::init() { |
---|
| 67 | labelStrings[0] = "NAME"; |
---|
| 68 | labelStrings[1] = "DESC"; |
---|
| 69 | labelStrings[2] = "VALUE"; |
---|
| 70 | labelStrings[3] = "EXTRA"; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | HepRepAttValue* DefaultHepRepAttValue::copy() { |
---|
| 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 | |
---|
| 85 | string DefaultHepRepAttValue::getName() { |
---|
| 86 | return name; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | string DefaultHepRepAttValue::getLowerCaseName() { |
---|
| 90 | string s = name; |
---|
| 91 | transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower); |
---|
| 92 | return s; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | int DefaultHepRepAttValue::getType() { |
---|
| 96 | return type; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | string DefaultHepRepAttValue::getTypeName() { |
---|
| 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 | |
---|
| 111 | int DefaultHepRepAttValue::showLabel() { |
---|
| 112 | return showLabelValue; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | string DefaultHepRepAttValue::getString() { |
---|
| 116 | if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl; |
---|
| 117 | return stringValue; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | string DefaultHepRepAttValue::getLowerCaseString() { |
---|
| 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 | |
---|
| 127 | int64 DefaultHepRepAttValue::getLong() { |
---|
| 128 | if (type != HepRepConstants::TYPE_LONG) cerr << "Trying to access AttValue '" << getName() << "' as 'long'" << endl; |
---|
| 129 | return longValue; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int DefaultHepRepAttValue::getInteger() { |
---|
| 133 | if (type != HepRepConstants::TYPE_INT) cerr << "Trying to access AttValue '" << getName() << "' as 'int'" << endl; |
---|
| 134 | return (int64)longValue; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | double DefaultHepRepAttValue::getDouble() { |
---|
| 138 | if (type != HepRepConstants::TYPE_DOUBLE) cerr << "Trying to access AttValue '" << getName() << "' as 'double'" << endl; |
---|
| 139 | return doubleValue; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | bool DefaultHepRepAttValue::getBoolean() { |
---|
| 143 | if (type != HepRepConstants::TYPE_BOOLEAN) cerr << "Trying to access AttValue '" << getName() << "' as 'boolean'" << endl; |
---|
| 144 | return booleanValue; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | vector<double> DefaultHepRepAttValue::getColor() { |
---|
| 148 | if (type != HepRepConstants::TYPE_COLOR) cerr << "Trying to access AttValue '" << getName() << "' as 'color'" << endl; |
---|
| 149 | return colorValue; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | string DefaultHepRepAttValue::getAsString() { |
---|
| 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 | |
---|
| 175 | string DefaultHepRepAttValue::getAsString(int i) { |
---|
| 176 | char buffer[40]; |
---|
| 177 | sprintf(buffer, "%d", i); |
---|
| 178 | return buffer; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | string DefaultHepRepAttValue::getAsString(int64 i) { |
---|
| 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 |
---|
| 188 | string DefaultHepRepAttValue::getAsString(double d) { |
---|
| 189 | char buffer[40]; |
---|
| 190 | sprintf(buffer, "%g", d); |
---|
| 191 | return buffer; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | string DefaultHepRepAttValue::getAsString(bool b) { |
---|
| 195 | return b ? "true" : "false"; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | string DefaultHepRepAttValue::toShowLabel() { |
---|
| 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 |
---|