CMS 3D CMS Logo

Parameter.cc
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <memory>
4 
5 using namespace std;
6 
7 namespace l1t{
8 
9 Parameter::Parameter(const char *id,
10  const char *procOrRole,
11  const char *type,
12  const char *value,
13  const char *delimeter
14  ){
15  this->id = id;
16  this->procOrRole = procOrRole;
17  this->type = type;
18  this->scalarOrVector = value;
19  this->delim = delimeter;
20 }
21 
22 Parameter::Parameter(const char *id,
23  const char *procOrRole,
24  const char *types,
25  const char *columns,
26  const vector<string>& rows,
27  const char *delimeter
28  ){
29  this->id = id;
30  this->procOrRole = procOrRole;
31  this->type = types;
32 
33  map<int,string> colIndexToName;
34  unique_ptr<char,void(*)(void*)> copy( strdup(columns), free );
35  unsigned long nItems = 0;
36  char *saveptr;
37  for(const char *item=strtok_r(copy.get(),delimeter,&saveptr); item != NULL; item = strtok_r(NULL,delimeter,&saveptr), nItems++){
38  colIndexToName.insert( make_pair(nItems,string(item)) );
39  columnNameToIndex.insert( make_pair(string(item),nItems) );
40  if( table.insert( make_pair(string(item),vector<string>(rows.size())) ).second == false )
41  throw runtime_error("Duplicate column name: '" + string(item) + "'");
42  }
43 
44  for(unsigned int r=0; r<rows.size(); r++){
45  unique_ptr<char,void(*)(void*)> copy( strdup(rows[r].c_str()), free );
46  for(unsigned int pos=0; pos<nItems; pos++){
47  char *item = strtok_r((pos==0?copy.get():NULL),delimeter,&saveptr);
48  if( item == NULL )
49  throw runtime_error("Too few elements in '" + rows[r] + "'");
50 
51  table[ colIndexToName[pos] ][r] = item;
52  }
53  if( strtok_r(NULL,delimeter,&saveptr) != NULL )
54  throw runtime_error("Too many elements in '" + rows[r] + "', expected " + to_string(nItems));
55  }
56 
57  this->delim = delimeter;
58 }
59 
60 // following specifications take care of the basic types
61 template<> long long castTo<long long>(const char *arg) {
62  char *endptr = NULL;
63  long long retval = strtoll(arg,&endptr,0);
64  if( *endptr == '\0' ) return retval;
65  else throw runtime_error("Cannot convert '" + string(arg)+ "' to integral type");
66 }
67 
68 // simply cast the long long down
69 template<> bool castTo<bool> (const char *arg) {
70  if( strlen(arg) > 3 ){
71  // look for "true"
72  if( strstr(arg,"true") != NULL && strstr(arg,"false") == NULL ) return true;
73  // look for "false"
74  if( strstr(arg,"true") == NULL && strstr(arg,"false") != NULL ) return false;
75  }
76  // look for "a number
77  char *endptr = NULL;
78  long retval = strtol(arg,&endptr,0);
79  if( *endptr == '\0' ) return retval;
80  // nothing worked
81  throw runtime_error("Cannot convert '" + string(arg)+ "' to boolean");
82 }
83 
84 template<> char castTo<char> (const char *arg) { return castTo<long long>(arg); }
85 template<> short castTo<short>(const char *arg) { return castTo<long long>(arg); }
86 template<> int castTo<int> (const char *arg) { return castTo<long long>(arg); }
87 template<> long castTo<long> (const char *arg) { return castTo<long long>(arg); }
88 
89 template<> long double castTo<long double>(const char *arg) {
90  char *endptr = NULL;
91  long double retval = strtold(arg,&endptr);
92  if( *endptr == '\0' ) return retval;
93  else throw runtime_error("Cannot convert '" + string(arg) + "' to floating point type");
94 }
95 template<> float castTo<float> (const char *arg) { return castTo<long double>(arg); }
96 template<> double castTo<double>(const char *arg) { return castTo<long double>(arg); }
97 
98 template<> unsigned long long castTo<unsigned long long>(const char *arg) {
99  char *endptr = NULL;
100  unsigned long long retval = strtoull(arg,&endptr,0);
101  if( *endptr == '\0' ) return retval;
102  else throw runtime_error("Cannot convert '" + string(arg)+ "' to unsigned integral type");
103 }
104 template<> unsigned char castTo<unsigned char> (const char *arg) { return castTo<unsigned long long>(arg); }
105 template<> unsigned short castTo<unsigned short>(const char *arg) { return castTo<unsigned long long>(arg); }
106 template<> unsigned int castTo<unsigned int> (const char *arg) { return castTo<unsigned long long>(arg); }
107 template<> unsigned long castTo<unsigned long> (const char *arg) { return castTo<unsigned long long>(arg); }
108 
109 } // end of l1t namespace
110 
type
Definition: HCALResponse.h:21
char castTo< char >(const char *arg)
Definition: Parameter.cc:84
long double castTo< long double >(const char *arg)
Definition: Parameter.cc:89
unsigned int castTo< unsigned int >(const char *arg)
Definition: Parameter.cc:106
#define NULL
Definition: scimark2.h:8
delete x;
Definition: CaloConfig.h:22
A arg
Definition: Factorize.h:36
unsigned long long castTo< unsigned long long >(const char *arg)
Definition: Parameter.cc:98
U second(std::pair< T, U > const &p)
unsigned long castTo< unsigned long >(const char *arg)
Definition: Parameter.cc:107
Definition: value.py:1
float castTo< float >(const char *arg)
Definition: Parameter.cc:95
bool castTo< bool >(const char *arg)
Definition: Parameter.cc:69
long long castTo< long long >(const char *arg)
Definition: Parameter.cc:61
unsigned short castTo< unsigned short >(const char *arg)
Definition: Parameter.cc:105
double castTo< double >(const char *arg)
Definition: Parameter.cc:96
short castTo< short >(const char *arg)
Definition: Parameter.cc:85
long castTo< long >(const char *arg)
Definition: Parameter.cc:87
unsigned char castTo< unsigned char >(const char *arg)
Definition: Parameter.cc:104
int castTo< int >(const char *arg)
Definition: Parameter.cc:86