00001
00002
00003 #include "cheprep/config.h"
00004
00005 #include <cstdlib>
00006 #include <cstring>
00007 #include <cctype>
00008 #include <cstdio>
00009 #include <iostream>
00010 #include <algorithm>
00011
00012 #include "HEPREP/HepRepConstants.h"
00013
00014 #include "cheprep/DefaultHepRepAttValue.h"
00015
00016 using namespace std;
00017 using namespace HEPREP;
00018
00023 namespace cheprep {
00024
00025 std::string DefaultHepRepAttValue::labelStrings[LABELSTRINGS_LEN];
00026
00027 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, string aValue, int aShowLabel)
00028 : name(aName), type(HepRepConstants::TYPE_STRING), stringValue(aValue), showLabelValue(aShowLabel) {
00029
00030 init();
00031 }
00032
00033 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int64 aValue, int aShowLabel)
00034 : name(aName), type(HepRepConstants::TYPE_LONG), longValue(aValue), showLabelValue(aShowLabel) {
00035
00036 init();
00037 }
00038
00039 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, int aValue, int aShowLabel)
00040 : name(aName), type(HepRepConstants::TYPE_INT), longValue(aValue), showLabelValue(aShowLabel) {
00041
00042 init();
00043 }
00044
00045 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, double aValue, int aShowLabel)
00046 : name(aName), type(HepRepConstants::TYPE_DOUBLE), doubleValue(aValue), showLabelValue(aShowLabel) {
00047
00048 init();
00049 }
00050
00051 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, bool aValue, int aShowLabel)
00052 : name(aName), type(HepRepConstants::TYPE_BOOLEAN), booleanValue(aValue), showLabelValue(aShowLabel) {
00053
00054 init();
00055 }
00056
00057 DefaultHepRepAttValue::DefaultHepRepAttValue(string aName, vector<double> aValue, int aShowLabel)
00058 : name(aName), type(HepRepConstants::TYPE_COLOR), colorValue(aValue), showLabelValue(aShowLabel) {
00059
00060 init();
00061 }
00062
00063 DefaultHepRepAttValue::~DefaultHepRepAttValue() {
00064 }
00065
00066 void DefaultHepRepAttValue::init() {
00067 labelStrings[0] = "NAME";
00068 labelStrings[1] = "DESC";
00069 labelStrings[2] = "VALUE";
00070 labelStrings[3] = "EXTRA";
00071 }
00072
00073 HepRepAttValue* DefaultHepRepAttValue::copy() {
00074 switch(type) {
00075 case HepRepConstants::TYPE_COLOR: new DefaultHepRepAttValue(name, colorValue, showLabelValue);
00076 case HepRepConstants::TYPE_STRING: new DefaultHepRepAttValue(name, stringValue, showLabelValue);
00077 case HepRepConstants::TYPE_LONG: new DefaultHepRepAttValue(name, longValue, showLabelValue);
00078 case HepRepConstants::TYPE_INT: new DefaultHepRepAttValue(name, (int)longValue, showLabelValue);
00079 case HepRepConstants::TYPE_DOUBLE: new DefaultHepRepAttValue(name, doubleValue, showLabelValue);
00080 case HepRepConstants::TYPE_BOOLEAN: new DefaultHepRepAttValue(name, booleanValue, showLabelValue);
00081 default: return new DefaultHepRepAttValue(name, "Unknown type stored in HepRepAttDef", showLabelValue);
00082 }
00083 }
00084
00085 string DefaultHepRepAttValue::getName() {
00086 return name;
00087 }
00088
00089 string DefaultHepRepAttValue::getLowerCaseName() {
00090 string s = name;
00091 transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
00092 return s;
00093 }
00094
00095 int DefaultHepRepAttValue::getType() {
00096 return type;
00097 }
00098
00099 string DefaultHepRepAttValue::getTypeName() {
00100 switch(type) {
00101 case HepRepConstants::TYPE_COLOR: return("Color");
00102 case HepRepConstants::TYPE_STRING: return("String");
00103 case HepRepConstants::TYPE_LONG: return("long");
00104 case HepRepConstants::TYPE_INT: return("int");
00105 case HepRepConstants::TYPE_DOUBLE: return("double");
00106 case HepRepConstants::TYPE_BOOLEAN: return("boolean");
00107 default: return "Unknown type stored in HepRepAttDef";
00108 }
00109 }
00110
00111 int DefaultHepRepAttValue::showLabel() {
00112 return showLabelValue;
00113 }
00114
00115 string DefaultHepRepAttValue::getString() {
00116 if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
00117 return stringValue;
00118 }
00119
00120 string DefaultHepRepAttValue::getLowerCaseString() {
00121 if (type != HepRepConstants::TYPE_STRING) cerr << "Trying to access AttValue '" << getName() << "' as 'string'" << endl;
00122 string s = stringValue;
00123 transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
00124 return s;
00125 }
00126
00127 int64 DefaultHepRepAttValue::getLong() {
00128 if (type != HepRepConstants::TYPE_LONG) cerr << "Trying to access AttValue '" << getName() << "' as 'long'" << endl;
00129 return longValue;
00130 }
00131
00132 int DefaultHepRepAttValue::getInteger() {
00133 if (type != HepRepConstants::TYPE_INT) cerr << "Trying to access AttValue '" << getName() << "' as 'int'" << endl;
00134 return (int64)longValue;
00135 }
00136
00137 double DefaultHepRepAttValue::getDouble() {
00138 if (type != HepRepConstants::TYPE_DOUBLE) cerr << "Trying to access AttValue '" << getName() << "' as 'double'" << endl;
00139 return doubleValue;
00140 }
00141
00142 bool DefaultHepRepAttValue::getBoolean() {
00143 if (type != HepRepConstants::TYPE_BOOLEAN) cerr << "Trying to access AttValue '" << getName() << "' as 'boolean'" << endl;
00144 return booleanValue;
00145 }
00146
00147 vector<double> DefaultHepRepAttValue::getColor() {
00148 if (type != HepRepConstants::TYPE_COLOR) cerr << "Trying to access AttValue '" << getName() << "' as 'color'" << endl;
00149 return colorValue;
00150 }
00151
00152
00153 string DefaultHepRepAttValue::getAsString() {
00154 switch(type) {
00155 case HepRepConstants::TYPE_COLOR: return getAsString(getColor());
00156 case HepRepConstants::TYPE_STRING: return getString();
00157 case HepRepConstants::TYPE_LONG: return getAsString(getLong());
00158 case HepRepConstants::TYPE_INT: return getAsString(getInteger());
00159 case HepRepConstants::TYPE_DOUBLE: return getAsString(getDouble());
00160 case HepRepConstants::TYPE_BOOLEAN: return getAsString(getBoolean());
00161 default: return "Unknown typecode";
00162 }
00163 }
00164
00165 string DefaultHepRepAttValue::getAsString(vector<double> c) {
00166 char buffer[40];
00167 sprintf(buffer, "%4.2f, %4.2f, %4.2f, %4.2f",
00168 c[0],
00169 c[1],
00170 c[2],
00171 (c.size() > 3) ? c[3] : 1.0);
00172 return buffer;
00173 }
00174
00175 string DefaultHepRepAttValue::getAsString(int i) {
00176 char buffer[40];
00177 sprintf(buffer, "%d", i);
00178 return buffer;
00179 }
00180
00181 string DefaultHepRepAttValue::getAsString(int64 i) {
00182 char buffer[40];
00183 sprintf(buffer, CHEPREP_INT64_FORMAT, i);
00184 return buffer;
00185 }
00186
00187
00188 string DefaultHepRepAttValue::getAsString(double d) {
00189 char buffer[40];
00190 sprintf(buffer, "%g", d);
00191 return buffer;
00192 }
00193
00194 string DefaultHepRepAttValue::getAsString(bool b) {
00195 return b ? "true" : "false";
00196 }
00197
00198
00199
00200
00201 string DefaultHepRepAttValue::toShowLabel() {
00202 return toShowLabel(showLabel());
00203 }
00204
00205
00206 string DefaultHepRepAttValue::toShowLabel(int showLabel) {
00207 string label = "";
00208 bool first = true;
00209 if (showLabel == HepRepConstants::SHOW_NONE) {
00210 label = "NONE";
00211 } else {
00212 for (int i=0; i<16; i++) {
00213 if (((showLabel >> i) & 0x0001) == 0x0001) {
00214 if (first) {
00215 first = false;
00216 } else {
00217 label.append(", ");
00218 }
00219 if (i < LABELSTRINGS_LEN) {
00220 label.append(labelStrings[i]);
00221 } else {
00222 char hex[20];
00223 sprintf(hex, "%0x", 1 << i);
00224 label.append(hex);
00225 }
00226 }
00227 }
00228 }
00229 return label;
00230 }
00231
00232 }