YSTest: ydef.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ydef.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2009 - 2013.
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
69 #ifdef __cplusplus
70 # define YB_IMPL_CPP __cplusplus
71 # ifdef _MSC_VER
72 # undef YB_IMPL_MSCPP
73 # define YB_IMPL_MSCPP _MSC_VER
74 # elif defined(__GNUC__)
75 # undef YB_IMPL_GNUCPP
76 # define YB_IMPL_GNUCPP (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 \
77 + __GNUC_PATCHLEVEL__)
78 # else
79 // TODO: Complete version checking for compiler and library implementations.
80 //#ifdef __GNUC__
81 //# include <tr1/type_traits>
82 # error This language implementation is not supported!
83 # endif
84 #else
85 # error This header is only for C++!
86 #endif
87
89
90 #include <cstddef> // for std::nullptr_t, std::size_t, std::ptrdiff_t, offsetof;
91 #include <climits>
92 #include <cassert> // for assert;
93 #include <cstdint>
94 #include <cwchar> // for std::wint_t;
95 #include <utility> // for std::forward;
96 #include <type_traits> // for std::is_class, std::is_standard_layout;
97
98
104
110 #undef YB_HAS_ALIGNAS
111 #define YB_HAS_ALIGNAS (YB_IMPL_GNUCPP >= 40800)
112
118 #undef YB_HAS_ALIGNOF
119 #define YB_HAS_ALIGNOF (YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40500)
120
126 #undef YB_HAS_BUILTIN_NULLPTR
127 #define YB_HAS_BUILTIN_NULLPTR (YB_IMPL_CPP >= 201103L \
128 || YB_IMPL_GNUCPP >= 40600 || YB_IMPL_MSCPP >= 1600)
129
135 #undef YB_HAS_CONSTEXPR
136 #define YB_HAS_CONSTEXPR (YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40600)
137
143 #undef YB_HAS_NOEXCEPT
144 #define YB_HAS_NOEXCEPT (YB_IMPL_CPP >= 201103L || YB_IMPL_GNUCPP >= 40600)
145
147
148
156
163 #if YB_IMPL_GNUCPP >= 20500
164 # define YB_ATTR(...) __attribute__((__VA_ARGS__))
165 #else
166 # define YB_ATTR(...)
167 #endif
168
179 #if YB_IMPL_GNUCPP >= 20296
180 # define YB_ALLOCATOR YB_ATTR(__malloc__)
181 #else
182 # define YB_ALLOCATOR
183 #endif
184
192 #if YB_IMPL_GNUCPP >= 29600
193 # define YB_EXPECT(expr, constant) (__builtin_expect(expr, constant))
194 # define YB_LIKELY(expr) (__builtin_expect(bool(expr), 1))
195 # define YB_UNLIKELY(expr) (__builtin_expect(bool(expr), 0))
196 #else
197 # define YB_EXPECT(expr, constant) (expr)
198 # define YB_LIKELY (expr) (expr)
199 # define YB_UNLIKELY (expr) (expr)
200 #endif
201
209 #if YB_IMPL_GNUCPP >= 20296
210 # define YB_NORETURN YB_ATTR(__noreturn__)
211 #else
212 # define YB_NORETURN
213 #endif
214
229 #if YB_IMPL_GNUCPP >= 20296
230 # define YB_PURE YB_ATTR(__pure__)
231 #else
232 # define YB_PURE
233 #endif
234
252 #if YB_IMPL_GNUCPP >= 20500
253 # define YB_STATELESS YB_ATTR(__const__)
254 #else
255 # define YB_STATELESS
256 #endif
257
259
260
266
283 #if defined(YB_DLL) && defined(YB_BUILD_DLL)
284 # error DLL could not be built and used at the same time!
285 #endif
286
287 #ifdef YB_DLL
288 # define YB_API __declspec(dllimport)
289 #elif defined(YB_BUILD_DLL)
290 # define YB_API __declspec(dllexport)
291 #else
292 # define YB_API
293 #endif
294
295
301 #ifndef NDEBUG
302 # define YB_USE_YASSERT
303 #endif
304
305
311 #ifndef NDEBUG
312 # define YB_USE_EXCEPTION_SPECIFICATION 1
313 #endif
314
316
317
318
333 #if YB_HAS_ALIGNOF
334 # define yalignof alignof
335 #else
336 # define yalignof(_type) std::alignment_of<_type>::value
337 #endif
338
339
352 #if YB_HAS_CONSTEXPR
353 # define yconstexpr constexpr
354 # define yconstfn constexpr
355 #else
356 # define yconstexpr const
357 # define yconstfn inline
358 #endif
359
360
367 #if YB_USE_EXCEPTION_SPECIFICATION
368 # define ythrow throw
369 #else
370 # define ythrow(...)
371 #endif
372
378 #ifdef YB_HAS_NOEXCEPT
379 # define ynothrow ynoexcept
380 #else
381 # define ynothrow ythrow()
382 #endif
383
390 #if YB_HAS_NOEXCEPT
391 # define ynoexcept noexcept
392 #else
393 # define ynoexcept(...)
394 #endif
395
396
406 #define yconstraint assert
407
417 #define yassume assert
418
419
420 namespace ystdex
421 {
422
429 typedef unsigned char byte;
430
431 typedef int errno_t;
432 using std::ptrdiff_t;
433 using std::size_t;
434 using std::wint_t;
435
436 #if YB_HAS_BUILTIN_NULLPTR
437
438 using std::nullptr_t;
439
440 #else
441
446 const class nullptr_t
447 {
448 public:
449 /*
450 \brief 转换任意类型至空非成员或静态成员指针。
451 */
452 template<typename _type>
453 inline operator _type*() const
454 {
455 return 0;
456 }
457
458 /*
459 \brief 转换任意类型至空非静态成员指针。
460 */
461 template<class _tClass, typename _type>
462 inline operator _type _tClass::*() const
463 {
464 return 0;
465 }
466 /*
467 \brief 支持关系运算符重载。
468 */
469 template<typename _type> bool
470 equals(const _type& rhs) const
471 {
472 return rhs == 0;
473 }
474
475 /*
476 \brief 禁止取 nullptr 的指针。
477 */
478 void operator&() const = delete;
479 } nullptr = {};
480
482
483 template<typename _type>
484 inline bool
485 operator==(nullptr_t lhs, const _type& rhs)
486 {
487 return lhs.equals(rhs);
488 }
489 template<typename _type>
490 inline bool
491 operator==(const _type& lhs, nullptr_t rhs)
492 {
493 return rhs.equals(lhs);
494 }
495
496 template<typename _type>
497 inline bool
498 operator!=(nullptr_t lhs, const _type& rhs)
499 {
500 return !lhs.equals(rhs);
501 }
502 template<typename _type>
503 inline bool
504 operator!=(const _type& lhs, nullptr_t rhs)
505 {
506 return !rhs.equals(lhs);
507 }
509
510 #endif
511
512
517 template<typename...>
518 struct empty_base
519 {};
520
521
534 template<bool _bMemObjPtr, bool _bNoExcept, class _type>
535 class offsetof_check
536 {
537 static_assert(std::is_class<_type>::value, "Non class type found.");
538 static_assert(std::is_standard_layout<_type>::value,
539 "Non standard layout type found.");
540 static_assert(_bMemObjPtr, "Non-static member object violation found.");
541 static_assert(_bNoExcept, "Exception guarantee violation found.");
542 };
543
553 #define yoffsetof(_type, _member) \
554 (decltype(sizeof(ystdex::offsetof_check<std::is_member_object_pointer< \
555 decltype(&_type::_member)>::value, ynoexcept(offsetof(_type, _member)), \
556 _type>))(offsetof(_type, _member)))
557
558
569 #define yforward(_expr) std::forward<decltype(_expr)>(_expr)
570
576 template<typename _type, typename... _tParams>
577 yconstfn auto
578 unsequenced(_type&& arg, _tParams&&...) -> decltype(yforward(arg))
579 {
580 return yforward(arg);
581 }
582
595 #define yunseq ystdex::unsequenced
596
597 } // namespace ystdex;
598
599 #endif
600