-
Notifications
You must be signed in to change notification settings - Fork 127
/
traits.h
111 lines (89 loc) · 3.71 KB
/
traits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __X_PACK_TRAITS_H
#define __X_PACK_TRAITS_H
#if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700
#define X_PACK_SUPPORT_CXX0X 1
#endif
namespace xpack {
// implement std::enable_if
template<bool B, class T = void>
struct x_enable_if {};
template <class T>
struct x_enable_if<true, T> { typedef T type; };
// mark XPACK_OUT
template <class T>
struct is_xpack_out{static bool const value = false;};
template <class T>
struct is_xpack_xtype {static bool const value = false;};
template <class CODER, class T>
struct is_xpack_type_spec {static bool const value = false;};
// for bitfield, declare raw type. thx https://stackoverflow.com/a/12199635/5845104
template<int N> struct x_size { char value[N]; };
x_size<1> x_decltype_encode(char);
x_size<2> x_decltype_encode(signed char);
x_size<3> x_decltype_encode(unsigned char);
x_size<4> x_decltype_encode(short);
x_size<5> x_decltype_encode(unsigned short);
x_size<6> x_decltype_encode(int);
x_size<7> x_decltype_encode(unsigned int);
x_size<8> x_decltype_encode(long);
x_size<9> x_decltype_encode(unsigned long);
x_size<10> x_decltype_encode(long long);
x_size<11> x_decltype_encode(unsigned long long);
template<int N> struct x_decltype_decode {};
template <> struct x_decltype_decode<1> {typedef char type;};
template <> struct x_decltype_decode<2> {typedef signed char type;};
template <> struct x_decltype_decode<3> {typedef unsigned char type;};
template <> struct x_decltype_decode<4> {typedef short type;};
template <> struct x_decltype_decode<5> {typedef unsigned short type;};
template <> struct x_decltype_decode<6> {typedef int type;};
template <> struct x_decltype_decode<7> {typedef unsigned int type;};
template <> struct x_decltype_decode<8> {typedef long type;};
template <> struct x_decltype_decode<9> {typedef unsigned long type;};
template <> struct x_decltype_decode<10> {typedef long long type;};
template <> struct x_decltype_decode<11> {typedef unsigned long long type;};
class noncopyable {
public:
noncopyable(){}
~noncopyable(){}
private:
noncopyable(const noncopyable&v);
noncopyable& operator = (const noncopyable&v);
};
}
#define x_pack_decltype(T) typename xpack::x_decltype_decode<sizeof(xpack::x_decltype_encode(T))>::type
// fix SFINAE bug of vs2005, so vs2005(or pre version) not support XPACK_OUT define xtype.
// can use custom
// The priority of xtype is the highest
#if defined _MSC_VER && _MSC_VER<=1400
#define XPACK_IS_XTYPE(Coder, T) typename x_enable_if<is_xpack_xtype<T>::value && !is_xpack_type_spec<Coder, T>::value && !is_xpack_out<T>::value, bool>::type
#else
#define XPACK_IS_XTYPE(Coder, T) typename x_enable_if<is_xpack_xtype<T>::value && !is_xpack_type_spec<Coder, T>::value, bool>::type
#endif
/*
struct A {
XPACK(...);
};
struct B : public A{
};
XPACK_OUT(B, ...);
in this case, B::__x_pack_value is true and should hit xpack_out, so
XPACK_IS_XPACK need to add !is_xpack_out<T>::value
*/
#define XPACK_IS_XOUT(T) typename x_enable_if<is_xpack_out<T>::value && !is_xpack_xtype<T>::value, bool>::type
#define XPACK_IS_XPACK(T) typename x_enable_if<T::__x_pack_value && !is_xpack_out<T>::value && !is_xpack_xtype<T>::value, bool>::type
#endif