Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sto is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_sto
0005 #define tools_sto
0006 
0007 #include <string>
0008 
0009 namespace tools {
0010 
0011 inline bool to(const std::string& a_string,bool& a_value,bool a_def = false){
0012   if(  (a_string=="1")
0013      ||(a_string=="true")||(a_string=="TRUE")||(a_string=="True")
0014      ||(a_string=="yes")||(a_string=="YES")||(a_string=="Yes")
0015      ||(a_string=="on")||(a_string=="ON")||(a_string=="On")
0016      ){
0017     a_value = true;
0018     return true;
0019   } else if((a_string=="0")
0020           ||(a_string=="false")||(a_string=="FALSE")||(a_string=="False")
0021           ||(a_string=="no")||(a_string=="NO")||(a_string=="No")
0022           ||(a_string=="off")||(a_string=="OFF")||(a_string=="Off")
0023           ){
0024     a_value = false;
0025     return true;
0026   } else {
0027     a_value = a_def;
0028     return false;
0029   }
0030 }
0031 
0032 inline bool tob(const std::string& a_string,bool& a_value,bool a_def = false) {return to(a_string,a_value,a_def);}
0033 
0034 }
0035 
0036 #include <sstream>
0037 
0038 namespace tools {
0039 
0040 template <class T>
0041 inline bool to(const std::string& a_s,T& a_v,const T& a_def = T()) {
0042   if(a_s.empty()) {a_v = a_def;return false;} //for TOOLS_STL istringstream.
0043   std::istringstream strm(a_s.c_str());
0044   strm >> a_v;
0045   if(strm.fail()) {a_v = a_def;return false;}
0046   return strm.eof();
0047 }
0048 
0049 template <class T>
0050 inline bool to(T& a_field,const std::string& a_s,bool& a_changed){
0051   T old = a_field;
0052   //if(!tools::to(a_s,a_field)) {a_field = old;a_changed=false;return false;}
0053   if(!to(a_s,a_field)) {a_field = old;a_changed=false;return false;}
0054   a_changed = a_field==old?false:true;
0055   return true;
0056 }
0057 
0058 }
0059 
0060 #include <ostream>
0061 
0062 namespace tools {
0063 
0064 template <class T>
0065 inline bool to(std::ostream& a_out,const std::string& a_string,T& a_value){
0066   if(!to<T>(a_string,a_value)) {
0067     a_out << "Passed value \"" << a_string << "\""
0068           << " is of bad type."
0069           << std::endl;
0070     return false;
0071   }
0072   return true;
0073 }
0074 
0075 inline bool to(std::ostream& a_out,const std::string& a_string,bool& a_value){
0076   if(!to(a_string,a_value)) {
0077     a_out << "Passed value \"" << a_string << "\""
0078           << " is not a boolean."
0079           << std::endl;
0080     return false;
0081   }
0082   return true;
0083 }
0084 
0085 }
0086 
0087 #include "typedefs"
0088 #include <cstddef> //size_t
0089 
0090 namespace tools {
0091 
0092 inline bool to_size_t(const std::string& a_string,size_t& a_value,const size_t& a_def = 0){
0093   if(sizeof(size_t)==8) {
0094     uint64 v;
0095     bool status = to<uint64>(a_string,v,a_def);
0096     a_value = v;
0097     return status;
0098   } else { //assume 4 :
0099     uint32 v;
0100     bool status = to<uint32>(a_string,v,(uint32)a_def);
0101     a_value = v;
0102     return status;
0103   }
0104 }
0105 
0106 }
0107 
0108 #endif