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 != nullptr; item = strtok_r(nullptr,delimeter,&saveptr), nItems++){
38  // trim leading and trailing whitespaces
39  size_t pos=0, len = strlen(item);
40  while( pos<len && isspace(item[pos]) ) pos++;
41  while( len>0 && isspace(item[--len]) );
42  string str = (pos<len+1 ? string(item+pos,len+1-pos) : item);
43 
44  colIndexToName.insert( make_pair(nItems,str) );
45  columnNameToIndex.insert( make_pair(str,nItems) );
46  if( table.insert( make_pair(str,vector<string>(rows.size())) ).second == false )
47  throw runtime_error("Duplicate column name: '" + str + "'");
48  }
49 
50  for(unsigned int r=0; r<rows.size(); r++){
51  unique_ptr<char,void(*)(void*)> copy( strdup(rows[r].c_str()), free );
52  for(unsigned int pos=0; pos<nItems; pos++){
53  char *item = strtok_r((pos==0?copy.get():nullptr),delimeter,&saveptr);
54  if( item == nullptr )
55  throw runtime_error("Too few elements in '" + rows[r] + "'");
56 
57  // trim leading and trailing whitespaces
58  size_t p=0, len = strlen(item);
59  while( p<len && isspace(item[p]) ) p++;
60  while( len>0 && isspace(item[--len]) );
61 
62  table[ colIndexToName[pos] ][r] = (pos<len+1 ? string(item+p,len+1-p) : item);
63  }
64  if( strtok_r(nullptr,delimeter,&saveptr) != nullptr )
65  throw runtime_error("Too many elements in '" + rows[r] + "', expected " + to_string(nItems));
66  }
67 
68  this->delim = delimeter;
69 }
70 
71 // following specifications take care of the basic types
72 template<> long long castTo<long long>(const char *arg) {
73  char *endptr = nullptr;
74  long long retval = strtoll(arg,&endptr,0);
75  if( *endptr == '\0' ) return retval;
76  else throw runtime_error("Cannot convert '" + string(arg)+ "' to integral type");
77 }
78 
79 // simply cast the long long down
80 template<> bool castTo<bool> (const char *arg) {
81  if( strlen(arg) > 3 ){
82  // look for "true"
83  if( strstr(arg,"true") != nullptr && strstr(arg,"false") == nullptr ) return true;
84  // look for "false"
85  if( strstr(arg,"true") == nullptr && strstr(arg,"false") != nullptr ) return false;
86  }
87  // look for "a number
88  char *endptr = nullptr;
89  long retval = strtol(arg,&endptr,0);
90  if( *endptr == '\0' ) return retval;
91  // nothing worked
92  throw runtime_error("Cannot convert '" + string(arg)+ "' to boolean");
93 }
94 
95 template<> char castTo<char> (const char *arg) { return castTo<long long>(arg); }
96 template<> short castTo<short>(const char *arg) { return castTo<long long>(arg); }
97 template<> int castTo<int> (const char *arg) { return castTo<long long>(arg); }
98 template<> long castTo<long> (const char *arg) { return castTo<long long>(arg); }
99 
100 template<> long double castTo<long double>(const char *arg) {
101  char *endptr = nullptr;
102  long double retval = strtold(arg,&endptr);
103  if( *endptr == '\0' ) return retval;
104  else throw runtime_error("Cannot convert '" + string(arg) + "' to floating point type");
105 }
106 template<> float castTo<float> (const char *arg) { return castTo<long double>(arg); }
107 template<> double castTo<double>(const char *arg) { return castTo<long double>(arg); }
108 
109 template<> unsigned long long castTo<unsigned long long>(const char *arg) {
110  char *endptr = nullptr;
111  unsigned long long retval = strtoull(arg,&endptr,0);
112  if( *endptr == '\0' ) return retval;
113  else throw runtime_error("Cannot convert '" + string(arg)+ "' to unsigned integral type");
114 }
115 template<> unsigned char castTo<unsigned char> (const char *arg) { return castTo<unsigned long long>(arg); }
116 template<> unsigned short castTo<unsigned short>(const char *arg) { return castTo<unsigned long long>(arg); }
117 template<> unsigned int castTo<unsigned int> (const char *arg) { return castTo<unsigned long long>(arg); }
118 template<> unsigned long castTo<unsigned long> (const char *arg) { return castTo<unsigned long long>(arg); }
119 
120 } // end of l1t namespace
121 
type
Definition: HCALResponse.h:21
char castTo< char >(const char *arg)
Definition: Parameter.cc:95
long double castTo< long double >(const char *arg)
Definition: Parameter.cc:100
unsigned int castTo< unsigned int >(const char *arg)
Definition: Parameter.cc:117
def copy(args, dbName)
delete x;
Definition: CaloConfig.h:22
#define nullptr
A arg
Definition: Factorize.h:37
unsigned long long castTo< unsigned long long >(const char *arg)
Definition: Parameter.cc:109
U second(std::pair< T, U > const &p)
unsigned long castTo< unsigned long >(const char *arg)
Definition: Parameter.cc:118
Definition: value.py:1
float castTo< float >(const char *arg)
Definition: Parameter.cc:106
bool castTo< bool >(const char *arg)
Definition: Parameter.cc:80
long long castTo< long long >(const char *arg)
Definition: Parameter.cc:72
unsigned short castTo< unsigned short >(const char *arg)
Definition: Parameter.cc:116
double castTo< double >(const char *arg)
Definition: Parameter.cc:107
short castTo< short >(const char *arg)
Definition: Parameter.cc:96
long castTo< long >(const char *arg)
Definition: Parameter.cc:98
unsigned char castTo< unsigned char >(const char *arg)
Definition: Parameter.cc:115
int castTo< int >(const char *arg)
Definition: Parameter.cc:97