lexical_cast.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <sstream>
16 #include <iomanip>
17 #include <limits>
18 #include "interop/util/math.h"
20 
21 namespace illumina { namespace interop { namespace util
22 {
27  template<class Source, class Destination>
29 
34  template<class Destination>
35  struct lexical_cast_helper<std::string, Destination>
36  {
42  static Destination cast(const std::string &str)
43  {
44  if (is_nan(str, static_cast<Destination*>(0)))
45  {
46  return nan_value(static_cast<Destination*>(0));
47  }
48  if(is_inf(str, static_cast<Destination*>(0)))
49  {
50  return inf_value(static_cast<Destination*>(0));
51  }
52  std::istringstream iss(str);
53  Destination val = Destination();
54  iss >> val;
55  return val;
56  }
57 
58  private:
59  static double nan_value(double*)
60  {
61  return std::numeric_limits<double>::quiet_NaN();
62  }
63  static float nan_value(float*)
64  {
65  return std::numeric_limits<float>::quiet_NaN();
66  }
67  static Destination nan_value(void*)
68  {
69  return Destination();
70  }
71  static bool is_nan(const std::string &str, double*)
72  {
73  return is_string_nan(str);
74  }
75  static bool is_nan(const std::string &str, float*)
76  {
77  return is_string_nan(str);
78  }
79  static bool is_nan(const std::string&, void*)
80  {
81  return false;
82  }
83  static bool is_string_nan(const std::string &str)
84  {
85  const size_t n = str.length();
86  if(n >= 3 && ::tolower(str[n-3]) =='n' && ::tolower(str[n-2]) == 'a' && ::tolower(str[n-1]) == 'n')
87  {
88  return true;
89  }
90  return false;
91  }
92  static double inf_value(double*)
93  {
94  return std::numeric_limits<double>::infinity();
95  }
96  static float inf_value(float*)
97  {
98  return std::numeric_limits<float>::infinity();
99  }
100  static Destination inf_value(void*)
101  {
102  return Destination();
103  }
104  static bool is_inf(const std::string &str, double*)
105  {
106  return is_string_inf(str);
107  }
108  static bool is_inf(const std::string &str, float*)
109  {
110  return is_string_inf(str);
111  }
112  static bool is_inf(const std::string&, void*)
113  {
114  return false;
115  }
116  static bool is_string_inf(const std::string &str)
117  {
118  const size_t n = str.length();
119  if(n >= 3 && ::tolower(str[n-3]) =='i' && ::tolower(str[n-2]) == 'n' && ::tolower(str[n-1]) == 'f')
120  {
121  return true;
122  }
123  return false;
124  }
125  };
126 
131  template<class Destination>
132  struct lexical_cast_helper<const char *, Destination>
133  {
139  static Destination cast(const char *str)
140  {
142  }
143  };
144 
149  template<class Destination>
150  struct lexical_cast_helper<char *, Destination>
151  {
157  static Destination cast(const char *str)
158  {
160  }
161  };
162 
167  template<class Source>
168  struct lexical_cast_helper<Source, std::string>
169  {
175  static std::string cast(const Source &source)
176  {
177  std::ostringstream oss;
178  oss << source;
179  return oss.str();
180  }
181  };
182 
187  template<class Source>
188  struct lexical_cast_helper<const Source &, std::string> : lexical_cast_helper<Source, std::string>
189  {
190  };
191 
194  template<>
195  struct lexical_cast_helper<std::string, std::string>
196  {
202  static const std::string &cast(const std::string &source)
203  {
204  return source;
205  }
206  };
207 
210  template<>
211  struct lexical_cast_helper<const char *, std::string>
212  {
218  static std::string cast(const char *source)
219  {
220  return std::string(source);
221  }
222  };
223 
226  template<>
227  struct lexical_cast_helper<char *, std::string>
228  {
234  static std::string cast(const char *source)
235  {
236  return std::string(source);
237  }
238  };
239 
242  template<>
243  struct lexical_cast_helper<std::string, const char *>
244  {
250  static const char *cast(const std::string &source)
251  {
252  return source.c_str();
253  }
254  };
255 
263  template<class Destination, class Source>
264  inline Destination lexical_cast(const Source &src)
265  {
267  }
268 
278  inline std::string format(const float val, const int width, const int precision, const char fill = ' ',
279  const bool fixed = true)
280  {
281  std::ostringstream oss;
282  if(std::isnan(val))
283  {
284  oss << std::numeric_limits<float>::quiet_NaN();
285  return oss.str();
286  }
287  if (fixed) oss << std::fixed;
288  if (width > -1) oss << std::setw(width);
289  if (precision > -1) oss << std::setprecision(precision);
290  if (fill != 0) oss << std::setfill(fill);
291  oss << val;
292  return oss.str();
293  }
294 
295 }}}
296 
297 
static Destination cast(const std::string &str)
Definition: lexical_cast.h:42
Definition: enum_description.h:15
static const std::string & cast(const std::string &source)
Definition: lexical_cast.h:202
static std::string cast(const char *source)
Definition: lexical_cast.h:218
char tolower(const char ch)
Definition: string.h:25
static Destination cast(const char *str)
Definition: lexical_cast.h:157
static const char * cast(const std::string &source)
Definition: lexical_cast.h:250
std::string format(const float val, const int width, const int precision, const char fill= ' ', const bool fixed=true)
Definition: lexical_cast.h:278
static std::string cast(const char *source)
Definition: lexical_cast.h:234
static Destination cast(const char *str)
Definition: lexical_cast.h:139
static std::string cast(const Source &source)
Definition: lexical_cast.h:175
Destination lexical_cast(const Source &src)
Definition: lexical_cast.h:264