g4tools  5.4.0
tos
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_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_value){return a_value;}
96 
97 template <class T>
98 inline std::string tos(const std::vector<T>& a_vals,
99  const std::string& a_sep = "\n",
100  bool a_sep_at_end = false) {
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,const char* a_lines[]) {
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
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::int64
long long int64
Definition: typedefs:67
typedefs
tools::tos
bool tos(const rotf &a_v, std::string &a_s)
Definition: rotf:68
tools::uint64_format
const char * uint64_format()
Definition: typedefs:74
sprintf
tools::int64_format
const char * int64_format()
Definition: typedefs:69
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
charmanip
tools::is_printable
bool is_printable(char a_char)
Definition: charmanip:109
tools::sprintf
bool sprintf(std::string &a_string, int a_length, const char *a_format,...)
Definition: sprintf:34
tools::tosx
std::string tosx(unsigned int a_value)
Definition: tos:55