YSTest: ydef.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

ydef.h
浏览该文件的文档.
1 /*
2 © 2009-2016 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
244
245
256 #define yimpl(...) __VA_ARGS__
257
258
264
270 #undef YB_HAS_ALIGNAS
271 #define YB_HAS_ALIGNAS \
272 (__has_feature(cxx_alignas) || __has_extension(cxx_alignas) \
273 || YB_IMPL_GNUCPP >= 40800)
274
280 #undef YB_HAS_ALIGNOF
281 #define YB_HAS_ALIGNOF (__cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40500)
282
288 #undef YB_HAS_BUILTIN_NULLPTR
289 #define YB_HAS_BUILTIN_NULLPTR \
290 (__has_feature(cxx_nullptr) || __has_extension(cxx_nullptr) \
291 || __cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40600 || \
292 YB_IMPL_MSCPP >= 1600)
293
299 #undef YB_HAS_NOEXCEPT
300 #define YB_HAS_NOEXCEPT \
301 (__has_feature(cxx_noexcept) || __has_extension(cxx_noexcept) || \
302 __cplusplus >= 201103L || YB_IMPL_GNUCPP >= 40600 \
303 || YB_IMPL_MSCPP >= 1900)
304
311 #undef YB_HAS_THREAD_LOCAL
312 #define YB_HAS_THREAD_LOCAL \
313 (__has_feature(cxx_thread_local) || (__cplusplus >= 201103L \
314 && !YB_IMPL_GNUCPP) || (YB_IMPL_GNUCPP >= 40800 && _GLIBCXX_HAVE_TLS))
315
316
317
324 #if __has_builtin(__builtin_trap) || YB_IMPL_GNUCPP >= 40203
325 # define YB_ABORT __builtin_trap()
326 #else
327 # define YB_ABORT std::abort()
328 #endif
329
330
344 #if YB_IMPL_GNUC >= 20500
345 # define YB_ATTR(...) __attribute__((__VA_ARGS__))
346 #else
347 # define YB_ATTR(...)
348 #endif
349
357 #if __cpp_attributes >= 200809 || __has_feature(cxx_attributes)
358 # define YB_ATTR_STD(...) [[__VA_ARGS__]]
359 #else
360 # define YB_ATTR_STD(...)
361 #endif
362
369 #if YB_IMPL_GNUC >= 40400 && !YB_IMPL_CLANGPP
370 # define YB_ATTR_gnu_printf(...) \
371 YB_ATTR(__format__ (__gnu_printf__, __VA_ARGS__))
372 #elif YB_IMPL_GNUC >= 20604 && !YB_IMPL_CLANGPP
373 # define YB_ATTR_gnu_printf(...) YB_ATTR(__format__ (__printf__, __VA_ARGS__))
374 #else
375 # define YB_ATTR_gnu_printf(...)
376 #endif
377
386 #if __has_attribute(returns_nonnull) || YB_IMPL_GNUC >= 40900 \
387 || YB_IMPL_CLANGPP >= 30500
388 # define YB_ATTR_returns_nonnull YB_ATTR(returns_nonnull)
389 #else
390 # define YB_ATTR_returns_nonnull
391 #endif
392
403 #if __has_attribute(__malloc__) || YB_IMPL_GNUCPP >= 20296
404 # define YB_ALLOCATOR YB_ATTR(__malloc__)
405 #else
406 # define YB_ALLOCATOR
407 #endif
408
416 #if YB_IMPL_MSCPP >= 1200
417 # define YB_ASSUME(_expr) __assume(_expr)
418 #elif __has_builtin(__builtin_unreachable) || YB_IMPL_GNUCPP >= 40500
419 # define YB_ASSUME(_expr) ((_expr) ? void(0) : __builtin_unreachable())
420 #else
421 # define YB_ASSUME(_expr) ((_expr) ? void(0) : YB_ABORT)
422 #endif
423
431 #if __has_builtin(__builtin_expect) || YB_IMPL_GNUCPP >= 29600
432 # define YB_EXPECT(_expr, _constant) (__builtin_expect(_expr, _constant))
433 # define YB_LIKELY(_expr) (__builtin_expect(bool(_expr), 1))
434 # define YB_UNLIKELY(_expr) (__builtin_expect(bool(_expr), 0))
435 #else
436 # define YB_EXPECT(_expr, _constant) (_expr)
437 # define YB_LIKELY(_expr) (_expr)
438 # define YB_UNLIKELY(_expr) (_expr)
439 #endif
440
447 #if (__has_attribute(__flatten__) || YB_IMPL_GNUCPP >= 40102) \
448 && YB_IMPL_GNUCPP != 40600
449 # define YB_FLATTEN YB_ATTR(__flatten__)
450 #else
451 # define YB_FLATTEN
452 #endif
453
460 #if YB_IMPL_GNUCPP >= 30300
461 # define YB_NONNULL(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))
462 #else
463 # define YB_NONNULL(...)
464 #endif
465
473 #if __has_cpp_attribute(noreturn) >= 200809 || YB_IMPL_GNUCPP >= 40800
474 # define YB_NORETURN [[noreturn]]
475 #elif __has_attribute(__noreturn__) || YB_IMPL_GNUCPP >= 20296
476 # define YB_NORETURN YB_ATTR(__noreturn__)
477 #else
478 # define YB_NORETURN
479 #endif
480
498 #if __has_attribute(__pure__) || YB_IMPL_GNUCPP >= 20296
499 # define YB_PURE YB_ATTR(__pure__)
500 #else
501 # define YB_PURE
502 #endif
503
519 #if __has_attribute(__const__) || YB_IMPL_GNUCPP >= 20500
520 # define YB_STATELESS YB_ATTR(__const__)
521 #else
522 # define YB_STATELESS
523 #endif
524
525
526
527
549 #if defined(YB_DLL) && defined(YB_BUILD_DLL)
550 # error "DLL could not be built and used at the same time."
551 #endif
552
553 #if YB_IMPL_MSCPP \
554 || (YB_IMPL_GNUCPP && (defined(__MINGW32__) || defined(__CYGWIN__)))
555 # ifdef YB_DLL
556 # define YB_API __declspec(dllimport)
557 # elif defined(YB_BUILD_DLL)
558 # define YB_API __declspec(dllexport)
559 # else
560 # define YB_API
561 # endif
562 #elif defined(YB_BUILD_DLL) && (__has_attribute(__visibility__) \
563 || YB_IMPL_GNUCPP >= 40000 || YB_IMPL_CLANGPP)
564 # define YB_API YB_ATTR(__visibility__("default"))
565 #else
566 # define YB_API
567 #endif
568
569
575 #if false && !defined(NDEBUG)
576 # define YB_Use_DynamicExceptionSpecification 1
577 #endif
578
594 #ifndef NDEBUG
595 # ifndef YB_Use_YAssert
596 # define YB_Use_YAssert 1
597 # endif
598 #elif !defined(YB_Use_StrictNoThrow)
599 # define YB_Use_StrictNoThrow 1
600 #endif
601 #define YB_Use_YTrace 1
602
603
604
617 #if YB_HAS_ALIGNAS
618 # define yalignas alignas
619 # define yalignas_type alignas
620 #else
621 # define yalignas(_expr) YB_ATTR(__aligned__(_expr))
622 # define yalignas_type(_type) YB_ATTR(__aligned__(__alignof(_type)))
623 #endif
624
631 #if YB_HAS_ALIGNOF
632 # define yalignof alignof
633 #else
634 # define yalignof(_type) std::alignment_of<_type>::value
635 #endif
636
649 #if __cpp_constexpr >= 200704
650 # define yconstexpr constexpr
651 # define yconstfn constexpr
652 #else
653 # define yconstexpr
654 # define yconstfn inline
655 #endif
656
663 #if __cpp_constexpr >= 201304
664 # define yconstfn_relaxed constexpr
665 #else
666 # define yconstfn_relaxed inline
667 #endif
668
675 #if YB_IMPL_MSCPP || __INTEL_COMPILER >= 600 || __IBMCPP__ >= 500
676 # define yfname __FUNCTION__
677 #elif __BORLANDC__ >= 0x550
678 # define yfname __FUNC__
679 #elif __cplusplus >= 201103 || __STDC_VERSION__ >= 199901
680 # define yfname __func__
681 #else
682 # define yfname "<unknown-fn>"
683 #endif
684
691 #if YB_IMPL_GNUCPP || YB_IMPL_CLANGPP
692 # define yfsig __PRETTY_FUNCTION__
693 #elif defined(__FUNCSIG__)
694 # define yfsig __FUNCSIG__
695 #elif __INTEL_COMPILER >= 600 || __IBMCPP__ >= 500
696 # define yfsig __FUNCTION__
697 #elif __BORLANDC__ >= 0x550
698 # define yfsig __FUNC__
699 #elif __cplusplus >= 201103 || __STDC_VERSION__ >= 199901
700 # define yfsig __func__
701 #else
702 # define yfsig "<unknown-fsig>"
703 #endif
704
715 #if YB_HAS_NOEXCEPT
716 # define ynoexcept noexcept
717 # define ynoexcept_assert(_msg, ...) \
718 static_assert(noexcept(__VA_ARGS__), _msg)
719 #else
720 # define ynoexcept(...)
721 # define ynoexcept_assert(_msg, ...)
722 #endif
723
735 #if YB_HAS_NOEXCEPT
736 # define ynothrow ynoexcept
737 #elif YB_IMPL_GNUCPP >= 30300
738 # define ynothrow __attribute__ ((nothrow))
739 #else
740 # define ynothrow ythrow()
741 #endif
742
757 #if YB_Use_StrictNoThrow
758 # define ynothrowv ynothrow
759 #else
760 # define ynothrowv
761 #endif
762
768 #define ynoexcept_spec(...) ynoexcept(noexcept(__VA_ARGS__))
769
777 #if YB_HAS_THREAD_LOCAL && defined(_MT)
778 # define ythread thread_local
779 #else
780 # define ythread static
781 #endif
782
788 #if YB_Use_DynamicExceptionSpecification
789 # define ythrow throw
790 #else
791 # define ythrow(...)
792 #endif
793
794
795
796 namespace ystdex
797 {
798
808 using byte = unsigned char;
809
810 #if CHAR_BIT == 8
811
816 using octet = byte;
817 # else
818 using octet = void;
819 #endif
820
821 using std::ptrdiff_t;
822 using std::size_t;
823 using std::wint_t;
824
825 #if YB_HAS_BUILTIN_NULLPTR
826
827 using std::nullptr_t;
828
829 #else
830
835 const class nullptr_t
836 {
837 public:
839 template<typename _type>
840 inline
841 operator _type*() const
842 {
843 return 0;
844 }
845
847 template<class _tClass, typename _type>
848 inline
849 operator _type _tClass::*() const
850 {
851 return 0;
852 }
854 template<typename _type>
855 bool
856 equals(const _type& rhs) const
857 {
858 return rhs == 0;
859 }
860
862 void operator&() const = delete;
863 } nullptr = {};
864
866
867 template<typename _type>
868 inline bool
869 operator==(nullptr_t lhs, const _type& rhs)
870 {
871 return lhs.equals(rhs);
872 }
873 template<typename _type>
874 inline bool
875 operator==(const _type& lhs, nullptr_t rhs)
876 {
877 return rhs.equals(lhs);
878 }
879
880 template<typename _type>
881 inline bool
882 operator!=(nullptr_t lhs, const _type& rhs)
883 {
884 return !lhs.equals(rhs);
885 }
886 template<typename _type>
887 inline bool
888 operator!=(const _type& lhs, nullptr_t rhs)
889 {
890 return !rhs.equals(lhs);
891 }
893
894 #endif
895
896
901 template<typename...>
902 struct empty_base
903 {};
904
909 struct raw_tag
910 {};
911
912
925 template<bool _bMemObjPtr, bool _bNoExcept, class _type>
926 class offsetof_check
927 {
928 static_assert(std::is_class<_type>::value, "Non class type found.");
929 static_assert(std::is_standard_layout<_type>::value,
930 "Non standard layout type found.");
931 static_assert(_bMemObjPtr, "Non-static member object violation found.");
932 static_assert(_bNoExcept, "Exception guarantee violation found.");
933 };
934
935
942 #define yunused(...) static_cast<void>(__VA_ARGS__)
943
952 #define yoffsetof(_type, _member) \
953 (decltype(sizeof(ystdex::offsetof_check<std::is_member_object_pointer< \
954 decltype(&_type::_member)>::value, ynoexcept(offsetof(_type, _member)), \
955 _type>))(offsetof(_type, _member)))
956
969 #define yforward(_expr) std::forward<decltype(_expr)>(_expr)
970
976 template<typename _type, typename... _tParams>
977 yconstfn auto
978 unsequenced(_type&& arg, _tParams&&...) ynothrow -> decltype(yforward(arg))
979 {
980 return yforward(arg);
981 }
982
996 #define yunseq ystdex::unsequenced
997
998 } // namespace ystdex;
999
1000 #endif
1001
ystdex::operator==
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:869
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::unsequenced
auto unsequenced(_type &&arg, _tParams &&...) -> decltype(std::forward< decltype(arg)>(arg))
无序列依赖表达式组求值实现。
Definition: ydef.h:978
ystdex::operator!=
bool operator!=(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:882
platform::size_t
YF_API size_t
Definition: FileIO.h:1262
ystdex::offsetof_check
成员偏移计算静态类型检查。
Definition: ydef.h:926
ystdex::octet
void octet
Definition: ydef.h:818
ystdex::nullptr_t::equals
bool equals(const _type &rhs) const
支持关系运算符重载。
Definition: ydef.h:856
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::raw_tag
直接构造类型（直接构造重载用）。
Definition: ydef.h:909
ystdex::nullptr_t
空指针类。
Definition: ydef.h:835
ystdex::nullptr_t::operator&
void operator&() const =delete
禁止取 nullptr 的指针。
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
ystdex::empty_base
空基类模板。
Definition: ydef.h:902
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
YSLib::char
char
Definition: ValueNode.h:851