1 #ifndef MeasAlgoShapeletConfigFile_H
2 #define MeasAlgoShapeletConfigFile_H
20 namespace algorithms {
27 enum { value =
false };
31 struct is_bool<bool> {
32 enum { value =
true };
36 #ifdef __INTEL_COMPILER
37 #pragma warning (disable : 444)
73 std::string::operator=(rhs);
80 std::stringstream oss;
89 std::stringstream oss;
90 const int n = x.size();
91 if (n > 0) oss << x[0];
92 for(
int i=1;i<n;++i) oss <<
' ' << x[i];
101 if (is_bool<T>::value) {
106 #ifdef Use_Zero_Default
107 if (*
this ==
"")
return T();
110 std::string err=
"Could not convert ConvertibleString to input type ";
111 err +=
typeid(T).
name();
112 err += std::string(
": this = ") + *
this;
114 std::stringstream ss(*
this);
119 if (std::numeric_limits<T>::is_integer) {
120 if ((*
this)[0] ==
'0') {
121 if ((*
this)[1] ==
'x' || (*
this)[1] ==
'X') {
131 std::cerr<<err<<std::endl;
140 template <
typename T>
141 operator std::vector<T>()
const
143 #ifdef Use_Zero_Default
144 if (*
this ==
"")
return std::vector<T>();
147 std::string err=
"Could not convert ConvertibleString to input type ";
148 err += std::string(
"std::vector<")+
typeid(T).
name()+
">";
149 err += std::string(
": this = ") + *
this;
152 if ((*
this)[0] ==
'{') {
155 int i1 = this->find_first_not_of(
" \t\n\r\f",1);
156 if (i1 ==
int(std::string::npos)) {
158 std::cerr<<err<<std::endl;
160 return std::vector<T>();
165 if ((*
this)[i1] ==
'}') {
167 return std::vector<T>();
170 int nComma = std::count(this->begin(),this->end(),
',');
171 std::vector<T> ret(nComma+1);
173 int i2 = this->find_first_of(
"},",i1);
176 while ((*
this)[i2] !=
'}') {
177 std::string s = this->substr(i1,i2-i1);
178 std::stringstream ss(s);
181 i2 = this->find_first_of(
"},",i1);
185 std::string s = this->substr(i1,i2-i1);
186 std::stringstream ss(s);
195 std::stringstream ss(*
this);
198 while (ss >> x) ret.push_back(x);
202 #ifdef __INTEL_COMPILER
203 #pragma warning (default : 444)
209 #ifdef Use_Zero_Default
210 if (*
this ==
"")
return false;
214 std::string sup = *
this;
215 for ( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
218 if ( sup==
"FALSE" || sup==
"F" || sup==
"NO" || sup==
"N" ||
219 sup==
"0" || sup==
"NONE" ) {
221 }
else if ( sup==
"TRUE" || sup==
"T" || sup==
"YES" || sup==
"Y" ||
226 "Could not convert ConvertibleString to input type bool"
229 std::cerr<<err<<std::endl;
248 const std::string delimiter =
"=",
249 const std::string comment =
"#",
250 const std::string include =
"+",
251 const std::string sentry =
"EndConfigFile" );
254 void load(
const std::string fileName )
255 { std::ifstream fs(fileName.c_str());
read(fs); }
260 void load(
const std::string fileName,
261 const std::string delimiter,
262 const std::string comment =
"",
263 const std::string include =
"",
264 const std::string sentry =
"" );
267 void read(std::istream& is);
269 { std::istringstream ss(s);
read(ss); }
272 void write(std::ostream& os)
const;
284 template <
typename T>
inline T
read(
const std::string& key )
const;
285 template <
typename T>
inline T
read(
286 const std::string& key,
const T& value )
const;
287 template <
typename T>
inline bool readInto(
288 T& var,
const std::string& key )
const;
289 template <
typename T>
inline bool readInto(
290 T& var,
const std::string& key,
const T& value )
const;
296 std::string
getstr(
const std::string key)
const throw (
const char*);
299 const std::string key,
300 const std::string defval);
303 template <
typename T>
inline void add( std::string key,
const T& value );
304 void remove(
const std::string& key );
307 bool keyExists(
const std::string& key )
const;
328 static void trim( std::string& s );
337 typedef std::map<std::string,ConvertibleString>::iterator
MapIt;
338 typedef std::map<std::string,ConvertibleString>::const_iterator
MapCIt;
342 { cf.
write(os);
return os; }
344 { cf.
read(is);
return is; }
346 template <
typename T>
350 std::string key2 = key;
355 std::cerr<<
"Key not found: "<<key2<<std::endl;
359 "ConfigFile error: key "+key2+
" not found");
366 xdbg<<
"Caught ParameterException: \n"<<e.what()<<std::endl;
368 "ConfigFile error: Could not convert entry for key " +
370 " to given type.\nCaught error from ConvertibleString: \n" +
377 template <
typename T>
382 std::string key2 = key;
392 xdbg<<
"Caught ParameterException: \n"<<e.what()<<std::endl;
394 "ConfigFile error: Could not convert entry for key " +
396 " to given type.\nCaught error from ConvertibleString: \n" +
404 template <
typename T>
410 std::string key2 = key;
418 xdbg<<
"Caught ParameterException: \n"<<e.what()<<std::endl;
420 "ConfigFile error: Could not convert entry for key " +
422 " to given type.\nCaught error from ConvertibleString: \n" +
430 template <
typename T>
432 T& var,
const std::string& key,
const T& value )
const
437 std::string key2 = key;
445 xdbg<<
"Caught ParameterException: \n"<<e.what()<<std::endl;
447 "ConfigFile error: Could not convert entry for key " +
449 " to given type.\nCaught error from ConvertibleString: \n" +
458 template <
typename T>
468 #endif // CONFIGFILE_H
bool operator_bool() const
std::string getDelimiter() const
std::string getComment() const
ConvertibleString(const std::string s)
table::Key< std::string > name
ConvertibleString operator[](const std::string &key) const
ConvertibleString(const T &x)
static void trim(std::string &s)
std::ostream & operator<<(std::ostream &os, const Position &pos)
void append(const std::string &s)
std::string setInclude(const std::string &s)
std::string getstr(const std::string key) const
std::string setComment(const std::string &s)
ConvertibleString & operator=(const T &x)
ConvertibleString(const std::vector< T > &x)
void load(const std::string fileName)
void write(std::ostream &os) const
bool keyExists(const std::string &key) const
std::map< std::string, ConvertibleString >::iterator MapIt
std::string getSentry() const
ConvertibleString & operator=(const std::string &rhs)
bool readInto(T &var, const std::string &key) const
ConvertibleString & getNoCheck(const std::string &key)
std::istream & operator>>(std::istream &os, Position &pos)
ConvertibleString & operator[](const std::string &key)
void writeAsComment(std::ostream &os) const
std::map< std::string, ConvertibleString > _contents
std::map< std::string, ConvertibleString >::const_iterator MapCIt
ConvertibleString & operator=(const std::vector< T > &x)
void read(std::istream &is)
void add(std::string key, const T &value)
std::string setSentry(const std::string &s)
std::string setDelimiter(const std::string &s)
std::string getInclude() const