Warning, /include/Geant4/tools/num2s 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_num2s
0005 #define tools_num2s
0006
0007 // write numerics in a string as if done with a std::ostringstream::operator<<(<num>).
0008
0009 #include "sprintf"
0010 #include "typedefs"
0011
0012 #include <cstddef> //ptrdiff_t
0013
0014 namespace tools {
0015
0016 /*
0017 inline bool num2s(unsigned char a_value,std::string& a_s){
0018 return print2s(a_s,32,"%c",a_value);
0019 }
0020
0021 inline bool num2s(char a_value,std::string& a_s){
0022 return print2s(a_s,32,"%c",a_value);
0023 }
0024 */
0025
0026 inline bool num2s(unsigned short a_value,std::string& a_s) { //used in value
0027 return print2s(a_s,32,"%u",a_value);
0028 }
0029
0030 inline bool num2s(short a_value,std::string& a_s){ //used in value
0031 return print2s(a_s,32,"%d",a_value);
0032 }
0033
0034 inline bool num2s(unsigned int a_value,std::string& a_s) {
0035 return print2s(a_s,32,"%u",a_value);
0036 }
0037
0038 /*
0039 inline bool num2sx(unsigned int a_value,std::string& a_s) {
0040 return print2s(a_s,32,"%x",a_value);
0041 }
0042 */
0043
0044 inline bool num2s(int a_value,std::string& a_s){
0045 return print2s(a_s,32,"%d",a_value);
0046 }
0047
0048 inline bool num2s(uint64 a_value,std::string& a_s) {
0049 return print2s(a_s,32,uint64_format(),a_value);
0050 }
0051
0052 inline bool num2s(int64 a_value,std::string& a_s){
0053 return print2s(a_s,32,int64_format(),a_value);
0054 }
0055
0056 inline bool num2s(float a_value,std::string& a_s){
0057 return print2s(a_s,32,"%g",a_value);
0058 }
0059
0060 inline bool num2s(double a_value,std::string& a_s){
0061 return print2s(a_s,32,"%g",a_value);
0062 }
0063
0064 inline bool size_t2s(size_t a_value,std::string& a_s) {
0065 if(sizeof(size_t)==8) {
0066 return num2s((uint64)a_value,a_s);
0067 } else { //assume 4 :
0068 return num2s((uint32)a_value,a_s);
0069 }
0070 }
0071
0072 inline bool ptrdiff_t2s(ptrdiff_t a_value,std::string& a_s) { //used in write_bsg.
0073 if(sizeof(ptrdiff_t)==8) {
0074 return num2s((int64)a_value,a_s);
0075 } else { //assume 4 :
0076 return num2s((int32)a_value,a_s);
0077 }
0078 }
0079
0080 /*
0081 inline bool num2s(bool a_value,std::string& a_s){
0082 //a_s = a_value?"true":"false";
0083 a_s = a_value?"1":"0";
0084 return true;
0085 }
0086 */
0087
0088 template <class T>
0089 inline bool numas(const T& a_value,std::string& a_s){
0090 std::string stmp;
0091 if(!num2s(a_value,stmp)) return false;
0092 a_s += stmp;
0093 return true;
0094 }
0095
0096 template <class T>
0097 inline bool size_tas(const T& a_value,std::string& a_s){
0098 std::string stmp;
0099 if(!size_t2s(a_value,stmp)) return false;
0100 a_s += stmp;
0101 return true;
0102 }
0103
0104 /*
0105 inline bool num2s(unsigned int a_linen,const char* a_lines[],std::string& a_s) {
0106 a_s.clear();
0107 for(unsigned int index=0;index<a_linen;index++) {
0108 a_s += a_lines[index];
0109 a_s += "\n";
0110 }
0111 return true;
0112 }
0113 */
0114
0115 // for the below std::vector num2s in case T=std::string :
0116 inline bool num2s(const std::string& a_value,std::string& a_s){a_s = a_value;return true;}
0117
0118 template <class T>
0119 class num_out : public std::string {
0120 typedef std::string parent;
0121 private:
0122 //typedef typename std::enable_if<std::is_floating_point<T>::value, T>::type type_t;
0123 //typedef typename std::enable_if<std::is_integral<T>::value, T>::type type_t;
0124 //typedef typename std::enable_if<std::is_arithmetic<T>::value, T>::type type_t;
0125 public:
0126 num_out(const T& a_value) {
0127 parent::operator+=("\"");
0128 if(!numas(a_value,*this)) {} //throw
0129 parent::operator+=("\"");
0130 }
0131 public:
0132 num_out(const num_out& a_from):parent(a_from){}
0133 num_out& operator=(const num_out& a_from){parent::operator=(a_from);return *this;}
0134 };
0135
0136 }
0137
0138 #include <vector>
0139
0140 namespace tools {
0141
0142 template <class VEC>
0143 inline bool nums2s(const VEC& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
0144 a_s.clear();
0145 typename VEC::size_type number = a_vals.size();
0146 if(number<=0) return true; //it is ok.
0147 number--;
0148 std::string stmp;
0149 bool status = true;
0150 for(typename VEC::size_type index=0;index<number;index++) {
0151 if(!num2s(a_vals[index],stmp)) status = false; //continue.
0152 a_s += stmp;
0153 a_s += a_sep;
0154 }
0155 if(!num2s(a_vals[number],stmp)) status = false;
0156 a_s += stmp;
0157 if(a_sep_at_end) a_s += a_sep;
0158 return status;
0159 }
0160
0161 template <class T>
0162 inline bool nums2s(const std::vector<T>& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
0163 return nums2s< std::vector<T> >(a_vals,a_s,a_sep,a_sep_at_end);
0164 }
0165
0166 }
0167
0168 #endif