YSTest: ydef.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

ydef.h
浏览该文件的文档.
1 /*
2 © 2009-2017 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
36 #ifndef YB_INC_ydef_h_
37 #define YB_INC_ydef_h_ 1
38
44
77 #if defined(__GNUC__)
78 # undef YB_IMPL_GNUC
79 # define YB_IMPL_GNUC \
80 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
81 #endif
82 #ifdef __cplusplus
83 # ifdef _MSC_VER
84 # undef YB_IMPL_MSCPP
85 # define YB_IMPL_MSCPP _MSC_VER
86 # elif defined(__clang__)
87 # undef YB_IMPL_CLANGPP
88 # if defined(__apple_build_version__)
89 # define YB_IMPL_CLANGPP (40300 + __clang_patchlevel__)
90 # else
91 # define YB_IMPL_CLANGPP (__clang__ * 10000 + __clang_minor__ * 100 \
92 + __clang_patchlevel__)
93 # endif
94 # elif defined(__GNUG__)
95 # undef YB_IMPL_GNUCPP
96 # define YB_IMPL_GNUCPP \
97 (__GNUG__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
98 # else
99 // TODO: Deferred. Complete version checking for compiler and library
100 // implementations, e.g. EDG frontends.
101 //#ifdef __GNUC__
102 //# include <tr1/type_traits>
103 # error "This language implementation is not supported."
104 # endif
105 #else
106 # error "This header is only for C++."
107 #endif
108
109 #if YB_IMPL_MSCPP >= 1400
110 # define _CRT_SECURE_NO_WARNINGS
112 #endif
113
114
120 #ifndef __has_attribute
122 # define __has_attribute(...) 0
123 #endif
124
126 #ifndef __has_builtin
127 # define __has_builtin(...) 0
128 #endif
129
131 #ifndef __has_cpp_attribute
132 # define __has_cpp_attribute(...) 0
133 #endif
134
135 #ifndef __has_feature
136 # define __has_feature(...) 0
137 #endif
138
139 #ifndef __has_extension
140 # define __has_extension(...) 0
141 #endif
142
143
149
151 #ifndef __cpp_constexpr
152 # if __has_feature(cxx_relaxed_constexpr) || __cplusplus >= 201402L
153 # define __cpp_constexpr 201304
154 # elif __has_feature(cxx_constexpr) || __cplusplus >= 201103L \
155 || YB_IMPL_GNUCPP >= 40600 || YB_IMPL_MSCPP >= 1900
156 # define __cpp_constexpr 200704
157 # endif
158 #endif
159
160
161 #include <cstddef> // for std::nullptr_t, std::size_t, std::ptrdiff_t, offsetof;
162 #include <cstdlib> // for std::abort;
163 #include <climits> // for CHAR_BIT;
164 #include <cassert> // for assert;
165 #include <cstdint>
166 #include <cwchar> // for std::wint_t;
167 #include <utility> // for std::forward;
168 #include <type_traits> // for std::is_class, std::is_standard_layout;
169
170 #if YB_IMPL_MSCPP >= 1900
171
173
179 # ifndef __cpp_lib_bool_constant
180 # define __cpp_lib_bool_constant 201505
181 # endif
182 # ifndef __cpp_lib_void_t
183 # define __cpp_lib_void_t 201411
184 # endif
185
186
192 # ifndef __cpp_lib_integer_sequence
193 # define __cpp_lib_integer_sequence 201304
194 # endif
195 # ifndef __cpp_lib_exchange_function
196 # define __cpp_lib_exchange_function 201304
197 # endif
198 # ifndef __cpp_lib_tuple_element_t
199 # define __cpp_lib_tuple_element_t 201402
200 # endif
201
202
203 #endif
204
205
216 #define YPP_Args(...) __VA_ARGS__
217
219 #define YPP_Empty
220
225 #define YPP_Comma ,
226
231 #define YPP_Concat(_x, _y) _x ## _y
232
242 #define YPP_Join(_x, _y) YPP_Concat(_x, _y)
243
248 #define YPP_Stringify(_x) #_x
249
250
251
262 #define yimpl(...) __VA_ARGS__
263
264
270
276 #undef YB_HAS_ALIGNAS
277 #define YB_HAS_ALIGNAS \
278 (__has_feature(cxx_alignas) || __has_extension(cxx_alignas) \
279 || YB_IMPL_GNUCPP >= 40800)
280
286 #undef YB_HAS_ALIGNOF
287 #define YB_HAS_ALIGNOF (__cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40500)
288
294 #undef YB_HAS_BUILTIN_NULLPTR
295 #define YB_HAS_BUILTIN_NULLPTR \
296 (__has_feature(cxx_nullptr) || __has_extension(cxx_nullptr) \
297 || __cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40600 || \
298 YB_IMPL_MSCPP >= 1600)
299
305 #undef YB_HAS_NOEXCEPT
306 #define YB_HAS_NOEXCEPT \
307 (__has_feature(cxx_noexcept) || __has_extension(cxx_noexcept) || \
308 __cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40600 \
309 || YB_IMPL_MSCPP >= 1900)
310
317 #undef YB_HAS_THREAD_LOCAL
318 #define YB_HAS_THREAD_LOCAL \
319 (__has_feature(cxx_thread_local) || (__cplusplus >= 201103L \
320 && !YB_IMPL_GNUCPP) || (YB_IMPL_GNUCPP >= 40800 && _GLIBCXX_HAVE_TLS))
321
322
323
330 #if __has_builtin(__builtin_trap) || YB_IMPL_GNUCPP >= 40203
331 # define YB_ABORT __builtin_trap()
332 #else
333 # define YB_ABORT std::abort()
334 #endif
335
336
350 #if YB_IMPL_GNUC >= 20500
351 # define YB_ATTR(...) __attribute__((__VA_ARGS__))
352 #else
353 # define YB_ATTR(...)
354 #endif
355
363 #if __cpp_attributes >= 200809 || __has_feature(cxx_attributes)
364 # define YB_ATTR_STD(...) [[__VA_ARGS__]]
365 #else
366 # define YB_ATTR_STD(...)
367 #endif
368
374 #if __has_cpp_attribute(fallthrough)
375 # define YB_ATTR_fallthrough YB_ATTR_STD(fallthrough)
376 #elif __has_cpp_attribute(clang::fallthrough)
377 # define YB_ATTR_fallthrough YB_ATTR_STD(clang::fallthrough)
378 #elif __has_cpp_attribute(gnu::fallthrough)
379 # define YB_ATTR_fallthrough YB_ATTR_STD(gnu::fallthrough)
380 #elif __has_attribute(fallthrough)
381 # define YB_ATTR_fallthrough YB_ATTR(fallthrough)
382 #else
383 # define YB_ATTR_fallthrough
384 #endif
385
392 #if YB_IMPL_GNUC >= 40400 && !YB_IMPL_CLANGPP
393 # define YB_ATTR_gnu_printf(...) \
394 YB_ATTR(__format__ (__gnu_printf__, __VA_ARGS__))
395 #elif YB_IMPL_GNUC >= 20604 && !YB_IMPL_CLANGPP
396 # define YB_ATTR_gnu_printf(...) YB_ATTR(__format__ (__printf__, __VA_ARGS__))
397 #else
398 # define YB_ATTR_gnu_printf(...)
399 #endif
400
409 #if __has_attribute(returns_nonnull) || YB_IMPL_GNUC >= 40900 \
410 || YB_IMPL_CLANGPP >= 30500
411 # define YB_ATTR_returns_nonnull YB_ATTR(returns_nonnull)
412 #else
413 # define YB_ATTR_returns_nonnull
414 #endif
415
427 #if YB_IMPL_MSCPP >= 1900 && !defined(__EDG__) && !defined _CORECRT_BUILD
428 # define YB_ALLOCATOR __declspec(allocator)
429 #elif __has_attribute(__malloc__) || YB_IMPL_GNUCPP >= 20296
430 # define YB_ALLOCATOR YB_ATTR(__malloc__)
431 #else
432 # define YB_ALLOCATOR
433 #endif
434
442 #if YB_IMPL_MSCPP >= 1200
443 # define YB_ASSUME(_expr) __assume(_expr)
444 #elif __has_builtin(__builtin_unreachable) || YB_IMPL_GNUCPP >= 40500
445 # define YB_ASSUME(_expr) ((_expr) ? void(0) : __builtin_unreachable())
446 #else
447 # define YB_ASSUME(_expr) ((_expr) ? void(0) : YB_ABORT)
448 #endif
449
457 #if __has_builtin(__builtin_expect) || YB_IMPL_GNUCPP >= 29600
458 # define YB_EXPECT(_expr, _constant) (__builtin_expect(_expr, _constant))
459 # define YB_LIKELY(_expr) (__builtin_expect(bool(_expr), 1))
460 # define YB_UNLIKELY(_expr) (__builtin_expect(bool(_expr), 0))
461 #else
462 # define YB_EXPECT(_expr, _constant) (_expr)
463 # define YB_LIKELY(_expr) (_expr)
464 # define YB_UNLIKELY(_expr) (_expr)
465 #endif
466
473 #if (__has_attribute(__flatten__) || YB_IMPL_GNUCPP >= 40102) \
474 && YB_IMPL_GNUCPP != 40600
475 # define YB_FLATTEN YB_ATTR(__flatten__)
476 #else
477 # define YB_FLATTEN
478 #endif
479
486 #if YB_IMPL_GNUCPP >= 30300
487 # define YB_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
488 #else
489 # define YB_NONNULL(...)
490 #endif
491
499 #if __has_cpp_attribute(noreturn) >= 200809 || YB_IMPL_GNUCPP >= 40800
500 # define YB_NORETURN [[noreturn]]
501 #elif __has_attribute(__noreturn__) || YB_IMPL_GNUCPP >= 20296
502 # define YB_NORETURN YB_ATTR(__noreturn__)
503 #else
504 # define YB_NORETURN
505 #endif
506
524 #if __has_attribute(__pure__) || YB_IMPL_GNUCPP >= 20296
525 # define YB_PURE YB_ATTR(__pure__)
526 #else
527 # define YB_PURE
528 #endif
529
545 #if __has_attribute(__const__) || YB_IMPL_GNUCPP >= 20500
546 # define YB_STATELESS YB_ATTR(__const__)
547 #else
548 # define YB_STATELESS
549 #endif
550
551
552
553
575 #if defined(YB_DLL) && defined(YB_BUILD_DLL)
576 # error "DLL could not be built and used at the same time."
577 #endif
578
579 #if YB_IMPL_MSCPP \
580 || (YB_IMPL_GNUCPP && (defined(__MINGW32__) || defined(__CYGWIN__)))
581 # ifdef YB_DLL
582 # define YB_API __declspec(dllimport)
583 # elif defined(YB_BUILD_DLL)
584 # define YB_API __declspec(dllexport)
585 # else
586 # define YB_API
587 # endif
588 #elif defined(YB_BUILD_DLL) && (__has_attribute(__visibility__) \
589 || YB_IMPL_GNUCPP >= 40000 || YB_IMPL_CLANGPP)
590 # define YB_API YB_ATTR(__visibility__("default"))
591 #else
592 # define YB_API
593 #endif
594
595
601 #if false && !defined(NDEBUG)
602 # define YB_Use_DynamicExceptionSpecification 1
603 #endif
604
620 #ifndef NDEBUG
621 # ifndef YB_Use_YAssert
622 # define YB_Use_YAssert 1
623 # endif
624 #elif !defined(YB_Use_StrictNoThrow)
625 # define YB_Use_StrictNoThrow 1
626 #endif
627 #define YB_Use_YTrace 1
628
629
630
643 #if YB_HAS_ALIGNAS
644 # define yalignas alignas
645 # define yalignas_type alignas
646 #else
647 # define yalignas(_expr) YB_ATTR(__aligned__(_expr))
648 # define yalignas_type(_type) YB_ATTR(__aligned__(__alignof(_type)))
649 #endif
650
657 #if YB_HAS_ALIGNOF
658 # define yalignof alignof
659 #else
660 # define yalignof(_type) std::alignment_of<_type>::value
661 #endif
662
675 #if __cpp_constexpr >= 200704
676 # define yconstexpr constexpr
677 # define yconstfn constexpr
678 #else
679 # define yconstexpr
680 # define yconstfn inline
681 #endif
682
689 #if __cpp_constexpr >= 201304
690 # define yconstfn_relaxed constexpr
691 #else
692 # define yconstfn_relaxed inline
693 #endif
694
701 #if YB_IMPL_MSCPP || __INTEL_COMPILER >= 600 || __IBMCPP__ >= 500
702 # define yfname __FUNCTION__
703 #elif __BORLANDC__ >= 0x550
704 # define yfname __FUNC__
705 #elif __cplusplus >= 201103 || __STDC_VERSION__ >= 199901
706 # define yfname __func__
707 #else
708 # define yfname "<unknown-fn>"
709 #endif
710
717 #if YB_IMPL_GNUCPP || YB_IMPL_CLANGPP
718 # define yfsig __PRETTY_FUNCTION__
719 #elif defined(__FUNCSIG__)
720 # define yfsig __FUNCSIG__
721 #elif __INTEL_COMPILER >= 600 || __IBMCPP__ >= 500
722 # define yfsig __FUNCTION__
723 #elif __BORLANDC__ >= 0x550
724 # define yfsig __FUNC__
725 #elif __cplusplus >= 201103 || __STDC_VERSION__ >= 199901
726 # define yfsig __func__
727 #else
728 # define yfsig "<unknown-fsig>"
729 #endif
730
741 #if YB_HAS_NOEXCEPT
742 # define ynoexcept noexcept
743 # define ynoexcept_assert(_msg, ...) \
744 static_assert(noexcept(__VA_ARGS__), _msg)
745 #else
746 # define ynoexcept(...)
747 # define ynoexcept_assert(_msg, ...)
748 #endif
749
761 #if YB_HAS_NOEXCEPT
762 # define ynothrow ynoexcept
763 #elif YB_IMPL_GNUCPP >= 30300
764 # define ynothrow __attribute__((nothrow))
765 #else
766 # define ynothrow ythrow()
767 #endif
768
783 #if YB_Use_StrictNoThrow
784 # define ynothrowv ynothrow
785 #else
786 # define ynothrowv
787 #endif
788
794 #define ynoexcept_spec(...) ynoexcept(noexcept(__VA_ARGS__))
795
803 #if YB_HAS_THREAD_LOCAL && defined(_MT)
804 # define ythread thread_local
805 #else
806 # define ythread static
807 #endif
808
814 #if YB_Use_DynamicExceptionSpecification
815 # define ythrow throw
816 #else
817 # define ythrow(...)
818 #endif
819
820
821
822 namespace ystdex
823 {
824
834 using byte = unsigned char;
835
836 #if CHAR_BIT == 8
837
842 using octet = byte;
843 # else
844 using octet = void;
845 #endif
846
847 using std::ptrdiff_t;
848 using std::size_t;
849 using std::wint_t;
850
851 #if YB_HAS_BUILTIN_NULLPTR
852
853 using std::nullptr_t;
854
855 #else
856
861 const class nullptr_t
862 {
863 public:
865 template<typename _type>
866 inline
867 operator _type*() const
868 {
869 return 0;
870 }
871
873 template<class _tClass, typename _type>
874 inline
875 operator _type _tClass::*() const
876 {
877 return 0;
878 }
880 template<typename _type>
881 bool
882 equals(const _type& rhs) const
883 {
884 return rhs == 0;
885 }
886
888 void operator&() const = delete;
889 } nullptr = {};
890
892
893 template<typename _type>
894 inline bool
895 operator==(nullptr_t lhs, const _type& rhs)
896 {
897 return lhs.equals(rhs);
898 }
899 template<typename _type>
900 inline bool
901 operator==(const _type& lhs, nullptr_t rhs)
902 {
903 return rhs.equals(lhs);
904 }
905
906 template<typename _type>
907 inline bool
908 operator!=(nullptr_t lhs, const _type& rhs)
909 {
910 return !lhs.equals(rhs);
911 }
912 template<typename _type>
913 inline bool
914 operator!=(const _type& lhs, nullptr_t rhs)
915 {
916 return !rhs.equals(lhs);
917 }
919
920 #endif
921
922
927 template<typename...>
928 struct empty_base
929 {};
930
935 struct raw_tag
936 {};
937
938
951 template<bool _bMemObjPtr, bool _bNoExcept, class _type>
952 class offsetof_check
953 {
954 static_assert(std::is_class<_type>::value, "Non class type found.");
955 static_assert(std::is_standard_layout<_type>::value,
956 "Non standard layout type found.");
957 static_assert(_bMemObjPtr, "Non-static member object violation found.");
958 static_assert(_bNoExcept, "Exception guarantee violation found.");
959 };
960
961
968 #define yunused(...) static_cast<void>(__VA_ARGS__)
969
978 #define yoffsetof(_type, _member) \
979 (decltype(sizeof(ystdex::offsetof_check<std::is_member_object_pointer< \
980 decltype(&_type::_member)>::value, ynoexcept(offsetof(_type, _member)), \
981 _type>))(offsetof(_type, _member)))
982
995 #define yforward(_expr) std::forward<decltype(_expr)>(_expr)
996
1002 template<typename _type, typename... _tParams>
1003 yconstfn auto
1004 unsequenced(_type&& arg, _tParams&&...) ynothrow -> decltype(yforward(arg))
1005 {
1006 return yforward(arg);
1007 }
1008
1022 #define yunseq ystdex::unsequenced
1023
1024 } // namespace ystdex;
1025
1026 #endif
1027
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
ystdex::raw_tag
直接构造类型（直接构造重载用）。
Definition: ydef.h:935
ystdex::unsequenced
auto unsequenced(_type &&arg, _tParams &&...) -> decltype(std::forward< decltype(arg)>(arg))
无序列依赖表达式组求值实现。
Definition: ydef.h:1004
ystdex::operator!=
bool operator!=(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:908
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::operator==
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:895
ystdex::offsetof_check
成员偏移计算静态类型检查。
Definition: ydef.h:952
ystdex::nullptr_t
空指针类。
Definition: ydef.h:861
ystdex::nullptr_t::operator&
void operator&() const =delete
禁止取 nullptr 的指针。
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
ystdex::empty_base
空基类模板。
Definition: ydef.h:928
ystdex::nullptr_t::equals
bool equals(const _type &rhs) const
支持关系运算符重载。
Definition: ydef.h:882
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::octet
void octet
Definition: ydef.h:844