iutest  1.17.1.0
iutest_string_stream.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
16 #define INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
17 
18 //======================================================================
19 // include
20 #include "iutest_string.hpp"
21 #if IUTEST_HAS_STRINGSTREAM
22 # include <sstream>
23 #elif IUTEST_HAS_STRSTREAM
24 # include <strstream>
25 #endif
26 #if IUTEST_HAS_IOMANIP
27 # include <iomanip>
28 #endif
29 #if IUTEST_HAS_EXCEPTIONS
30 # include <stdexcept>
31 #endif
32 
33 namespace iutest
34 {
35 
36 namespace detail
37 {
38 
39 template<typename T>
40 bool StringToValue(const ::std::string& s, T& out)
41 {
42  ::std::istringstream strm(s);
43  if( strm >> out )
44  {
45  return true;
46  }
47  return false;
48 }
49 
50 inline bool StringToValue(const ::std::string& s, float& out)
51 {
52 #if IUTEST_HAS_STD_STR_TO_VALUE
53  out = ::std::stof(s);
54 #else
55  char* endptr=NULL;
56  const char* p = s.c_str();
57  errno = 0;
58 #if !defined(IUTEST_OS_WINDOWS_MINGW) || !defined(__STRICT_ANSI__)
59  const floating_point<float> v = strtof(p, &endptr);
60 #else
61  const floating_point<float> v = static_cast<float>(strtod(p, &endptr));
62 #endif
63 #if IUTEST_HAS_EXCEPTIONS
64  if(p == endptr)
65  {
66  throw ::std::invalid_argument(p);
67  }
68  if((errno == ERANGE) || v.is_inf() )
69  {
70  throw ::std::out_of_range(p);
71  }
72 #endif
73  out = v;
74 #endif
75  return true;
76 }
77 
78 inline bool StringToValue(const ::std::string& s, double& out)
79 {
80 #if IUTEST_HAS_STD_STR_TO_VALUE
81  out = ::std::stod(s);
82 #else
83  char* endptr=NULL;
84  const char* p = s.c_str();
85  errno = 0;
86  const floating_point<double> v = strtod(s.c_str(), &endptr);
87 #if IUTEST_HAS_EXCEPTIONS
88  if(p == endptr)
89  {
90  throw ::std::invalid_argument(p);
91  }
92  if((errno == ERANGE) || v.is_inf() )
93  {
94  throw ::std::out_of_range(p);
95  }
96 #endif
97  out = v;
98 #endif
99  return true;
100 }
101 
102 #if IUTEST_HAS_LONG_DOUBLE
103 
104 inline bool StringToValue(const ::std::string& s, long double& out)
105 {
106 #if IUTEST_HAS_STD_STR_TO_VALUE
107  out = ::std::stold(s);
108 #else
109  char* endptr=NULL;
110  const char* p = s.c_str();
111  errno = 0;
112  const floating_point<long double> v = strtold(s.c_str(), &endptr);
113 #if IUTEST_HAS_EXCEPTIONS
114  if(p == endptr)
115  {
116  throw ::std::invalid_argument(p);
117  }
118  if((errno == ERANGE) || v.is_inf() )
119  {
120  throw ::std::out_of_range(p);
121  }
122 #endif
123  out = v;
124 #endif
125  return true;
126 }
127 
128 #endif
129 
130 #if !IUTEST_HAS_STRINGSTREAM && !IUTEST_HAS_STRSTREAM
131 
132 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
133 
134 //======================================================================
135 // class
136 
137 template<class _Elem, class _Traits>
138 class iu_basic_ostream
139 {
140  typedef iu_basic_ostream<_Elem, _Traits> _Myt;
141  //typedef ::std::basic_streambuf<_Elem, _Traits> streambuf;
142  //typedef ::std::basic_ostream<_Elem, _Traits> ostream;
143  typedef ::std::basic_string<_Elem, _Traits> string;
144  string s;
145 
146  template<typename T>
147  struct xcs
148  {
149  private:
150  template<typename TMP, typename TN>
151  struct impl_select
152  {
153  template<typename TA, typename TB>
154  static const TA constant(const TA a, const TB b)
155  {
156  (void)b;
157  return a;
158  }
159  };
160  template<typename TMP>
161  struct impl_select<TMP, wchar_t>
162  {
163  template<typename TA, typename TB>
164  static const TB constant(const TA a, const TB b)
165  {
166  (void)a;
167  return b;
168  }
169  };
170 
171  public:
172  typedef impl_select<void, T> select;
173  };
174 #define IIUT_PP_XCS(txt_) xcs<_Elem>::select::constant(txt_, L##txt_)
175 
176  struct impl
177  {
178  template<typename E>
179  static int vastring(E* dst, const E* fmt, va_list va);
180  static int vastring(char* dst, size_t len, const char* fmt, va_list va)
181  {
182  (void)len;
183  return vsprintf(dst, fmt, va);
184  }
185  static int vastring(wchar_t* dst, size_t len, const wchar_t* fmt, va_list va)
186  {
187 #ifdef IUTEST_OS_WINDOWS_MINGW
188  return _vsnwprintf(dst, len, fmt, va);
189 #else
190  return vswprintf(dst, len, fmt, va);
191 #endif
192  }
193 
194  template<typename E>
195  static int tostring(E* dst, size_t len, const E* fmt, ...)
196  {
197  va_list va;
198  va_start(va, fmt);
199  int ret = vastring(dst, len, fmt, va);
200  va_end(va);
201  return ret;
202  }
203  };
204 public:
205  iu_basic_ostream() {}
206  explicit iu_basic_ostream(const char* str) : s(str) {}
207  explicit iu_basic_ostream(const ::std::string& str) : s(str) {}
208 
209 public:
210  inline _Myt& operator<< (char v)
211  {
212  s += v;
213  return *this;
214  }
215  inline _Myt& operator<< (signed char v)
216  {
217  s += static_cast<char>(v);
218  return *this;
219  }
220  inline _Myt& operator<< (unsigned char v)
221  {
222  s += static_cast<char>(v);
223  return *this;
224  }
225  inline _Myt& operator<< (const _Elem* v)
226  {
227  s += v;
228  return *this;
229  }
230  //inline _Myt& operator<< (const signed _Elem* v)
231  //{
232  // s += v;
233  // return *this;
234  //}
235  //inline _Myt& operator<< (const unsigned _Elem* v)
236  //{
237  // s += v;
238  // return *this;
239  //}
240  inline _Myt& operator<< (bool v)
241  {
242 #if 0
243  _Elem a[16];
244  impl::tostring(a, 16, IIUT_PP_XCS("%i"), v);
245  s += a;
246 #else
247  s += (v ? IIUT_PP_XCS("true") : IIUT_PP_XCS("false"));
248 #endif
249  return *this;
250  }
251  inline _Myt& operator<< (short v)
252  {
253  _Elem a[64];
254  impl::tostring(a, 64, IIUT_PP_XCS("%i"), v);
255  s += a;
256  return *this;
257  }
258  inline _Myt& operator<< (unsigned short v)
259  {
260  _Elem a[64];
261  impl::tostring(a, 64, IIUT_PP_XCS("%u"), v);
262  s += a;
263  return *this;
264  }
265  inline _Myt& operator<< (int v)
266  {
267  _Elem a[64];
268  impl::tostring(a, 64, IIUT_PP_XCS("%i"), v);
269  s += a;
270  return *this;
271  }
272  inline _Myt& operator<< (unsigned int v)
273  {
274  _Elem a[64];
275  impl::tostring(a, 64, IIUT_PP_XCS("%u"), v);
276  s += a;
277  return *this;
278  }
279  inline _Myt& operator<< (long v)
280  {
281  _Elem a[64];
282  impl::tostring(a, 64, IIUT_PP_XCS("%i"), v);
283  s += a;
284  return *this;
285  }
286  inline _Myt& operator<< (unsigned long v)
287  {
288  _Elem a[64];
289  impl::tostring(a, 64, IIUT_PP_XCS("%u"), v);
290  s += a;
291  return *this;
292  }
293  inline _Myt& operator<< (long long int v)
294  {
295  _Elem a[64];
296  impl::tostring(a, 64, IIUT_PP_XCS("%lld"), v);
297  s += a;
298  return *this;
299  }
300  inline _Myt& operator<< (unsigned long long int v)
301  {
302  _Elem a[64];
303  impl::tostring(a, 64, IIUT_PP_XCS("%llu"), v);
304  s += a;
305  return *this;
306  }
307  inline _Myt& operator<< (float v)
308  {
309  _Elem a[64];
310  impl::tostring(a, 64, IIUT_PP_XCS("%f"), v);
311  s += a;
312  return *this;
313  }
314  inline _Myt& operator<< (double v)
315  {
316  _Elem a[64];
317  impl::tostring(a, 64, IIUT_PP_XCS("%l"), v);
318  s += a;
319  return *this;
320  }
321  inline _Myt& operator<< (long double v)
322  {
323  _Elem a[64];
324  impl::tostring(a, 64, IIUT_PP_XCS("%L"), v);
325  s += a;
326  return *this;
327  }
328  inline _Myt& operator<< (const void* v)
329  {
330  _Elem a[64];
331  impl::tostring(a, 64, IIUT_PP_XCS("%t"), v);
332  s += a;
333  return *this;
334  }
335  inline _Myt& operator<< (const ::std::string& v)
336  {
337  s += v;
338  return *this;
339  }
340 public:
341  const string& str() const { return s; }
342  void copyfmt(const _Myt&) {}
343 };
344 
345 #undef IIUT_PP_XCS
346 
347 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
348 
349 #endif
350 
351 //======================================================================
352 // declare
353 #if IUTEST_HAS_STRINGSTREAM
354 
355 typedef ::std::stringstream stlstream;
356 
357 #elif IUTEST_HAS_STRSTREAM
358 
359 IUTEST_PRAGMA_MSC_WARN_PUSH()
360 IUTEST_PRAGMA_MSC_WARN_DISABLE(4250)
361 class stlstream : public ::std::strstream
362 {
363  char buf[512];
364 public:
365  stlstream()
366  : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
367  {}
368  explicit stlstream(const char* str)
369  : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
370  {
371  *this << str;
372  }
373  explicit stlstream(const ::std::string& str)
374  : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
375  {
376  *this << str;
377  }
378 public:
379  ::std::string str() const
380  {
381  return const_cast<stlstream*>(this)->str();
382  }
383  virtual ::std::string str()
384  {
385  *this << ::std::ends;
386  ::std::string str = ::std::strstream::str();
387  return str;
388  }
389 };
390 
391 IUTEST_PRAGMA_MSC_WARN_POP()
392 
393 #endif
394 
395 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
396 
397 } // end of namespace detail
398 
399 #if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
400 
401 typedef ::std::ostream iu_ostream;
402 typedef detail::stlstream iu_stringstream;
403 
404 #else
405 
406 typedef detail::iu_basic_ostream<char, ::std::char_traits<char> > iu_ostream;
407 typedef detail::iu_basic_ostream<wchar_t, ::std::char_traits<wchar_t> > iu_wostream;
408 typedef iu_ostream iu_stringstream;
409 
410 #endif
411 
412 #if IUTEST_HAS_IOMANIP
413 typedef iu_ostream& (*iu_basic_iomanip)(iu_ostream&);
414 #endif
415 
416 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
417 # if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
418 # if (defined(_STLPORT_VERSION) && !defined(_STLP_LONG_LONG)) || (defined(_MSC_VER) && _MSC_VER < 1310)
419 # define IUTEST_HAS_BIGGESTINT_OSTREAM 0
420 # endif
421 # endif
422 #endif
423 
424 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
425 # define IUTEST_HAS_BIGGESTINT_OSTREAM 1
426 #endif
427 
428 } // end of namespace iutest
429 
430 #endif // INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
iutest_config.hpp
iris unit test config
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
iutest_string.hpp
iris unit test string utilities