iutest  1.17.99.14
iutest_stdlib.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_STDLIB_HPP_54D4BEEE_7B6B_4AF4_B1F6_138560480D55_
16 #define INCG_IRIS_IUTEST_STDLIB_HPP_54D4BEEE_7B6B_4AF4_B1F6_138560480D55_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "iutest_stdlib_defs.hpp"
22 // IWYU pragma: end_exports
23 
24 //======================================================================
25 // declare
26 
28 #if !defined(IUTEST_USING_BEGIN_END)
29 # if IUTEST_HAS_STD_BEGIN_END
30 # define IUTEST_USING_BEGIN_END() \
31  using ::std::begin; using ::std::end
32 # else
33 # define IUTEST_USING_BEGIN_END() \
34  using ::iutest::detail::cxx::begin; using ::iutest::detail::cxx::end
35 # endif
36 #endif
37 
38 namespace iutest {
39 namespace detail {
40 namespace cxx
41 {
42 
43 #if IUTEST_HAS_STD_BEGIN_END
44 
45 using ::std::begin;
46 using ::std::end;
47 
48 #else
49 
50 template<typename T> typename T::iterator begin(T& x) { return x.begin(); }
51 template<typename T> typename T::iterator end (T& x) { return x.end(); }
52 
53 template<typename T> typename T::const_iterator begin(const T& x) { return x.begin(); }
54 template<typename T> typename T::const_iterator end (const T& x) { return x.end(); }
55 
56 #if !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
57 template<typename T, size_t SIZE> T* begin(T (&x)[SIZE]) { return &x[0]; }
58 template<typename T, size_t SIZE> T* end (T (&x)[SIZE]) { return begin(x) + SIZE; }
59 
60 template<typename T, size_t SIZE> const T* begin(const T (&x)[SIZE]) { return &x[0]; }
61 template<typename T, size_t SIZE> const T* end (const T (&x)[SIZE]) { return begin(x) + SIZE; }
62 #endif
63 
64 #endif
65 
66 } // end of namespace cxx
67 } // end of namespace detail
68 } // end of namespace iutest
69 
70 #if IUTEST_HAS_TUPLE
71 // IWYU pragma: begin_exports
72 #if !IUTEST_USE_EXTERNAL_STD_TUPLE && !IUTEST_USE_EXTERNAL_TR1_TUPLE
73 # if IUTEST_HAS_STD_TUPLE
74 # include <tuple>
75 # elif IUTEST_HAS_TR1_TUPLE
76 # if (defined(__GNUC__) && (__GNUC__ >= 4))
77 # include <tr1/tuple>
78 # else
79 # include <tuple>
80 # endif
81 # endif
82 #endif
83 // IWYU pragma: end_exports
84 
85 namespace iutest {
86 namespace tuples
87 {
88 
89 #if IUTEST_HAS_STD_TUPLE
90 namespace alias = ::std;
91 #elif IUTEST_HAS_TR1_TUPLE
92 namespace alias = ::std::tr1;
93 #endif
94 
95 using alias::tuple;
96 using alias::tuple_element;
97 using alias::make_tuple;
98 using alias::get;
99 
100 template<typename T>struct tuple_size : public alias::tuple_size<T> {};
101 template<typename T>struct tuple_size<const T> : public alias::tuple_size<T> {};
102 template<typename T>struct tuple_size<volatile T> : public alias::tuple_size<T>{};
103 template<typename T>struct tuple_size<const volatile T> : public alias::tuple_size<T>{};
104 
105 namespace detail
106 {
107 
108 template<typename T, typename F, int Begin>
109 struct tuple_foreach_impl
110 {
111  template<int N, int I>
112  struct impl
113  {
114  static void do_something(T& t, F fn)
115  {
116  fn(I, get<I>(t));
118  }
119  };
120  template<int N>
121  struct impl<N, N>
122  {
123  static void do_something(T&, F) {}
124  };
125 
126  static void do_something(T& t, F fn)
127  {
128  impl<tuple_size<T>::value, Begin>::do_something(t, fn);
129  }
130 };
131 
132 template<typename T, typename U>
133 struct tuple_cast_copy_impl
134 {
135  template<int N, int I>
136  struct impl
137  {
138  static void copy(T& dst, const U& src)
139  {
140  get<I>(dst) = static_cast<typename tuple_element<I, T>::type>(get<I>(src));
141  impl<N, I + 1>::copy(dst, src);
142  }
143  };
144  template<int N>
145  struct impl<N, N>
146  {
147  static void copy(T&, const U&) {}
148  };
149 
150  static void copy(T& dst, const U& src)
151  {
152  impl<tuple_size<T>::value, 0>::copy(dst, src);
153  }
154 };
155 
156 } // end of namespace detail
157 
158 template<int I, typename tuple_t, typename F>
159 void tuple_foreach(tuple_t& t, F& fn)
160 {
161  detail::tuple_foreach_impl<tuple_t, F&, I>::do_something(t, fn);
162 }
163 template<typename tuple_t, typename F>
164 void tuple_foreach(tuple_t& t, F& fn)
165 {
166  tuple_foreach<0>(t, fn);
167 }
168 template<int I, typename tuple_t, typename F>
169 void tuple_foreach(tuple_t& t, const F& fn)
170 {
171  detail::tuple_foreach_impl<tuple_t, const F&, I>::do_something(t, fn);
172 }
173 template<typename tuple_t, typename F>
174 void tuple_foreach(tuple_t& t, const F& fn)
175 {
176  tuple_foreach<0>(t, fn);
177 }
178 template<typename T, typename U>
179 void tuple_cast_copy(T& dst, const U& src)
180 {
181  detail::tuple_cast_copy_impl<T, U>::copy(dst, src);
182 }
183 
184 } // end of namespace tuples
185 
186 using tuples::tuple;
187 using tuples::tuple_size;
188 using tuples::tuple_element;
189 using tuples::tuple_foreach;
190 using tuples::make_tuple;
191 using tuples::get;
192 
193 } // end of namespace iutest
194 
195 #endif
196 
197 namespace iutest
198 {
199 
200 namespace stl
201 {
202 
203 // #if IUTEST_HAS_CXX_HDR_OPTIONAL
204 // #else
205 // #endif
206 
207 #if IUTEST_HAS_EXCEPTIONS
211 class bad_optional_access : public ::std::exception {};
212 #endif
213 
214 class nullpot_t {};
215 
216 template<typename T>
217 class optional
218 {
219 public:
220  typedef T value_type;
221 public:
222  optional() IUTEST_CXX_NOEXCEPT_SPEC : m_init(false) {}
223  explicit optional(nullpot_t) IUTEST_CXX_NOEXCEPT_SPEC : m_init(false) {}
224  explicit optional(const T& rhs) : m_init(true), m_value(rhs) {}
225  template<typename U>
226  explicit optional(const U& rhs) : m_init(true), m_value(T(rhs)) {}
227  optional(const optional& rhs) : m_init(rhs.m_init), m_value(rhs.value) {}
228 
229 public:
230  optional& operator = (const T& rhs) { m_init = true; m_value = rhs; return *this; }
231  operator bool () const { return has_value(); }
232  const T& operator * () const { return value(); }
233  T& operator * () { return value(); }
234  const T* operator -> () const { return ptr(); }
235  T* operator -> () { return ptr(); }
236 
237 public:
238  bool has_value() const { return m_init; }
239  const T* ptr() const { return &m_value; }
240  T* ptr() { return &m_value; }
241  const T& value() const
242  {
243 #if IUTEST_HAS_EXCEPTIONS
244  if( !has_value() ) {
245  throw bad_optional_access();
246  }
247 #endif
248  return m_value;
249  }
250  T& value()
251  {
252 #if IUTEST_HAS_EXCEPTIONS
253  if( !has_value() ) {
254  throw bad_optional_access();
255  }
256 #endif
257  return m_value;
258  }
259  template<typename U>
260  T value_or(const U& v) const
261  {
262  if( !has_value() ) {
263  return v;
264  }
265  return m_value;
266  }
267 
268 private:
269  bool m_init;
270  T m_value;
271 };
272 
273 inline bool uncaught_exception()
274 {
275 #if IUTEST_HAS_CXX1Z && (!defined(IUTEST_LIBSTDCXX_VERSION) || (IUTEST_LIBSTDCXX_VERSION >= 60000))
276  return ::std::uncaught_exceptions() > 0;
277 #else
278  return ::std::uncaught_exception();
279 #endif
280 }
281 
282 } // end of namespace stl
283 
284 namespace detail
285 {
286 
287 //======================================================================
288 // struct
289 
290 namespace type_t_helper
291 {
292 
296 template<size_t SIZE>
297 struct type_fit_t {};
298 
300 template<>
301 struct type_fit_t<1>
302 {
303 #if defined(INT8_MIN)
304  typedef int8_t Int;
305  typedef uint8_t UInt;
306 #else
307  typedef char Int;
308  typedef unsigned char UInt;
309 #endif
310 };
311 
313 template<>
314 struct type_fit_t<2>
315 {
316 #if defined(INT16_MIN)
317  typedef int16_t Int;
318  typedef uint16_t UInt;
319 #else
320  typedef short Int;
321  typedef unsigned short UInt;
322 #endif
323 };
324 
326 template<>
327 struct type_fit_t<4>
328 {
329 #if defined(INT32_MIN)
330  typedef int32_t Int;
331  typedef uint32_t UInt;
332 #else
333 #if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
334 private:
335  template<typename T, typename F, bool b>
336  struct impl { typedef T type; };
337  template<typename T, typename F>
338  struct impl<T, F, false> { typedef F type; };
339 
340 public:
341  typedef impl<long, int
342  , sizeof(int) != 4 && sizeof(long) == 4>::type Int;
343  typedef impl<unsigned long, unsigned int
344  , sizeof(int) != 4 && sizeof(long) == 4>::type UInt;
345 #else
346  typedef int Int;
347  typedef unsigned int UInt;
348 #endif
349 #endif
350 };
351 
353 template<>
354 struct type_fit_t<8>
355 {
356 #if defined(INT64_MIN)
357  typedef int64_t Int;
358  typedef uint64_t UInt;
359 #else
360 #if defined(_MSC_VER)
361  typedef __int64 Int;
362  typedef unsigned __int64 UInt;
363 #else
364  typedef long long Int;
365  typedef unsigned long long UInt;
366 #endif
367 #endif
368 };
369 
371 template<>
372 struct type_fit_t<16>
373 {
374 #if IUTEST_HAS_INT128
375 #if defined(_MSC_VER)
376  typedef __int128 Int;
377  typedef unsigned __int128 UInt;
378 #else
379  typedef __int128_t Int;
380  typedef __uint128_t UInt;
381 #endif
382 #endif
383 };
384 
388 template<size_t SIZE>
389 struct type_least_t {};
390 
392 template<>
393 struct type_least_t<1>
394 {
395 #if defined(INT_LEAST8_MIN)
396  typedef int_least8_t Int;
397  typedef uint_least8_t UInt;
398 #else
399  typedef char Int;
400  typedef unsigned char UInt;
401 #endif
402 };
403 
405 template<>
406 struct type_least_t<2>
407 {
408 #if defined(INT_LEAST16_MIN)
409  typedef int_least16_t Int;
410  typedef uint_least16_t UInt;
411 #else
412  typedef short Int;
413  typedef unsigned short UInt;
414 #endif
415 };
416 
418 template<>
419 struct type_least_t<4>
420 {
421 #if defined(INT_LEAST32_MIN)
422  typedef int_least32_t Int;
423  typedef uint_least32_t UInt;
424 #else
425  typedef int Int;
426  typedef unsigned int UInt;
427 #endif
428 };
429 
431 template<>
432 struct type_least_t<8>
433 {
434 #if defined(INT_LEAST64_MIN)
435  typedef int_least64_t Int;
436  typedef uint_least64_t UInt;
437 #else
438 #if defined(_MSC_VER)
439  typedef __int64 Int;
440  typedef unsigned __int64 UInt;
441 #else
442  typedef long long Int;
443  typedef unsigned long long UInt;
444 #endif
445 #endif
446 };
447 
449 template<>
450 struct type_least_t<16> : public type_fit_t<16>
451 {
452 };
453 
454 template<bool B, typename T, typename U>
455 class conditional
456 {
457  template<bool X, typename TMP>
458  struct impl { typedef T type; };
459  template<typename TMP>
460  struct impl<false, TMP> { typedef U type; };
461 public:
462  typedef typename impl<B, void>::type type;
463 };
464 
465 template<size_t SIZE>
466 struct type_fit_t_select
467 {
468  typedef typename conditional<(SIZE & (SIZE - 1)) == 0, type_fit_t<SIZE>
469  , typename conditional<(SIZE > 8), type_fit_t<16>
470  , typename conditional<(SIZE > 4), type_fit_t<8>
471  , typename conditional<(SIZE > 2), type_fit_t<4>
472  , typename conditional<(SIZE > 1), type_fit_t<2>
473  , type_fit_t<1>
474  >::type
475  >::type
476  >::type
477  >::type
478  >::type type;
479 };
480 
481 template<size_t SIZE>
482 struct type_least_t_select
483 {
484  typedef typename conditional<(SIZE & (SIZE - 1)) == 0, type_least_t<SIZE>
485  , typename conditional<(SIZE > 8), type_least_t<16>
486  , typename conditional<(SIZE > 4), type_least_t<8>
487  , typename conditional<(SIZE > 2), type_least_t<4>
488  , typename conditional<(SIZE > 1), type_least_t<2>
489  , type_least_t<1>
490  >::type
491  >::type
492  >::type
493  >::type
494  >::type type;
495 };
496 
497 } // end of namespace type_t_helper
498 
502 template<size_t SIZE>
503 struct type_fit_t : public type_t_helper::type_fit_t_select<SIZE>::type {};
504 
508 template<size_t SIZE>
509 struct type_least_t : public type_t_helper::type_least_t_select<SIZE>::type {};
510 
511 //======================================================================
512 // function
517 inline int iu_mbtowc(wchar_t* dst, const char* src, size_t size)
518 {
519 #if defined(IUTEST_OS_LINUX_ANDROID) || defined(IUTEST_OS_WINDOWS_MOBILE)
520  // unimplimented
521  IUTEST_UNUSED_VAR(dst);
522  IUTEST_UNUSED_VAR(src);
523  IUTEST_UNUSED_VAR(size);
524  return 0;
525 #else
526  return mbtowc(dst, src, size);
527 #endif
528 }
529 
530 template<typename T>
531 T numeric_min()
532 {
533  return (::std::numeric_limits<T>::min)();
534 }
535 
536 template<typename T>
537 T numeric_max()
538 {
539  return (::std::numeric_limits<T>::max)();
540 }
541 
542 } // end of namespace detail
543 } // end of namespace iutest
544 
545 #endif // INCG_IRIS_IUTEST_STDLIB_HPP_54D4BEEE_7B6B_4AF4_B1F6_138560480D55_
any_cast の失敗例外
Definition: iutest_stdlib.hpp:212
Definition: iutest_stdlib.hpp:215
Definition: iutest_stdlib.hpp:219
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
stdlib detect defines
iutest root namespace
Definition: iutest_charcode.hpp:33
Definition: iutest_stdlib.hpp:114
Definition: iutest_stdlib.hpp:101