iutest  1.17.1.0
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 #include "../iutest_defs.hpp"
21 #include "iutest_string_stream.hpp"
22 #include "iutest_string_view.hpp"
23 #include "iutest_type_traits.hpp"
25 #include "iutest_exception.hpp"
26 
27 #if IUTEST_HAS_HDR_CXXABI
28 # include <cxxabi.h>
29 #endif
30 
31 //======================================================================
32 // define
33 
37 #ifdef __INTEL_COMPILER
38 # define IUTEST_AMBIGUOUS_ELSE_BLOCKER_
39 #else
40 # define IUTEST_AMBIGUOUS_ELSE_BLOCKER_ switch(::iutest::detail::AlwaysZero()) case 0: default:
41 #endif
42 
43 #define IUTEST_SUPPRESS_UNREACHABLE_CODE_WARNING(statement) if( ::iutest::detail::AlwaysTrue() ) statement
44 
45 // console
46 #define IUTEST_MBS_CODE_UNKNOWN 0
47 #define IUTEST_MBS_CODE_UTF8 1
48 #define IUTEST_MBS_CODE_WINDOWS31J 2
49 #ifndef IUTEST_MBS_CODE
50 # if defined(IUTEST_OS_WINDOWS)
51 # define IUTEST_MBS_CODE IUTEST_MBS_CODE_WINDOWS31J
52 # else
53 # define IUTEST_MBS_CODE IUTEST_MBS_CODE_UTF8
54 # endif
55 #endif
56 
57 
58 #ifndef NULL
59 # ifdef __cplusplus
60 # define NULL 0
61 # else
62 # define NULL (void*)0
63 # endif
64 #endif
65 
66 #ifndef IUTEST_BREAK
67 # if defined(__MINGW32__)
68 # define IUTEST_BREAK() DebugBreak()
69 # elif defined(_MSC_VER)
70 # if _MSC_VER >= 1310
71 # define IUTEST_BREAK() __debugbreak()
72 # else
73 # define IUTEST_BREAK() DebugBreak()
74 # endif
75 # elif defined(IUTEST_OS_MAC)
76 // https://www.cocoawithlove.com/2008/03/break-into-debugger.html
77 # if defined(__ppc64__) || defined(__ppc__)
78 # define IUTEST_BREAK() __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" : : : "memory", "r0", "r3", "r4" )
79 # else
80 # define IUTEST_BREAK() __asm__("int $3\n" : : )
81 # endif
82 # elif defined(__GUNC__) && (defined (__i386__) || defined (__x86_64__))
83 # define IUTEST_BREAK() do { __asm { int 3 } } while(::iutest::detail::AlwaysFalse())
84 # elif defined(__clang__) || defined(__GNUC__)
85 # define IUTEST_BREAK() __builtin_trap()
86 # elif defined(__ARMCC_VERSION)
87 # define IUTEST_BREAK() do { __breakpoint(0xF02C); } while(::iutest::detail::AlwaysFalse())
88 # else
89 # define IUTEST_BREAK() *static_cast<volatile int*>(NULL) = 1
90 //# define IUTEST_BREAK() (void)0
91 # endif
92 #endif
93 
94 #if IUTEST_HAS_IF_EXISTS
95 # define IUTEST_IF_EXISTS(identifier_, statement_) __if_exists(identifier_) { statement_ }
96 # define IUTEST_IF_NOT_EXISTS(identifier_, statement_) __if_not_exists(identifier_) { statement_ }
97 # define IUTEST_STATIC_EXISTS(identifier_) IUTEST_IF_EXISTS(identifier_, true) IUTEST_IF_NOT_EXISTS(identifier_, false)
98 #else
99 # define IUTEST_IF_EXISTS(identifier_, statement_)
100 # define IUTEST_IF_NOT_EXISTS(identifier_, statement_)
101 # define IUTEST_STATIC_EXISTS(identifier_)
102 #endif
103 
104 #if IUTEST_HAS_LIB && IUTEST_HAS_EXTERN_TEMPLATE
105 
106 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_BEGIN()
107 
108 extern template class ::std::vector< ::std::basic_string<char> >;
109 extern template class ::std::vector< ::std::basic_string<wchar_t> >;
110 
111 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_END()
112 
113 #endif
114 
115 namespace iutest
116 {
117 
118 namespace type_traits = iutest_type_traits;
119 
120 namespace detail
121 {
122 
123 //======================================================================
124 // types
125 #if IUTEST_HAS_NULLPTR && !defined(IUTEST_NO_NULL_TO_NULLPTR_T)
126 typedef ::std::nullptr_t iu_nullptr_convertible_t;
127 #else
128 typedef ::iutest_compatible::IsNullLiteralHelper::Object* iu_nullptr_convertible_t;
129 #endif
130 
131 //======================================================================
132 // function
133 
137 inline bool AlwaysTrue() { return true; }
138 
142 inline bool AlwaysFalse() { return !AlwaysTrue(); }
143 
147 inline int AlwaysZero() { return 0; }
148 
152 inline bool IsTrue(bool b) { return b; }
153 
154 //======================================================================
155 // class
156 
157 // detail から使えるようにする
158 using namespace iutest_type_traits; // NOLINT
159 using namespace iutest_compatible; // NOLINT
160 
161 #if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
162 
166 template<typename Ite>
167 struct IteratorTraits
168 {
169  typedef typename Ite::value_type type;
170 };
171 template<typename T>
172 struct IteratorTraits<T*>
173 {
174  typedef T type;
175 };
176 template<typename T>
177 struct IteratorTraits<const T*>
178 {
179  typedef T type;
180 };
181 
182 #endif
183 
187 template<typename T>
188 inline void Delete(T* ptr)
189 {
190  delete ptr;
191 }
192 
197 class None {};
198 template<typename T>
199 class NoneT1 {};
200 
205 template<typename T>
206 struct explicit_type_t {};
207 
208 #if defined(IUTEST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
209 # define IUTEST_EXPLICIT_TEMPLATE_TYPE_(t) ::iutest::detail::explicit_type_t<t>*
210 # define IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(t) , ::iutest::detail::explicit_type_t<t>*
211 #else
212 # define IUTEST_EXPLICIT_TEMPLATE_TYPE_(t)
213 # define IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(t)
214 #endif
215 
216 template<typename T>
217 inline explicit_type_t<T>* explicit_type() { return NULL; }
218 
222 template<typename T>
223 class TypeUniqueCounter
224 {
225  static size_t value;
226 public:
227  static size_t count() { return value++; }
228 };
229 template<typename T>size_t TypeUniqueCounter<T>::value = 0;
230 
231 template<typename T>
232 inline size_t GetTypeUniqueCounter() { return TypeUniqueCounter<T>::count(); }
233 
238 template<typename T>
239 class auto_ptr
240 {
241  mutable T* m_ptr;
242 public:
243  explicit auto_ptr(T* p = NULL) : m_ptr(p) {}
244  auto_ptr(const auto_ptr& rhs) : m_ptr(rhs.m_ptr) { rhs.m_ptr = NULL; }
245  ~auto_ptr() { if( m_ptr != NULL ) delete m_ptr; }
246 
247  T& operator * () const { return *m_ptr; }
248  T* operator -> () const { return m_ptr; }
249 
250  auto_ptr& operator = (auto_ptr& rhs) { m_ptr = rhs.m_ptr; rhs.m_ptr = NULL; return *this; }
251 
252  T* get() { return m_ptr; }
253 };
254 
259 template<typename T>
260 class scoped_ptr
261 {
262  T* m_ptr;
263 public:
264  explicit scoped_ptr(T* p=NULL) : m_ptr(p) {}
265  ~scoped_ptr() { reset(); }
266 
267  T& operator * () const { return *m_ptr; }
268  T* operator -> () const { return m_ptr; }
269 
270  T* get() const { return m_ptr; }
271  T* release()
272  {
273  T* const p = m_ptr;
274  m_ptr = NULL;
275  return p;
276  }
277 
278  void reset(T* p=NULL)
279  {
280  if( m_ptr != p )
281  {
282  if( IsTrue(sizeof(T) > 0) )
283  {
284  delete m_ptr;
285  }
286  m_ptr = p;
287  }
288  }
289 private:
291 };
292 
297 #define IUTEST_IS_NULLLITERAL(x) \
298  (sizeof(::iutest::detail::IsNullLiteralHelper::IsNullLiteral(x)) == 1)
299 
304 struct IsContainerHelper
305 {
306  typedef int yes_t;
307  typedef char no_t;
308 
309  template<typename T>
310  static IUTEST_CXX_CONSTEXPR yes_t IsContainer(int IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(T)
311  , typename T::iterator* =NULL, typename T::const_iterator* =NULL) { return 0; }
312 
313  template<typename T>
314  static IUTEST_CXX_CONSTEXPR no_t IsContainer(long IUTEST_APPEND_EXPLICIT_TEMPLATE_TYPE_(T)) { return 0; }
315 };
316 
320 template<typename T>
321 inline ::std::string GetTypeName()
322 {
323 #if IUTEST_HAS_RTTI
324  const char* const name = typeid(T).name();
325 
326 #if IUTEST_HAS_HDR_CXXABI
327  using abi::__cxa_demangle;
328  int status=1;
329  char* const read_name = __cxa_demangle(name, 0, 0, &status);
330  ::std::string str(status == 0 ? read_name : name);
331  free(read_name);
332  return str;
333 #else
334  return name;
335 #endif
336 
337 #else
338  return "<type>";
339 #endif
340 }
341 
342 #if !IUTEST_HAS_RTTI
343 
344 #define IIUT_GeTypeNameSpecialization(type) \
345  template<>inline ::std::string GetTypeName<type>() { return #type; } \
346  template<>inline ::std::string GetTypeName<type*>() { return #type "*"; }
347 
348 #define IIUT_GeTypeNameSpecialization2(type) \
349  IIUT_GeTypeNameSpecialization(type) \
350  IIUT_GeTypeNameSpecialization(unsigned type)
351 
352 IIUT_GeTypeNameSpecialization2(char) // NOLINT
353 IIUT_GeTypeNameSpecialization2(short) // NOLINT
354 IIUT_GeTypeNameSpecialization2(int) // NOLINT
355 IIUT_GeTypeNameSpecialization2(long) // NOLINT
356 IIUT_GeTypeNameSpecialization(float) // NOLINT
357 IIUT_GeTypeNameSpecialization(double) // NOLINT
358 IIUT_GeTypeNameSpecialization(bool) // NOLINT
359 
360 #undef IIUT_GeTypeNameSpecialization
361 #undef IIUT_GeTypeNameSpecialization2
362 
363 #endif
364 
368 template<typename T>
369 struct GetTypeNameProxy
370 {
371  static ::std::string GetTypeName() { return detail::GetTypeName<T>(); }
372 };
373 
374 } // end of namespace detail
375 } // end of namespace iutest
376 
377 
378 #endif // INCG_IRIS_IUTEST_INTERNAL_DEFS_HPP_4B0AF5C2_8E8D_43EF_BFC5_F385E68F18DB_
iutest_config.hpp
iris unit test config
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
iutest_string_stream.hpp
iris unit test iu_basic_ostream implimentaion
IUTEST_CXX_CONSTEXPR
#define IUTEST_CXX_CONSTEXPR
constexpr
Definition: iutest_compiler.hpp:298
iutest_type_traits.hpp
type traits
IUTEST_PP_DISALLOW_COPY_AND_ASSIGN
#define IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(TypeName)
コピー禁止定義
Definition: iutest_pp.hpp:25
iutest_exception.hpp
iris unit test exception
iutest_compatible_defs.hpp
internal gtest/iutest compatible definition
iutest_string_view.hpp
string_view