g4tools  5.4.0
sto
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sto
5 #define tools_sto
6 
7 #include <string>
8 
9 namespace tools {
10 
11 inline bool to(const std::string& a_string,bool& a_value,bool a_def = false){
12  if( (a_string=="1")
13  ||(a_string=="true")||(a_string=="TRUE")||(a_string=="True")
14  ||(a_string=="yes")||(a_string=="YES")||(a_string=="Yes")
15  ||(a_string=="on")||(a_string=="ON")||(a_string=="On")
16  ){
17  a_value = true;
18  return true;
19  } else if((a_string=="0")
20  ||(a_string=="false")||(a_string=="FALSE")||(a_string=="False")
21  ||(a_string=="no")||(a_string=="NO")||(a_string=="No")
22  ||(a_string=="off")||(a_string=="OFF")||(a_string=="Off")
23  ){
24  a_value = false;
25  return true;
26  } else {
27  a_value = a_def;
28  return false;
29  }
30 }
31 
32 inline bool tob(const std::string& a_string,bool& a_value,bool a_def = false) {return to(a_string,a_value,a_def);}
33 
34 }
35 
36 #include <sstream>
37 
38 namespace tools {
39 
40 template <class T>
41 inline bool to(const std::string& a_s,T& a_v,const T& a_def = T()) {
42  if(a_s.empty()) {a_v = a_def;return false;} //for TOOLS_STL istringstream.
43  std::istringstream strm(a_s.c_str());
44  strm >> a_v;
45  if(strm.fail()) {a_v = a_def;return false;}
46  return strm.eof();
47 }
48 
49 template <class T>
50 inline bool to(T& a_field,const std::string& a_s,bool& a_changed){
51  T old = a_field;
52  //if(!tools::to(a_s,a_field)) {a_field = old;a_changed=false;return false;}
53  if(!to(a_s,a_field)) {a_field = old;a_changed=false;return false;}
54  a_changed = a_field==old?false:true;
55  return true;
56 }
57 
58 }
59 
60 #include <ostream>
61 
62 namespace tools {
63 
64 template <class T>
65 inline bool to(std::ostream& a_out,const std::string& a_string,T& a_value){
66  if(!to<T>(a_string,a_value)) {
67  a_out << "Passed value \"" << a_string << "\""
68  << " is of bad type."
69  << std::endl;
70  return false;
71  }
72  return true;
73 }
74 
75 inline bool to(std::ostream& a_out,const std::string& a_string,bool& a_value){
76  if(!to(a_string,a_value)) {
77  a_out << "Passed value \"" << a_string << "\""
78  << " is not a boolean."
79  << std::endl;
80  return false;
81  }
82  return true;
83 }
84 
85 }
86 
87 #include "typedefs"
88 #include <cstddef> //size_t
89 
90 namespace tools {
91 
92 inline bool to_size_t(const std::string& a_string,size_t& a_value,const size_t& a_def = 0){
93  if(sizeof(size_t)==8) {
94  uint64 v;
95  bool status = to<uint64>(a_string,v,a_def);
96  a_value = v;
97  return status;
98  } else { //assume 4 :
99  uint32 v;
100  bool status = to<uint32>(a_string,v,(uint32)a_def);
101  a_value = v;
102  return status;
103  }
104 }
105 
106 }
107 
108 #endif
tools::to_size_t
bool to_size_t(const std::string &a_string, size_t &a_value, const size_t &a_def=0)
Definition: sto:92
tools::uint64
unsigned long long uint64
Definition: typedefs:72
typedefs
tools::tob
bool tob(const std::string &a_string, bool &a_value, bool a_def=false)
Definition: sto:32
tools::to
std::vector< std::string > to(int a_argc, char **a_argv)
Definition: args:507
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::uint32
unsigned int uint32
Definition: typedefs:71