1
1
// ==========================================================================
2
2
// type traits for smart pointers.
3
- // supporting is_unique_ptr< T >, is_shared_ptr< T >, is_smart_pointer < T >.
3
+ // supporting is_unique_ptr< T >, is_shared_ptr< T >, is_smart_ptr < T >, is_ptr< T >
4
4
// usage is like std::is_pointer< T >.
5
+ //
5
6
// ==========================================================================
6
7
7
8
#ifndef _smart_pointer_type_trait
8
9
#define _smart_pointer_type_trait
9
10
10
11
#include < memory>
11
12
12
- template < typename T > struct is_shared_ptr_impl : std::false_type {};
13
- template < typename T > struct is_shared_ptr_impl < std::shared_ptr< T > > : std::true_type {};
14
- template < typename T > struct is_shared_ptr : is_shared_ptr_impl< std::remove_cv_t < std::remove_reference_t < T > > > {};
15
- template < typename T > constexpr bool is_shared_ptr_v = is_shared_ptr< T >::value;
13
+ // =======================================================================================
14
+ // shared_ptr type trait
15
+ // =======================================================================================
16
+ template < typename T >
17
+ struct is_shared_ptr_impl : std::false_type {};
16
18
17
- template < typename T > struct is_unique_ptr_impl : std::false_type {};
18
- template < typename T, typename Dx > struct is_unique_ptr_impl < std::unique_ptr< T, Dx > > : std::true_type {};
19
- template < typename T > struct is_unique_ptr : is_unique_ptr_impl< std::remove_cv_t < std::remove_reference_t < T > > > {};
20
- template < typename T > constexpr bool is_unique_ptr_v = is_unique_ptr< T >::value;
19
+ template < typename T >
20
+ struct is_shared_ptr_impl < std::shared_ptr< T > > : std::true_type {};
21
+
22
+ template < typename T >
23
+ struct is_shared_ptr : is_shared_ptr_impl< std::remove_cv_t < std::remove_reference_t < T > > > {};
24
+
25
+ template < typename T >
26
+ constexpr bool is_shared_ptr_v = is_shared_ptr< T >::value;
27
+
28
+ template < typename T >
29
+ std::true_type is_shared_ptr_soft_impl ( const std::shared_ptr< T >* );
30
+ std::false_type is_shared_ptr_soft_impl ( ... );
31
+
32
+ template < typename T >
33
+ using is_shared_ptr_soft = decltype ( is_shared_ptr_soft_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
21
34
22
35
template < typename T >
23
- std::true_type inherit_from_shared_ptr_impl ( const std::shared_ptr< T >* );
24
- std::false_type inherit_from_shared_ptr_impl ( ... );
36
+ constexpr bool is_shared_ptr_soft_v = is_shared_ptr_soft< T >::value;
37
+
38
+ // shared_ptr type trait end ==============================================================
39
+
40
+ // =======================================================================================
41
+ // unique_ptr type trait
42
+ // =======================================================================================
25
43
template < typename T >
26
- using inherit_from_shared_ptr = decltype ( inherit_from_shared_ptr_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
27
- template < typename T > constexpr bool inherit_from_shared_ptr_v = inherit_from_shared_ptr< T >::value;
44
+ struct is_unique_ptr_impl : std::false_type {};
28
45
29
46
template < typename T, typename Dx >
30
- std::true_type inherit_from_unique_ptr_impl ( const std::unique_ptr< T, Dx >* );
31
- std::false_type inherit_from_unique_ptr_impl ( ... );
47
+ struct is_unique_ptr_impl < std::unique_ptr< T, Dx > > : std::true_type {};
48
+
49
+ template < typename T >
50
+ struct is_unique_ptr : is_unique_ptr_impl< std::remove_cv_t < std::remove_reference_t < T > > > {};
51
+
52
+ template < typename T > constexpr bool is_unique_ptr_v = is_unique_ptr< T >::value;
53
+
54
+ template < typename T, typename Dx >
55
+ std::true_type is_unique_ptr_soft_impl ( std::unique_ptr< T, Dx >* );
56
+ std::false_type is_unique_ptr_soft_impl ( ... );
57
+
58
+ template < typename T >
59
+ using is_unique_ptr_soft = decltype ( is_unique_ptr_soft_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
60
+
32
61
template < typename T >
33
- using inherit_from_unique_ptr = decltype ( inherit_from_unique_ptr_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
34
- template < typename T > constexpr bool inherit_from_unique_ptr_v = inherit_from_unique_ptr< T >::value;
62
+ constexpr bool is_unique_ptr_soft_v = is_unique_ptr_soft< T >::value;
35
63
36
- template < typename T > struct is_smart_ptr : std::conditional_t < is_shared_ptr_v< T >, std::true_type,
64
+ // unique_ptr type trait end ==============================================================
65
+
66
+ // =======================================================================================
67
+ // united type trait ( smart pointer, pointer( smart pointers + raw_pointer ) )
68
+ // =======================================================================================
69
+ template < typename T >
70
+ struct is_smart_ptr : std::conditional_t < is_shared_ptr_v< T >, std::true_type,
37
71
std::conditional_t < is_unique_ptr_v< T >, std::true_type, std::false_type > > {};
72
+
38
73
template < typename T > constexpr bool is_smart_ptr_v = is_smart_ptr< T >::value;
39
74
40
75
template < typename T, typename Dx >
41
- std::true_type inherit_from_smart_ptr_impl ( const std::unique_ptr< T, Dx >* );
76
+ std::true_type is_smart_ptr_soft_impl ( std::unique_ptr< T, Dx >* );
77
+ template < typename T >
78
+ std::true_type is_smart_ptr_soft_impl ( std::shared_ptr< T >* );
79
+ std::false_type is_smart_ptr_soft_impl ( ... );
80
+
81
+ template < typename T >
82
+ using is_smart_ptr_soft = decltype ( is_smart_ptr_soft_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
83
+
84
+ template < typename T >
85
+ constexpr bool is_smart_ptr_soft_v = is_smart_ptr_soft< T >::value;
86
+
87
+
88
+ template < typename T >
89
+ struct is_ptr_impl : std::conditional_t < std::is_pointer_v< T >, std::true_type,
90
+ std::conditional_t < is_smart_ptr_v< T >, std::true_type, std::false_type > > {};
91
+
92
+ template < typename T >
93
+ struct is_ptr : is_ptr_impl< std::remove_cv_t < std::remove_reference_t < T > > > {};
94
+
95
+ template < typename T >
96
+ constexpr bool is_ptr_v = is_ptr< T >::value;
97
+
98
+ template < typename T, typename Dx >
99
+ std::true_type is_ptr_soft_impl ( std::unique_ptr< T, Dx >* );
42
100
template < typename T >
43
- std::true_type inherit_from_smart_ptr_impl ( const std::shared_ptr< T >* );
44
- std::false_type inherit_from_smart_ptr_impl ( ... );
101
+ std::true_type is_ptr_soft_impl ( std::shared_ptr< T >* );
45
102
template < typename T >
46
- using inherit_from_smart_ptr = decltype ( inherit_from_smart_ptr_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
47
- template < typename T > constexpr bool inherit_from_smart_ptr_v = inherit_from_smart_ptr< T >::value;
103
+ std::true_type is_ptr_soft_impl ( T** );
104
+ std::false_type is_ptr_soft_impl ( ... );
105
+
106
+ template < typename T >
107
+ using is_ptr_soft = decltype ( is_ptr_soft_impl( std::declval< std::remove_cv_t < std::remove_reference_t < T > >* >() ) );
108
+
109
+ template < typename T >
110
+ constexpr bool is_ptr_soft_v = is_ptr_soft< T >::value;
111
+
112
+ // united type trait end ==============================================================
48
113
49
114
#endif
0 commit comments