Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_tos 5 #define tools_tos 6 7 // Used in BatchLab/MemoryTuple and Rio_Tuple. 8 // We need something different than the to<T> 9 // to handle std::vector<> and bool. 10 11 #include "sprintf" 12 #include "charmanip" 13 #include "typedefs" 14 15 #include <vector> 16 17 namespace tools { 18 19 inline std::string tos(unsigned char a_value){ 20 std::string _s; 21 if(is_printable(a_value)) 22 sprintf(_s,32,"%c",a_value); 23 else 24 sprintf(_s,32,"%d",a_value); 25 return _s; 26 } 27 28 inline std::string tos(char a_value){ 29 std::string _s; 30 if(is_printable(a_value)) 31 sprintf(_s,32,"%c",a_value); 32 else 33 sprintf(_s,32,"%d",a_value); 34 return _s; 35 } 36 37 inline std::string tos(unsigned short a_value) 38 std::string _s; 39 sprintf(_s,32,"%d",a_value); 40 return _s; 41 } 42 43 inline std::string tos(short a_value){ 44 std::string _s; 45 sprintf(_s,32,"%d",a_value); 46 return _s; 47 } 48 49 inline std::string tos(unsigned int a_value) { 50 std::string _s; 51 sprintf(_s,32,"%u",a_value); 52 return _s; 53 } 54 55 inline std::string tosx(unsigned int a_value) 56 std::string _s; 57 sprintf(_s,32,"%x",a_value); 58 return _s; 59 } 60 61 inline std::string tos(int a_value){ 62 std::string _s; 63 sprintf(_s,32,"%d",a_value); 64 return _s; 65 } 66 67 inline std::string tos(uint64 a_value) { 68 std::string _s; 69 sprintf(_s,32,uint64_format(),a_value); 70 return _s; 71 } 72 73 inline std::string tos(int64 a_value){ 74 std::string _s; 75 sprintf(_s,32,int64_format(),a_value); 76 return _s; 77 } 78 79 inline std::string tos(float a_value){ 80 std::string _s; 81 sprintf(_s,32,"%g",a_value); 82 return _s; 83 } 84 85 inline std::string tos(double a_value){ 86 std::string _s; 87 sprintf(_s,32,"%g",a_value); 88 return _s; 89 } 90 91 inline std::string tos(bool a_value){ 92 return std::string(a_value?"true":"false"); 93 } 94 95 inline std::string tos(const std::string& a_va 96 97 template <class T> 98 inline std::string tos(const std::vector<T>& a 99 const std::string& a_se 100 bool a_sep_at_end = fal 101 size_t number = a_vals.size(); 102 if(number<=0) return ""; 103 std::string result; 104 number--; 105 for(size_t index=0;index<number;index++) { 106 result += tos(a_vals[index]); 107 result += a_sep; 108 } 109 result += tos(a_vals[number]); 110 if(a_sep_at_end) result += a_sep; 111 return result; 112 } 113 114 inline std::string tos(unsigned int a_linen,co 115 std::string _s; 116 for(unsigned int index=0;index<a_linen;index 117 _s += std::string(a_lines[index]); 118 _s += "\n"; 119 } 120 return _s; 121 } 122 123 } 124 125 #endif