GCS  0.2.3
gu_utils.hpp
1 // Copyright (C) 2009-2010 Codership Oy <info@codership.com>
2 
9 #ifndef _gu_utils_hpp_
10 #define _gu_utils_hpp_
11 
12 #include <string>
13 #include <sstream>
14 #include <iomanip>
15 #include <limits>
16 
17 #include "gu_exception.hpp"
18 //#include "gu_string.hpp"
19 
20 namespace gu {
21 
22 /*
23  * String conversion functions for primitive types
24  */
26 template <typename T>
27 inline std::string to_string(const T& x,
28  std::ios_base& (*f)(std::ios_base&) = std::dec)
29 {
30  std::ostringstream out;
31  out << std::showbase << f << x;
32  return out.str();
33 }
34 
35 
37 template <>
38 inline std::string to_string<bool>(const bool& x,
39  std::ios_base& (*f)(std::ios_base&))
40 {
41  std::ostringstream out;
42  out << std::boolalpha << x;
43  return out.str();
44 }
45 
47 template <>
48 inline std::string to_string<double>(const double& x,
49  std::ios_base& (*f)(std::ios_base&))
50 {
51  const int sigdigits = std::numeric_limits<double>::digits10;
52  // or perhaps std::numeric_limits<double>::max_digits10?
53  std::ostringstream out;
54  out << std::setprecision(sigdigits) << x;
55  return out.str();
56 }
57 
60 template <typename T> inline T
61 from_string(const std::string& s,
62  std::ios_base& (*f)(std::ios_base&) = std::dec)
63 {
64  std::istringstream iss(s);
65  T ret;
66 
67  try
68  {
69  if ((iss >> f >> ret).fail()) throw NotFound();
70  }
71  catch (gu::Exception& e)
72  {
73  throw NotFound();
74  }
75  return ret;
76 }
77 
80 template <> inline std::string
81 from_string<std::string>(const std::string& s,
82  std::ios_base& (*f)(std::ios_base&))
83 {
84  return s;
85 }
86 
89 template <> inline void* from_string<void*>(const std::string& s,
90  std::ios_base& (*f)(std::ios_base&))
91 {
92  std::istringstream iss(s);
93  void* ret;
94 
95  if ((iss >> std::hex >> ret).fail()) throw NotFound();
96 
97  return ret;
98 }
99 
100 extern "C" const char* gu_str2bool (const char* str, bool* bl);
101 
104 template <> inline bool from_string<bool> (const std::string& s,
105  std::ios_base& (*f)(std::ios_base&))
106 {
107  bool ret;
108  const char* endptr(gu_str2bool(s.c_str(), &ret));
109  if (endptr == 0 || *endptr != '\0') throw NotFound();
110  return ret;
111 }
112 
119 template <typename T> class VLA
120 {
121  T* array;
122 
123  VLA (const VLA&);
124  VLA& operator= (const VLA&);
125 
126 public:
127 
128  VLA (size_t n) : array(new T[n]) {}
129 
130  ~VLA () { delete[] array; }
131 
132  T* operator& () { return array; }
133 
134  T& operator[] (size_t i) { return array[i]; }
135 };
136 
137 
152 {
153 public:
154  template <class T> void operator()(T* t) { delete t; }
155 };
156 
157 
158 } // namespace gu
159 
160 #endif /* _gu_utils_hpp_ */
std::string to_string(const T &x, std::ios_base &(*f)(std::ios_base &)=std::dec)
Definition: gu_utils.hpp:27
T from_string(const std::string &s, std::ios_base &(*f)(std::ios_base &)=std::dec)
Definition: gu_utils.hpp:61
std::string to_string< double >(const double &x, std::ios_base &(*f)(std::ios_base &))
Definition: gu_utils.hpp:48
bool from_string< bool >(const std::string &s, std::ios_base &(*f)(std::ios_base &))
Definition: gu_utils.hpp:104
std::string to_string< bool >(const bool &x, std::ios_base &(*f)(std::ios_base &))
Definition: gu_utils.hpp:38
void * from_string< void * >(const std::string &s, std::ios_base &(*f)(std::ios_base &))
Definition: gu_utils.hpp:89
Definition: gu_exception.hpp:19
Definition: gu_exception.hpp:17
Definition: gu_utils.hpp:119
Definition: gu_utils.hpp:151