Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_num2s 5 #define tools_num2s 6 7 // write numerics in a string as if done with 8 9 #include "sprintf" 10 #include "typedefs" 11 12 #include <cstddef> //ptrdiff_t 13 14 namespace tools { 15 16 /* 17 inline bool num2s(unsigned char a_value,std::s 18 return print2s(a_s,32,"%c",a_value); 19 } 20 21 inline bool num2s(char a_value,std::string& a_ 22 return print2s(a_s,32,"%c",a_value); 23 } 24 */ 25 26 inline bool num2s(unsigned short a_value,std:: 27 return print2s(a_s,32,"%u",a_value); 28 } 29 30 inline bool num2s(short a_value,std::string& a 31 return print2s(a_s,32,"%d",a_value); 32 } 33 34 inline bool num2s(unsigned int a_value,std::st 35 return print2s(a_s,32,"%u",a_value); 36 } 37 38 /* 39 inline bool num2sx(unsigned int a_value,std::s 40 return print2s(a_s,32,"%x",a_value); 41 } 42 */ 43 44 inline bool num2s(int a_value,std::string& a_s 45 return print2s(a_s,32,"%d",a_value); 46 } 47 48 inline bool num2s(uint64 a_value,std::string& 49 return print2s(a_s,32,uint64_format(),a_valu 50 } 51 52 inline bool num2s(int64 a_value,std::string& a 53 return print2s(a_s,32,int64_format(),a_value 54 } 55 56 inline bool num2s(float a_value,std::string& a 57 return print2s(a_s,32,"%g",a_value); 58 } 59 60 inline bool num2s(double a_value,std::string& 61 return print2s(a_s,32,"%g",a_value); 62 } 63 64 inline bool size_t2s(size_t a_value,std::strin 65 if(sizeof(size_t)==8) { 66 return num2s((uint64)a_value,a_s); 67 } else { //assume 4 : 68 return num2s((uint32)a_value,a_s); 69 } 70 } 71 72 inline bool ptrdiff_t2s(ptrdiff_t a_value,std: 73 if(sizeof(ptrdiff_t)==8) { 74 return num2s((int64)a_value,a_s); 75 } else { //assume 4 : 76 return num2s((int32)a_value,a_s); 77 } 78 } 79 80 /* 81 inline bool num2s(bool a_value,std::string& a_ 82 //a_s = a_value?"true":"false"; 83 a_s = a_value?"1":"0"; 84 return true; 85 } 86 */ 87 88 template <class T> 89 inline bool numas(const T& a_value,std::string 90 std::string stmp; 91 if(!num2s(a_value,stmp)) return false; 92 a_s += stmp; 93 return true; 94 } 95 96 template <class T> 97 inline bool size_tas(const T& a_value,std::str 98 std::string stmp; 99 if(!size_t2s(a_value,stmp)) return false; 100 a_s += stmp; 101 return true; 102 } 103 104 /* 105 inline bool num2s(unsigned int a_linen,const c 106 a_s.clear(); 107 for(unsigned int index=0;index<a_linen;index 108 a_s += a_lines[index]; 109 a_s += "\n"; 110 } 111 return true; 112 } 113 */ 114 115 // for the below std::vector num2s in case T=s 116 inline bool num2s(const std::string& a_value,s 117 118 template <class T> 119 class num_out : public std::string { 120 typedef std::string parent; 121 private: 122 //typedef typename std::enable_if<std::is_fl 123 //typedef typename std::enable_if<std::is_in 124 //typedef typename std::enable_if<std::is_ar 125 public: 126 num_out(const T& a_value) { 127 parent::operator+=("\""); 128 if(!numas(a_value,*this)) {} //throw 129 parent::operator+=("\""); 130 } 131 public: 132 num_out(const num_out& a_from):parent(a_from 133 num_out& operator=(const num_out& a_from){pa 134 }; 135 136 } 137 138 #include <vector> 139 140 namespace tools { 141 142 template <class VEC> 143 inline bool nums2s(const VEC& a_vals,std::stri 144 a_s.clear(); 145 typename VEC::size_type number = a_vals.size 146 if(number<=0) return true; //it is ok. 147 number--; 148 std::string stmp; 149 bool status = true; 150 for(typename VEC::size_type index=0;index<nu 151 if(!num2s(a_vals[index],stmp)) status = fa 152 a_s += stmp; 153 a_s += a_sep; 154 } 155 if(!num2s(a_vals[number],stmp)) status = fal 156 a_s += stmp; 157 if(a_sep_at_end) a_s += a_sep; 158 return status; 159 } 160 161 template <class T> 162 inline bool nums2s(const std::vector<T>& a_val 163 return nums2s< std::vector<T> >(a_vals,a_s,a 164 } 165 166 } 167 168 #endif