iutest  1.17.99.14
iutest_internal_defs.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_INTERNAL_DEFS_HPP_4B0AF5C2_8E8D_43EF_BFC5_F385E68F18DB_
16 #define INCG_IRIS_IUTEST_INTERNAL_DEFS_HPP_4B0AF5C2_8E8D_43EF_BFC5_F385E68F18DB_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "../iutest_defs.hpp"
22 #include "iutest_string_stream.hpp"
23 #include "iutest_string_view.hpp"
24 #include "iutest_tchar.hpp"
25 #include "iutest_type_traits.hpp"
27 #include "iutest_exception.hpp"
28 // IWYU pragma: end_exports
29 
30 #if IUTEST_HAS_HDR_CXXABI
31 # include <cxxabi.h>
32 #endif
33 
34 //======================================================================
35 // define
36 
40 #ifdef __INTEL_COMPILER
41 # define IUTEST_AMBIGUOUS_ELSE_BLOCKER_
42 #else
43 # define IUTEST_AMBIGUOUS_ELSE_BLOCKER_ switch(::iutest::detail::AlwaysZero()) case 0: default:
44 #endif
45 
46 #define IUTEST_SUPPRESS_UNREACHABLE_CODE_WARNING(statement) if( ::iutest::detail::AlwaysTrue() ) statement
47 
48 // console
49 #define IUTEST_MBS_CODE_UNKNOWN 0
50 #define IUTEST_MBS_CODE_UTF8 1
51 #define IUTEST_MBS_CODE_WINDOWS31J 2
52 #ifndef IUTEST_MBS_CODE
53 # if defined(IUTEST_OS_WINDOWS)
54 # define IUTEST_MBS_CODE IUTEST_MBS_CODE_WINDOWS31J
55 # else
56 # define IUTEST_MBS_CODE IUTEST_MBS_CODE_UTF8
57 # endif
58 #endif
59 
60 
61 #ifndef NULL
62 # ifdef __cplusplus
63 # define NULL 0
64 # else
65 # define NULL (void*)0
66 # endif
67 #endif
68 
69 #ifndef IUTEST_BREAK
70 # if defined(IUTEST_OS_WINDOWS_MINGW)
71 # define IUTEST_BREAK() DebugBreak()
72 # elif defined(_MSC_VER)
73 # if _MSC_VER >= 1310
74 # define IUTEST_BREAK() __debugbreak()
75 # else
76 # define IUTEST_BREAK() DebugBreak()
77 # endif
78 # elif defined(IUTEST_OS_MAC)
79 // https://www.cocoawithlove.com/2008/03/break-into-debugger.html
80 # if defined(__ppc64__) || defined(__ppc__)
81 # define IUTEST_BREAK() __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" : : : "memory", "r0", "r3", "r4" )
82 # else
83 # define IUTEST_BREAK() __asm__("int $3\n" : : )
84 # endif
85 # elif defined(__GUNC__) && (defined (__i386__) || defined (__x86_64__))
86 # define IUTEST_BREAK() do { __asm { int 3 } } while(::iutest::detail::AlwaysFalse())
87 # elif defined(__clang__) || defined(__GNUC__)
88 # define IUTEST_BREAK() __builtin_trap()
89 # elif defined(__ARMCC_VERSION)
90 # define IUTEST_BREAK() do { __breakpoint(0xF02C); } while(::iutest::detail::AlwaysFalse())
91 # else
92 # define IUTEST_BREAK() *static_cast<volatile int*>(NULL) = 1
93 //# define IUTEST_BREAK() (void)0
94 # endif
95 #endif
96 
97 #if IUTEST_HAS_IF_EXISTS
98 # define IUTEST_IF_EXISTS(identifier_, statement_) __if_exists(identifier_) { statement_ }
99 # define IUTEST_IF_NOT_EXISTS(identifier_, statement_) __if_not_exists(identifier_) { statement_ }
100 # define IUTEST_STATIC_EXISTS(identifier_) IUTEST_IF_EXISTS(identifier_, true) IUTEST_IF_NOT_EXISTS(identifier_, false)
101 #else
102 # define IUTEST_IF_EXISTS(identifier_, statement_)
103 # define IUTEST_IF_NOT_EXISTS(identifier_, statement_)
104 # define IUTEST_STATIC_EXISTS(identifier_)
105 #endif
106 
107 #if IUTEST_HAS_LIB && IUTEST_HAS_EXTERN_TEMPLATE
108 
109 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_BEGIN()
110 
111 extern template class ::std::vector< ::std::basic_string<char> >;
112 extern template class ::std::vector< ::std::basic_string<wchar_t> >;
113 
114 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_END()
115 
116 #endif
117 
118 namespace iutest
119 {
120 
121 namespace type_traits = iutest_type_traits;
122 
123 namespace detail
124 {
125 
126 //======================================================================
127 // types
128 #if IUTEST_HAS_NULLPTR && !defined(IUTEST_NO_NULL_TO_NULLPTR_T)
129 typedef ::std::nullptr_t iu_nullptr_convertible_t;
130 #else
131 typedef ::iutest_compatible::IsNullLiteralHelper::Object* iu_nullptr_convertible_t;
132 #endif
133 
134 //======================================================================
135 // function
136 
140 inline bool AlwaysTrue() { return true; }
141 
145 inline bool AlwaysFalse() { return !AlwaysTrue(); }
146 
150 inline int AlwaysZero() { return 0; }
151 
155 inline bool IsTrue(bool b) { return b; }
156 
157 //======================================================================
158 // class
159 
160 // detail から使えるようにする
161 using namespace iutest_type_traits; // NOLINT
162 using namespace iutest_compatible; // NOLINT
163 
164 #if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
165 
169 template<typename Ite>
170 struct IteratorTraits
171 {
172  typedef typename Ite::value_type type;
173 };
174 template<typename T>
175 struct IteratorTraits<T*>
176 {
177  typedef T type;
178 };
179 template<typename T>
180 struct IteratorTraits<const T*>
181 {
182  typedef T type;
183 };
184 
185 #endif
186 
190 template<typename T>
191 inline void Delete(T* ptr)
192 {
193  delete ptr;
194 }
195 
200 class None {};
201 template<typename T>
202 class NoneT1 {};
203 
208 template<typename T>
209 struct explicit_type_t {};
210 
211 #if defined(IUTEST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
212 # define IUTEST_EXPLICIT_TEMPLATE_TYPE_(t) ::iutest::detail::explicit_type_t<t>*
213 # define IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(t) , ::iutest::detail::explicit_type_t<t>*
214 #else
215 # define IUTEST_EXPLICIT_TEMPLATE_TYPE_(t)
216 # define IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(t)
217 #endif
218 
219 template<typename T>
220 inline explicit_type_t<T>* explicit_type() { return NULL; }
221 
225 template<typename T>
226 class TypeUniqueCounter
227 {
228  static size_t value;
229 public:
230  static size_t count() { return value++; }
231 };
232 template<typename T>size_t TypeUniqueCounter<T>::value = 0;
233 
234 template<typename T>
235 inline size_t GetTypeUniqueCounter() { return TypeUniqueCounter<T>::count(); }
236 
241 template<typename T>
242 class auto_ptr
243 {
244  mutable T* m_ptr;
245 public:
246  explicit auto_ptr(T* p = NULL) : m_ptr(p) {}
247  auto_ptr(const auto_ptr& rhs) : m_ptr(rhs.m_ptr) { rhs.m_ptr = NULL; }
248  ~auto_ptr() { if( m_ptr != NULL ) delete m_ptr; }
249 
250  T& operator * () const { return *m_ptr; }
251  T* operator -> () const { return m_ptr; }
252 
253  auto_ptr& operator = (auto_ptr& rhs) { m_ptr = rhs.m_ptr; rhs.m_ptr = NULL; return *this; }
254 
255  T* get() { return m_ptr; }
256 };
257 
262 template<typename T>
263 class scoped_ptr
264 {
265  T* m_ptr;
266 public:
267  explicit scoped_ptr(T* p=NULL) : m_ptr(p) {}
268  ~scoped_ptr() { reset(); }
269 
270  T& operator * () const { return *m_ptr; }
271  T* operator -> () const { return m_ptr; }
272 
273  T* get() const { return m_ptr; }
274  T* release()
275  {
276  T* const p = m_ptr;
277  m_ptr = NULL;
278  return p;
279  }
280 
281  void reset(T* p=NULL)
282  {
283  if( m_ptr != p )
284  {
285  if( IsTrue(sizeof(T) > 0) )
286  {
287  delete m_ptr;
288  }
289  m_ptr = p;
290  }
291  }
292 private:
294 };
295 
300 #define IUTEST_IS_NULLLITERAL(x) \
301  (sizeof(::iutest::detail::IsNullLiteralHelper::IsNullLiteral(x)) == 1)
302 
307 struct IsContainerHelper
308 {
309  typedef int yes_t;
310  typedef char no_t;
311 
312  template<typename T>
313  static IUTEST_CXX_CONSTEXPR yes_t IsContainer(int IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(T)
314  , typename T::iterator* =NULL, typename T::const_iterator* =NULL) { return 0; }
315 
316  template<typename T>
317  static IUTEST_CXX_CONSTEXPR no_t IsContainer(long IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(T)) { return 0; }
318 };
319 
323 template<typename T>
324 inline ::std::string GetTypeName()
325 {
326 #if IUTEST_HAS_RTTI
327  const char* const name = typeid(T).name();
328 
329 #if IUTEST_HAS_HDR_CXXABI
330  using abi::__cxa_demangle;
331  int status=1;
332  char* const read_name = __cxa_demangle(name, 0, 0, &status);
333  ::std::string str(status == 0 ? read_name : name);
334 #if defined(_IUTEST_DEBUG)
335  if( status != 0 ) {
336  fprintf(stderr, "Unable to demangle \"%s\" -> \"%s\". (status=%d)\n", name, read_name ? read_name : "", status);
337  fflush(stderr);
338  }
339 #endif
340  free(read_name);
341  return str;
342 #else
343  return name;
344 #endif
345 
346 #else
347  return "<type>";
348 #endif
349 }
350 
351 #if !IUTEST_HAS_RTTI
352 
353 #define IIUT_GeTypeNameSpecialization(type) \
354  template<>inline ::std::string GetTypeName<type>() { return #type; } \
355  template<>inline ::std::string GetTypeName<type*>() { return #type "*"; }
356 
357 #define IIUT_GeTypeNameSpecialization2(type) \
358  IIUT_GeTypeNameSpecialization(type) \
359  IIUT_GeTypeNameSpecialization(unsigned type)
360 
361 IIUT_GeTypeNameSpecialization2(char) // NOLINT
362 IIUT_GeTypeNameSpecialization2(short) // NOLINT
363 IIUT_GeTypeNameSpecialization2(int) // NOLINT
364 IIUT_GeTypeNameSpecialization2(long) // NOLINT
365 IIUT_GeTypeNameSpecialization(float) // NOLINT
366 IIUT_GeTypeNameSpecialization(double) // NOLINT
367 IIUT_GeTypeNameSpecialization(bool) // NOLINT
368 
369 #undef IIUT_GeTypeNameSpecialization
370 #undef IIUT_GeTypeNameSpecialization2
371 
372 #endif
373 
377 template<typename T>
378 struct GetTypeNameProxy
379 {
380  static ::std::string GetTypeName() { return detail::GetTypeName<T>(); }
381 };
382 
383 } // end of namespace detail
384 } // end of namespace iutest
385 
386 
387 #endif // INCG_IRIS_IUTEST_INTERNAL_DEFS_HPP_4B0AF5C2_8E8D_43EF_BFC5_F385E68F18DB_
internal gtest/iutest compatible definition
#define IUTEST_CXX_CONSTEXPR
constexpr
Definition: iutest_compiler.hpp:372
iris unit test exception
#define IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(TypeName)
コピー禁止定義
Definition: iutest_pp.hpp:31
iris unit test iu_basic_ostream implimentaion
iris unit test char/wchar_t mediator
iutest root namespace
Definition: iutest_charcode.hpp:33