g4tools  5.4.0
num2s
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
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 a std::ostringstream::operator<<(<num>).
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::string& a_s){
18  return print2s(a_s,32,"%c",a_value);
19 }
20 
21 inline bool num2s(char a_value,std::string& a_s){
22  return print2s(a_s,32,"%c",a_value);
23 }
24 */
25 
26 inline bool num2s(unsigned short a_value,std::string& a_s) { //used in value
27  return print2s(a_s,32,"%u",a_value);
28 }
29 
30 inline bool num2s(short a_value,std::string& a_s){ //used in value
31  return print2s(a_s,32,"%d",a_value);
32 }
33 
34 inline bool num2s(unsigned int a_value,std::string& a_s) {
35  return print2s(a_s,32,"%u",a_value);
36 }
37 
38 /*
39 inline bool num2sx(unsigned int a_value,std::string& a_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& a_s) {
49  return print2s(a_s,32,uint64_format(),a_value);
50 }
51 
52 inline bool num2s(int64 a_value,std::string& a_s){
53  return print2s(a_s,32,int64_format(),a_value);
54 }
55 
56 inline bool num2s(float a_value,std::string& a_s){
57  return print2s(a_s,32,"%g",a_value);
58 }
59 
60 inline bool num2s(double a_value,std::string& a_s){
61  return print2s(a_s,32,"%g",a_value);
62 }
63 
64 inline bool size_t2s(size_t a_value,std::string& a_s) {
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::string& a_s) { //used in write_bsg.
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_s){
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& a_s){
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::string& a_s){
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 char* a_lines[],std::string& a_s) {
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=std::string :
116 inline bool num2s(const std::string& a_value,std::string& a_s){a_s = a_value;return true;}
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_floating_point<T>::value, T>::type type_t;
123  //typedef typename std::enable_if<std::is_integral<T>::value, T>::type type_t;
124  //typedef typename std::enable_if<std::is_arithmetic<T>::value, T>::type type_t;
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){parent::operator=(a_from);return *this;}
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::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
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<number;index++) {
151  if(!num2s(a_vals[index],stmp)) status = false; //continue.
152  a_s += stmp;
153  a_s += a_sep;
154  }
155  if(!num2s(a_vals[number],stmp)) status = false;
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_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
163  return nums2s< std::vector<T> >(a_vals,a_s,a_sep,a_sep_at_end);
164 }
165 
166 }
167 
168 #endif
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::int64
long long int64
Definition: typedefs:67
typedefs
tools::nums2s
bool nums2s(const VEC &a_vals, std::string &a_s, const std::string &a_sep="\n", bool a_sep_at_end=false)
Definition: num2s:143
tools::numas
bool numas(const T &a_value, std::string &a_s)
Definition: num2s:89
tools::num_out::operator=
num_out & operator=(const num_out &a_from)
Definition: num2s:133
tools::ptrdiff_t2s
bool ptrdiff_t2s(ptrdiff_t a_value, std::string &a_s)
Definition: num2s:72
tools::size_t2s
bool size_t2s(size_t a_value, std::string &a_s)
Definition: num2s:64
tools::size_tas
bool size_tas(const T &a_value, std::string &a_s)
Definition: num2s:97
tools::print2s
bool print2s(std::string &a_string, int a_length, const char *a_format,...)
Definition: sprintf:77
tools::uint64_format
const char * uint64_format()
Definition: typedefs:74
tools::int32
int int32
Definition: typedefs:66
sprintf
tools::int64_format
const char * int64_format()
Definition: typedefs:69
tools::num_out
Definition: num2s:119
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::num_out::num_out
num_out(const num_out &a_from)
Definition: num2s:132
tools::num_out::num_out
num_out(const T &a_value)
Definition: num2s:126
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::num2s
bool num2s(unsigned short a_value, std::string &a_s)
Definition: num2s:26