Tapkee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wrappers.hpp
Go to the documentation of this file.
1 
30 #ifndef FORMATTING_WRAPPERS_H_
31 #define FORMATTING_WRAPPERS_H_
32 
33 #include <limits>
34 #include <iomanip>
35 
36 namespace formatting
37 {
38 
39 namespace utils
40 {
41  template <bool> struct compile_time_assert;
42  template <> struct compile_time_assert<true> {};
43 }
44 
45 namespace wrappers
46 {
47  template <typename T>
48  struct HexWrapper
49  {
51  explicit HexWrapper(T value) : value_(value) { }
52  const T value_;
53 
54  template <typename U>
55  friend std::ostream& operator<<(std::ostream& out, const HexWrapper<U>& h);
56  };
57 
58  template <typename T>
59  std::ostream& operator<<(std::ostream& out, const HexWrapper<T>& h)
60  {
61  out << "0x" << std::hex << std::uppercase << h.value_;
62  return out;
63  }
64 
65  template <typename T>
66  struct OctWrapper
67  {
69  explicit OctWrapper(T value) : value_(value) { }
70  const T value_;
71 
72  template <typename U>
73  friend std::ostream& operator<<(std::ostream& out, const HexWrapper<U>& h);
74  };
75 
76  template <typename T>
77  std::ostream& operator<<(std::ostream& out, const OctWrapper<T>& h)
78  {
79  out << "0" << std::oct << std::uppercase << h.value_;
80  return out;
81  }
82 
83  template <typename T>
84  struct WidthWrapper
85  {
86  explicit WidthWrapper(unsigned int width, T value) : value_(value), width_(width) { }
87  const T value_;
88  const unsigned int width_;
89 
90  template <typename U>
91  friend std::ostream& operator<<(std::ostream& out, const WidthWrapper& h);
92  };
93 
94  template <typename T>
95  std::ostream& operator<<(std::ostream& out, const WidthWrapper<T>& h)
96  {
97  out << std::setw(h.width_) << h.value_;
98  return out;
99  }
100 
102  {
103  explicit WidthWrapperBuilder(unsigned int width) : width_(width) { }
104  unsigned int width_;
105 
106  template <typename T>
107  inline WidthWrapper<T> operator()(T value)
108  {
109  return WidthWrapper<T>(width_,value);
110  }
111  };
112 
114  {
116  inline wrappers::WidthWrapperBuilder operator[](unsigned int w) const
117  {
119  }
120  };
121 
122  template <typename T>
124  {
126  explicit PrecisionWrapper(unsigned int precision, T value) : value_(value), precision_(precision) { }
127  const T value_;
128  const unsigned int precision_;
129 
130  template <typename U>
131  friend std::ostream& operator<<(std::ostream& out, const PrecisionWrapper& h);
132  };
133 
134  template <typename T>
135  std::ostream& operator<<(std::ostream& out, const PrecisionWrapper<T>& h)
136  {
137  out << std::setprecision(h.precision_) << h.value_;
138  return out;
139  }
140 
142  {
143  explicit PrecisionWrapperBuilder(unsigned int precision) : precision_(precision) { }
144  unsigned int precision_;
145 
146  template <typename T>
148  {
149  return PrecisionWrapper<T>(precision_,value);
150  }
151  };
152 
154  {
156  inline wrappers::PrecisionWrapperBuilder operator[](unsigned int p) const
157  {
159  }
160  };
161 
162 
163 }
164 
172 template<typename T>
173 inline wrappers::HexWrapper<T> hex(T value)
174 {
175  return wrappers::HexWrapper<T>(value);
176 }
177 
185 template<typename T>
187 {
188  return wrappers::OctWrapper<T>(value);
189 }
190 
196 inline wrappers::HexWrapper<size_t> raw(void* value)
197 {
198  size_t ptr = reinterpret_cast<size_t>(value);
199  return wrappers::HexWrapper<size_t>(ptr);
200 }
201 
207 inline wrappers::HexWrapper<size_t> raw(const void* value)
208 {
209  size_t ptr = reinterpret_cast<size_t>(value);
210  return wrappers::HexWrapper<size_t>(ptr);
211 }
212 
217 
223 
224 }
225 #endif
static const wrappers::PrecisionWrapperBuilderHelper precision
Definition: wrappers.hpp:222
wrappers::OctWrapper< T > oct(T value)
Definition: wrappers.hpp:186
PrecisionWrapperBuilder(unsigned int precision)
Definition: wrappers.hpp:143
static const wrappers::WidthWrapperBuilderHelper width
Definition: wrappers.hpp:216
utils::compile_time_assert< std::numeric_limits< T >::is_integer > HEX_USED_FOR_NON_INTEGER_TYPE
Definition: wrappers.hpp:50
wrappers::HexWrapper< size_t > raw(void *value)
Definition: wrappers.hpp:196
friend std::ostream & operator<<(std::ostream &out, const PrecisionWrapper &h)
PrecisionWrapper(unsigned int precision, T value)
Definition: wrappers.hpp:126
utils::compile_time_assert< std::numeric_limits< T >::is_specialized > PRECISION_USED_FOR_NON_NUMERIC_TYPE
Definition: wrappers.hpp:125
wrappers::PrecisionWrapperBuilder operator[](unsigned int p) const
Definition: wrappers.hpp:156
friend std::ostream & operator<<(std::ostream &out, const WidthWrapper &h)
PrecisionWrapper< T > operator()(T value)
Definition: wrappers.hpp:147
wrappers::WidthWrapperBuilder operator[](unsigned int w) const
Definition: wrappers.hpp:116
utils::compile_time_assert< std::numeric_limits< T >::is_integer > OCT_USED_FOR_NON_INTEGER_TYPE
Definition: wrappers.hpp:68
WidthWrapper(unsigned int width, T value)
Definition: wrappers.hpp:86
wrappers::HexWrapper< T > hex(T value)
Definition: wrappers.hpp:173
WidthWrapper< T > operator()(T value)
Definition: wrappers.hpp:107