YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ydef.h
浏览该文件的文档.
1 /*
2  © 2009-2014 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 #ifdef __cplusplus
78 # define YB_IMPL_CPP __cplusplus
79 # ifdef _MSC_VER
80 # undef YB_IMPL_MSCPP
81 # define YB_IMPL_MSCPP _MSC_VER
82 # elif defined(__clang__)
83 # undef YB_IMPL_CLANGPP
84 # if defined(__apple_build_version__)
85 # define YB_IMPL_CLANGPP \
86  (40300 + __clang_patchlevel__)
87 # else
88 # define YB_IMPL_CLANGPP \
89  (__clang__ * 10000 + __clang_minor__ * 100 \
90  + __clang_patchlevel__)
91 # endif
92 # elif defined(__GNUC__)
93 # undef YB_IMPL_GNUCPP
94 # define YB_IMPL_GNUCPP \
95  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
96 # else
97 // TODO: Complete version checking for compiler and library implementations.
98 //#ifdef __GNUC__
99 //# include <tr1/type_traits>
100 # error "This language implementation is not supported."
101 # endif
102 #else
103 # error "This header is only for C++."
104 #endif
105 
106 #if YB_IMPL_MSCPP >= 1400
107 # define _CRT_SECURE_NO_WARNINGS
109 #endif
110 
111 
117 #ifndef __has_feature
118 # define __has_feature(...) 0
119 #endif
120 
121 #ifndef __has_extension
122 # define __has_extension(...) 0
123 #endif
124 
125 
126 #include <cstddef> // for std::nullptr_t, std::size_t, std::ptrdiff_t, offsetof;
127 #include <climits> // for CHAR_BIT;
128 #include <cassert> // for assert;
129 #include <cstdint>
130 #include <cwchar> // for std::wint_t;
131 #include <utility> // for std::forward;
132 #include <type_traits> // for std::is_class, std::is_standard_layout;
133 
134 
140 #define YPP_Empty
142 
147 #define YPP_Comma ,
148 
149 /*
150 \brief 带宏替换的记号连接。
151 \see http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html 。
152 \see https://www.securecoding.cert.org/confluence/display/cplusplus/PRE05-CPP.+Understand+macro+replacement+when+concatenating+tokens+or+performing+stringification 。
153 
154 注意 ISO/IEC C++ 未确定宏定义内 # 和 ## 操作符求值顺序。
155 注意 GCC 中,宏定义内 ## 操作符修饰的形式参数为宏时,此宏不会被展开。
156 */
157 #define YPP_Join(x, y) YPP_Concat(x, y)
158 
159 /*
160 \brief 记号连接。
161 \sa YPP_Join
162 */
163 #define YPP_Concat(x, y) x ## y
164 
165 
166 
177 #define yimpl(...) __VA_ARGS__
178 
179 
185 
191 #undef YB_HAS_ALIGNAS
192 #define YB_HAS_ALIGNAS \
193  (__has_feature(cxx_alignas) || __has_extension(cxx_alignas) || \
194  YB_IMPL_GNUCPP >= 40800)
195 
201 #undef YB_HAS_ALIGNOF
202 #define YB_HAS_ALIGNOF (YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40500)
203 
209 #undef YB_HAS_BUILTIN_NULLPTR
210 #define YB_HAS_BUILTIN_NULLPTR \
211  (__has_feature(cxx_nullptr) || __has_extension(cxx_nullptr) || \
212  YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40600 || \
213  YB_IMPL_MSCPP >= 1600)
214 
220 #undef YB_HAS_CONSTEXPR
221 #define YB_HAS_CONSTEXPR \
222  (__has_feature(cxx_constexpr) || YB_IMPL_CPP >= 201103L || \
223  YB_IMPL_GNUCPP >= 40600)
224 
230 #undef YB_HAS_NOEXCEPT
231 #define YB_HAS_NOEXCEPT \
232  (__has_feature(cxx_noexcept) || __has_extension(cxx_noexcept) || \
233  YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40600)
234 
241 #undef YB_HAS_THREAD_LOCAL
242 #define YB_HAS_THREAD_LOCAL \
243  (__has_feature(cxx_thread_local) || (YB_IMPL_CPP >= 201103L \
244  && !YB_IMPL_GNUCPP) || YB_IMPL_GNUCPP >= 40800)
245 
246 
247 
255 
262 #if YB_IMPL_GNUCPP >= 20500
263 # define YB_ATTR(...) __attribute__((__VA_ARGS__))
264 #else
265 # define YB_ATTR(...)
266 #endif
267 
278 #if YB_IMPL_GNUCPP >= 20296
279 # define YB_ALLOCATOR YB_ATTR(__malloc__)
280 #else
281 # define YB_ALLOCATOR
282 #endif
283 
291 #if YB_IMPL_GNUCPP >= 29600
292 # define YB_EXPECT(expr, constant) (__builtin_expect(expr, constant))
293 # define YB_LIKELY(expr) (__builtin_expect(bool(expr), 1))
294 # define YB_UNLIKELY(expr) (__builtin_expect(bool(expr), 0))
295 #else
296 # define YB_EXPECT(expr, constant) (expr)
297 # define YB_LIKELY(expr) (expr)
298 # define YB_UNLIKELY(expr) (expr)
299 #endif
300 
308 #if YB_IMPL_GNUCPP >= 20296
309 # define YB_NORETURN YB_ATTR(__noreturn__)
310 #else
311 # define YB_NORETURN
312 #endif
313 
328 #if YB_IMPL_GNUCPP >= 20296
329 # define YB_PURE YB_ATTR(__pure__)
330 #else
331 # define YB_PURE
332 #endif
333 
351 #if YB_IMPL_GNUCPP >= 20500
352 # define YB_STATELESS YB_ATTR(__const__)
353 #else
354 # define YB_STATELESS
355 #endif
356 
358 
359 
365 
382 #if defined(YB_DLL) && defined(YB_BUILD_DLL)
383 # error "DLL could not be built and used at the same time."
384 #endif
385 
386 #ifdef YB_DLL
387 # define YB_API __declspec(dllimport)
388 #elif defined(YB_BUILD_DLL)
389 # define YB_API __declspec(dllexport)
390 #else
391 # define YB_API
392 #endif
393 
394 
405 #ifndef NDEBUG
406 # ifndef YB_Use_YAssert
407 # define YB_Use_YAssert 1
408 # endif
409 #endif
410 #define YB_Use_YTrace 1
411 
412 
418 #if 0 && !defined(NDEBUG)
419 # define YB_USE_EXCEPTION_SPECIFICATION 1
420 #endif
421 
423 
424 
439 #if YB_HAS_ALIGNOF
440 # define yalignof alignof
441 #else
442 # define yalignof(_type) std::alignment_of<_type>::value
443 #endif
444 
445 
458 #if YB_HAS_CONSTEXPR
459 # define yconstexpr constexpr
460 # define yconstfn constexpr
461 #else
462 # define yconstexpr const
463 # define yconstfn inline
464 #endif
465 
466 
473 #if YB_USE_EXCEPTION_SPECIFICATION
474 # define ythrow throw
475 #else
476 # define ythrow(...)
477 #endif
478 
491 #ifdef YB_USE_EXCEPTION_VALIDATION
492 # define ynothrowv ynothrow
493 #else
494 # define ynothrowv
495 #endif
496 
509 #if YB_HAS_NOEXCEPT
510 # define ynothrow ynoexcept
511 #elif YB_IMPL_GNUCPP >= 30300
512 # define ynothrow __attribute__ ((nothrow))
513 #else
514 # define ynothrow ythrow()
515 #endif
516 
523 #if YB_HAS_NOEXCEPT
524 # define ynoexcept noexcept
525 #else
526 # define ynoexcept(...)
527 #endif
528 
536 #if YB_HAS_THREAD_LOCAL && defined(_MT)
537 # define ythread thread_local
538 #else
539 # define ythread static
540 #endif
541 
542 
543 namespace ystdex
544 {
545 
555 using byte = unsigned char;
556 
557 #if CHAR_BIT == 8
558 
563 using octet = byte;
564 # else
565 using octet = void;
566 #endif
567 
568 using errno_t = int;
569 using std::ptrdiff_t;
570 using std::size_t;
571 using std::wint_t;
572 
573 #if YB_HAS_BUILTIN_NULLPTR
574 
575 using std::nullptr_t;
576 
577 #else
578 
583 const class nullptr_t
584 {
585 public:
586  /*
587  \brief 转换任意类型至空非成员或静态成员指针。
588  */
589  template<typename _type>
590  inline
591  operator _type*() const
592  {
593  return 0;
594  }
595 
596  /*
597  \brief 转换任意类型至空非静态成员指针。
598  */
599  template<class _tClass, typename _type>
600  inline
601  operator _type _tClass::*() const
602  {
603  return 0;
604  }
605  /*
606  \brief 支持关系运算符重载。
607  */
608  template<typename _type>
609  bool
610  equals(const _type& rhs) const
611  {
612  return rhs == 0;
613  }
614 
615  /*
616  \brief 禁止取 nullptr 的指针。
617  */
618  void operator&() const = delete;
619 } nullptr = {};
620 
622 
623 template<typename _type>
624 inline bool
625 operator==(nullptr_t lhs, const _type& rhs)
626 {
627  return lhs.equals(rhs);
628 }
629 template<typename _type>
630 inline bool
631 operator==(const _type& lhs, nullptr_t rhs)
632 {
633  return rhs.equals(lhs);
634 }
635 
636 template<typename _type>
637 inline bool
638 operator!=(nullptr_t lhs, const _type& rhs)
639 {
640  return !lhs.equals(rhs);
641 }
642 template<typename _type>
643 inline bool
644 operator!=(const _type& lhs, nullptr_t rhs)
645 {
646  return !rhs.equals(lhs);
647 }
649 
650 #endif
651 
652 
657 template<typename...>
659 {};
660 
666 
667 
680 template<bool _bMemObjPtr, bool _bNoExcept, class _type>
682 {
683  static_assert(std::is_class<_type>::value, "Non class type found.");
684  static_assert(std::is_standard_layout<_type>::value,
685  "Non standard layout type found.");
686  static_assert(_bMemObjPtr, "Non-static member object violation found.");
687  static_assert(_bNoExcept, "Exception guarantee violation found.");
688 };
689 
690 
697 #define yunused(...) static_cast<void>(__VA_ARGS__)
698 
707 #define yoffsetof(_type, _member) \
708  (decltype(sizeof(ystdex::offsetof_check<std::is_member_object_pointer< \
709  decltype(&_type::_member)>::value, ynoexcept(offsetof(_type, _member)), \
710  _type>))(offsetof(_type, _member)))
711 
722 #define yforward(_expr) std::forward<decltype(_expr)>(_expr)
723 
729 template<typename _type, typename... _tParams>
730 yconstfn auto
731 unsequenced(_type&& arg, _tParams&&...) -> decltype(yforward(arg))
732 {
733  return yforward(arg);
734 }
735 
748 #define yunseq ystdex::unsequenced
749 
750 
757 } // namespace ystdex;
758 
759 #endif
760 
bool operator!=(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:638
unsigned char byte
字节类型。
Definition: ydef.h:555
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
bool equals(const _type &rhs) const
Definition: ydef.h:610
void operator&() const =delete
空指针类。
Definition: ydef.h:583
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
int errno_t
Definition: ydef.h:568
空基类模板。
Definition: ydef.h:658
auto unsequenced(_type &&arg, _tParams &&...) -> decltype(std::forward< decltype(arg)>(arg))
无序列依赖表达式组求值实现。
Definition: ydef.h:731
void octet
Definition: ydef.h:565
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:625
成员偏移计算静态类型检查。
Definition: ydef.h:681