]> git.saurik.com Git - apple/objc4.git/blob - runtime/PointerUnion.h
objc4-781.tar.gz
[apple/objc4.git] / runtime / PointerUnion.h
1 /*
2 * Copyright (c) 2019 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef POINTERUNION_H
25 #define POINTERUNION_H
26
27 #include <cstdint>
28 #include <atomic>
29
30 namespace objc {
31
32 template <typename T> struct PointerUnionTypeSelectorReturn {
33 using Return = T;
34 };
35
36 /// Get a type based on whether two types are the same or not.
37 ///
38 /// For:
39 ///
40 /// \code
41 /// using Ret = typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return;
42 /// \endcode
43 ///
44 /// Ret will be EQ type if T1 is same as T2 or NE type otherwise.
45 template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
46 struct PointerUnionTypeSelector {
47 using Return = typename PointerUnionTypeSelectorReturn<RET_NE>::Return;
48 };
49
50 template <typename T, typename RET_EQ, typename RET_NE>
51 struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> {
52 using Return = typename PointerUnionTypeSelectorReturn<RET_EQ>::Return;
53 };
54
55 template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
56 struct PointerUnionTypeSelectorReturn<
57 PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>> {
58 using Return =
59 typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return;
60 };
61
62 template <class PT1, class PT2>
63 class PointerUnion {
64 uintptr_t _value;
65
66 static_assert(alignof(PT1) >= 2, "alignment requirement");
67 static_assert(alignof(PT2) >= 2, "alignment requirement");
68
69 struct IsPT1 {
70 static const uintptr_t Num = 0;
71 };
72 struct IsPT2 {
73 static const uintptr_t Num = 1;
74 };
75 template <typename T> struct UNION_DOESNT_CONTAIN_TYPE {};
76
77 uintptr_t getPointer() const {
78 return _value & ~1;
79 }
80 uintptr_t getTag() const {
81 return _value & 1;
82 }
83
84 public:
85 explicit PointerUnion(const std::atomic<uintptr_t> &raw)
86 : _value(raw.load(std::memory_order_relaxed))
87 { }
88 PointerUnion(PT1 t) : _value((uintptr_t)t) { }
89 PointerUnion(PT2 t) : _value((uintptr_t)t | 1) { }
90
91 void storeAt(std::atomic<uintptr_t> &raw, std::memory_order order) const {
92 raw.store(_value, order);
93 }
94
95 template <typename T>
96 bool is() const {
97 using Ty = typename PointerUnionTypeSelector<PT1, T, IsPT1,
98 PointerUnionTypeSelector<PT2, T, IsPT2,
99 UNION_DOESNT_CONTAIN_TYPE<T>>>::Return;
100 return getTag() == Ty::Num;
101 }
102
103 template <typename T> T get() const {
104 ASSERT(is<T>() && "Invalid accessor called");
105 return reinterpret_cast<T>(getPointer());
106 }
107
108 template <typename T> T dyn_cast() const {
109 if (is<T>())
110 return get<T>();
111 return T();
112 }
113 };
114
115 template <class PT1, class PT2, class PT3, class PT4 = void>
116 class PointerUnion4 {
117 uintptr_t _value;
118
119 static_assert(alignof(PT1) >= 4, "alignment requirement");
120 static_assert(alignof(PT2) >= 4, "alignment requirement");
121 static_assert(alignof(PT3) >= 4, "alignment requirement");
122 static_assert(alignof(PT4) >= 4, "alignment requirement");
123
124 struct IsPT1 {
125 static const uintptr_t Num = 0;
126 };
127 struct IsPT2 {
128 static const uintptr_t Num = 1;
129 };
130 struct IsPT3 {
131 static const uintptr_t Num = 2;
132 };
133 struct IsPT4 {
134 static const uintptr_t Num = 3;
135 };
136 template <typename T> struct UNION_DOESNT_CONTAIN_TYPE {};
137
138 uintptr_t getPointer() const {
139 return _value & ~3;
140 }
141 uintptr_t getTag() const {
142 return _value & 3;
143 }
144
145 public:
146 explicit PointerUnion4(const std::atomic<uintptr_t> &raw)
147 : _value(raw.load(std::memory_order_relaxed))
148 { }
149 PointerUnion4(PT1 t) : _value((uintptr_t)t) { }
150 PointerUnion4(PT2 t) : _value((uintptr_t)t | 1) { }
151 PointerUnion4(PT3 t) : _value((uintptr_t)t | 2) { }
152 PointerUnion4(PT4 t) : _value((uintptr_t)t | 3) { }
153
154 void storeAt(std::atomic<uintptr_t> &raw, std::memory_order order) const {
155 raw.store(_value, order);
156 }
157
158 template <typename T>
159 bool is() const {
160 using Ty = typename PointerUnionTypeSelector<PT1, T, IsPT1,
161 PointerUnionTypeSelector<PT2, T, IsPT2,
162 PointerUnionTypeSelector<PT3, T, IsPT3,
163 PointerUnionTypeSelector<PT4, T, IsPT4,
164 UNION_DOESNT_CONTAIN_TYPE<T>>>>>::Return;
165 return getTag() == Ty::Num;
166 }
167
168 template <typename T> T get() const {
169 ASSERT(is<T>() && "Invalid accessor called");
170 return reinterpret_cast<T>(getPointer());
171 }
172
173 template <typename T> T dyn_cast() const {
174 if (is<T>())
175 return get<T>();
176 return T();
177 }
178 };
179
180 } // namespace objc
181
182 #endif /* DENSEMAPEXTRAS_H */