Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringParameters.cc
Go to the documentation of this file.
2 
3 #include <sstream>
4 #include <algorithm>
5 #include <exception>
6 #include <iomanip>
7 
8 namespace marlin{
9 
11 
12 }
13 
15  _map(sp._map){
16  }
17 
19 
20 }
21 
22 
23 void StringParameters::add( const std::string& key , const std::vector<std::string> & values ) {
24 
25  for(unsigned int i=0 ; i< values.size() ; i++){
26 
27  _map[ key ].push_back( values[i] ) ;
28  }
29 }
30 
32 
33  for(unsigned int i= 1 ; i< tokens.size() ; i++){
34 
35  _map[ tokens[0] ].push_back( tokens[i] ) ;
36  }
37 }
38 
40 
41  _map.erase( key );
42 }
43 
45 
46  return _map[ key ].size() > 0 ;
47 }
48 
50 
51  if( _map[ key ].size() == 0 ) return 0 ;
52 
53  return intVal( _map[ key ][0] ) ;
54 }
55 
57 
58  if( _map[ key ].size() == 0 ) return 0 ;
59 
60  return floatVal( _map[ key ][0] ) ;
61 }
62 
63 
65 
66  static std::string empty("") ;
67  if( _map[ key ].size() == 0 ) return empty ;
68  return _map[ key ][0] ;
69 }
70 
71 
72 
74 
75  transform( _map[ key ].begin() , _map[ key ].end() , back_inserter(intVec) , intVal ) ;
76 
77  return intVec ;
78 }
79 
80 
82 
83  transform( _map[ key ].begin() , _map[ key ].end() , back_inserter(floatVec) , floatVal ) ;
84 
85  return floatVec ;
86 }
87 
88 
90 
91  copy( _map[ key ].begin() , _map[ key ].end() , back_inserter(stringVec) ) ;
92 
93  return stringVec ;
94 }
95 
97 
98  for(ParametersMap::iterator m = _map.begin() ; m != _map.end() ; m++){
99  stringVec.push_back( m->first );
100  }
101 
102  return stringVec ;
103 }
104 
105 // FIXME where should this utility function live ?
106 template <class T>
107 bool convert(std::string input, T &value) {
108 
109  std::istringstream stream(input);
110 
111  return ( ! (stream >> std::setbase(0) >> value).fail() ) && stream.eof();
112 
113 }
114 
115 
116 int intVal( const std::string& str ){
117 
118  static int i;
119 
120  if( convert( str , i ) ){
121  //std::cout << "converted : " << f << std::endl ;
122  return i;
123  }
124  else{
125 
126  std::stringstream sstr;
127 
128  sstr << "failed converting int from string: " << str << std::endl ;
129 
130  throw Exception( sstr.str() );
131  }
132 
133 
134 }
135 
136 float floatVal( const std::string& str ){
137 
138  // atof would not give an error trying to convert from string: "1.003.4"
139  //return atof( str.c_str() ) ;
140 
141  static float f;
142 
143  if( convert( str , f ) ){
144  //std::cout << "converted : " << f << std::endl ;
145  return f;
146  }
147  else{
148 
149  std::stringstream sstr;
150 
151  sstr << "failed converting float from string: " << str << std::endl ;
152 
153  throw Exception( sstr.str() );
154  }
155 }
156 
158 
159  for(ParametersMap::iterator m = p._map.begin() ; m != p._map.end() ; m++){
160 
161  s << " " << m->first << ": " ;
162 
163  for(unsigned int i=0; i < m->second.size(); i++ ){
164 
165  s << "[" << m->second[i] << "] " ;
166 
167  }
168  s << std::endl ;
169 
170  }
171  return s ;
172 }
173 
174 } //namsepace marlin
bool isParameterSet(const std::string &key)
T endl(T...args)
StringVec & getStringVals(const std::string &key, StringVec &stringVec)
T end(T...args)
void add(const std::string &key, const std::vector< std::string > &values)
bool convert(std::string input, T &value)
STL class.
T push_back(T...args)
StringVec & getStringKeys(StringVec &stringVec)
T erase(T...args)
const std::string & getStringVal(const std::string &key)
int getIntVal(const std::string &key)
T str(T...args)
T size(T...args)
int intVal(const std::string &str)
T begin(T...args)
float getFloatVal(const std::string &key)
void erase(const std::string &key)
float floatVal(const std::string &str)
T setbase(T...args)
STL class.
IntVec & getIntVals(const std::string &key, IntVec &intVec)
Simple parameters class for Marlin.
FloatVec & getFloatVals(const std::string &key, FloatVec &floatVec)
std::ostream & operator<<(std::ostream &s, Expression &e)