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