YSTest: enum.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

enum.hpp
浏览该文件的文档.
1 /*
2 © 2015 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
28 #ifndef YB_INC_ystdex_enum_hpp_
29 #define YB_INC_ystdex_enum_hpp_ 1
30
31 #include "tuple.hpp" // for false_type, underlying_type_t, _t, and_, or_,
32 // is_enum, common_type_t, vseq::find, std::tuple;
33
34 namespace ystdex
35 {
36
38
39 template<typename>
41 struct is_enum_union : false_type
42 {};
43
44
46
47 template<typename _type>
48 struct wrapped_enum_traits
49 {
50 using type = underlying_type_t<_type>;
51 };
52
53 template<typename _type>
54 using wrapped_enum_traits_t = _t<wrapped_enum_traits<_type>>;
56
57
58 template<typename... _types>
59 class enum_union
60 {
61 static_assert(and_<or_<is_enum<_types>, is_enum_union<_types>>...>(),
62 "Invalid types found.");
63
64 public:
65 using underlying_type = common_type_t<wrapped_enum_traits_t<_types>...>;
66
67 private:
68 underlying_type value;
69
70 public:
71 yconstfn
72 enum_union() = default;
73 explicit yconstfn
74 enum_union(underlying_type i) ynothrow
75 : value(i)
76 {}
77 template<typename _type,
78 yimpl(typename = vseq::find<std::tuple<_types...>, _type>())>
79 yconstfn
80 enum_union(_type e) ynothrow
81 : value(static_cast<underlying_type>(e))
82 {}
83 yconstfn
84 enum_union(const enum_union&) = default;
85
86 enum_union&
87 operator=(const enum_union&) = default;
88
89 explicit yconstfn
90 operator underlying_type() const ynothrow
91 {
92 return value;
93 }
94 };
95
96
98
99 template<typename... _types>
100 struct is_enum_union<enum_union<_types...>> : true_type
101 {};
102
103 template<typename... _types>
104 struct wrapped_enum_traits<enum_union<_types...>>
105 {
106 using type = typename enum_union<_types...>::underlying_type;
107 };
108
109 template<typename _type>
110 yconstfn yimpl(enable_if_t)<is_enum_union<_type>::value,
111 wrapped_enum_traits_t<_type>>
112 underlying(_type val) ynothrow
113 {
114 return wrapped_enum_traits_t<_type>(val);
115 }
117
118
119 } // namespace ystdex;
120
121 #endif
122
ystdex::wrapped_enum_traits
Definition: enum.hpp:48
ystdex::enum_union::underlying_type
common_type_t< wrapped_enum_traits_t< _types >... > underlying_type
Definition: enum.hpp:65
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:350
ystdex::wrapped_enum_traits< enum_union< _types... > >::type
typename enum_union< _types... >::underlying_type type
Definition: enum.hpp:106
ystdex::enum_union::enum_union
enum_union(underlying_type i)
Definition: enum.hpp:74
ystdex::underlying
underlying_type_t< _type > underlying(_type val)
取枚举值的底层整数。
Definition: utility.hpp:120
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
ystdex::enum_union::value
underlying_type value
Definition: enum.hpp:68
ystdex::wrapped_enum_traits_t
_t< wrapped_enum_traits< _type >> wrapped_enum_traits_t
Definition: enum.hpp:54
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:96
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::enum_union::enum_union
enum_union()=default
tuple.hpp
元组类型和操作。
ystdex::is_enum_union
Definition: enum.hpp:41
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::enum_union::operator=
enum_union & operator=(const enum_union &)=default
ystdex::enum_union::enum_union
enum_union(_type e)
Definition: enum.hpp:80
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::wrapped_enum_traits::type
underlying_type_t< _type > type
Definition: enum.hpp:50
ystdex::or_
Definition: type_traits.hpp:412
ystdex::and_
逻辑操作元函数。
Definition: type_traits.hpp:394
ystdex::enum_union
Definition: enum.hpp:59
ystdex::vseq::find
查找元素。
Definition: variadic.hpp:576