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