Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 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,boo 12 if( (a_string=="1") 13 ||(a_string=="true")||(a_string=="TRUE")| 14 ||(a_string=="yes")||(a_string=="YES")||( 15 ||(a_string=="on")||(a_string=="ON")||(a_ 16 ){ 17 a_value = true; 18 return true; 19 } else if((a_string=="0") 20 ||(a_string=="false")||(a_string=="F 21 ||(a_string=="no")||(a_string=="NO") 22 ||(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,bo 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,c 42 if(a_s.empty()) {a_v = a_def;return false;} 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 51 T old = a_field; 52 //if(!tools::to(a_s,a_field)) {a_field = old 53 if(!to(a_s,a_field)) {a_field = old;a_change 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:: 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:: 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_str 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,(uint3 101 a_value = v; 102 return status; 103 } 104 } 105 106 } 107 108 #endif