]> git.saurik.com Git - apple/objc4.git/blob - runtime/PointerUnion.h
objc4-818.2.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 T1, class T2, typename Auth1, typename Auth2>
63 class PointerUnion {
64 uintptr_t _value;
65
66 static_assert(alignof(T1) >= 2, "alignment requirement");
67 static_assert(alignof(T2) >= 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(T1 *t, const void *address) {
89 _value = (uintptr_t)Auth1::sign(t, address);
90 }
91 PointerUnion(T2 *t, const void *address) {
92 _value = (uintptr_t)Auth2::sign(t, address) | 1;
93 }
94
95 void storeAt(std::atomic<uintptr_t> &raw, std::memory_order order) const {
96 raw.store(_value, order);
97 }
98
99 template <typename T>
100 bool is() const {
101 using Ty = typename PointerUnionTypeSelector<T1 *, T, IsPT1,
102 PointerUnionTypeSelector<T2 *, T, IsPT2,
103 UNION_DOESNT_CONTAIN_TYPE<T>>>::Return;
104 return getTag() == Ty::Num;
105 }
106
107 template <typename T> T get(const void *address) const {
108 ASSERT(is<T>() && "Invalid accessor called");
109 using AuthT = typename PointerUnionTypeSelector<T1 *, T, Auth1,
110 PointerUnionTypeSelector<T2 *, T, Auth2,
111 UNION_DOESNT_CONTAIN_TYPE<T>>>::Return;
112
113 return AuthT::auth((T)getPointer(), address);
114 }
115
116 template <typename T> T dyn_cast(const void *address) const {
117 if (is<T>())
118 return get<T>(address);
119 return T();
120 }
121 };
122
123 template <class PT1, class PT2, class PT3, class PT4 = void>
124 class PointerUnion4 {
125 uintptr_t _value;
126
127 static_assert(alignof(PT1) >= 4, "alignment requirement");
128 static_assert(alignof(PT2) >= 4, "alignment requirement");
129 static_assert(alignof(PT3) >= 4, "alignment requirement");
130 static_assert(alignof(PT4) >= 4, "alignment requirement");
131
132 struct IsPT1 {
133 static const uintptr_t Num = 0;
134 };
135 struct IsPT2 {
136 static const uintptr_t Num = 1;
137 };
138 struct IsPT3 {
139 static const uintptr_t Num = 2;
140 };
141 struct IsPT4 {
142 static const uintptr_t Num = 3;
143 };
144 template <typename T> struct UNION_DOESNT_CONTAIN_TYPE {};
145
146 uintptr_t getPointer() const {
147 return _value & ~3;
148 }
149 uintptr_t getTag() const {
150 return _value & 3;
151 }
152
153 public:
154 explicit PointerUnion4(const std::atomic<uintptr_t> &raw)
155 : _value(raw.load(std::memory_order_relaxed))
156 { }
157 PointerUnion4(PT1 t) : _value((uintptr_t)t) { }
158 PointerUnion4(PT2 t) : _value((uintptr_t)t | 1) { }
159 PointerUnion4(PT3 t) : _value((uintptr_t)t | 2) { }
160 PointerUnion4(PT4 t) : _value((uintptr_t)t | 3) { }
161
162 void storeAt(std::atomic<uintptr_t> &raw, std::memory_order order) const {
163 raw.store(_value, order);
164 }
165
166 template <typename T>
167 bool is() const {
168 using Ty = typename PointerUnionTypeSelector<PT1, T, IsPT1,
169 PointerUnionTypeSelector<PT2, T, IsPT2,
170 PointerUnionTypeSelector<PT3, T, IsPT3,
171 PointerUnionTypeSelector<PT4, T, IsPT4,
172 UNION_DOESNT_CONTAIN_TYPE<T>>>>>::Return;
173 return getTag() == Ty::Num;
174 }
175
176 template <typename T> T get() const {
177 ASSERT(is<T>() && "Invalid accessor called");
178 return reinterpret_cast<T>(getPointer());
179 }
180
181 template <typename T> T dyn_cast() const {
182 if (is<T>())
183 return get<T>();
184 return T();
185 }
186 };
187
188 } // namespace objc
189
190 #endif /* DENSEMAPEXTRAS_H */