YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
thread_local_proxy.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace yaclib::detail::fiber {
4
5void* GetImpl(std::uint64_t i);
6
7void Set(void* new_value, std::uint64_t i);
8
9void SetDefault(void* new_value, std::uint64_t i);
10
11template <typename Type>
13 inline static std::uint64_t sNextFreeIndex = 0;
14
15 public:
16 ThreadLocalPtrProxy() noexcept : _i(sNextFreeIndex++) {
17 }
18
19 ThreadLocalPtrProxy(Type* value) noexcept : _i(sNextFreeIndex++) {
20 if (value != nullptr) {
21 SetDefault(value, _i);
22 }
23 }
26 ThreadLocalPtrProxy(const ThreadLocalPtrProxy& other) noexcept : _i(sNextFreeIndex++) {
27 SetDefault(GetImpl(other._i), _i);
28 }
29
31 Set(value, this->_i);
32 return *this;
33 }
35 _i = other._i;
36 return *this;
37 }
39 if (this->Get() == other.Get()) {
40 return *this;
41 }
42 SetDefault(GetImpl(other._i), _i);
43 return *this;
44 }
45
46 template <typename U>
49 template <typename U>
51 SetDefault(GetImpl(other._i), _i);
52 }
53
54 template <typename U>
56 _i = other._i;
57 return *this;
58 }
59 template <typename U>
61 SetDefault(GetImpl(other._i), _i);
62 return *this;
63 }
64
66 auto& val = *(Get());
67 return val;
68 }
69
71 return Get();
72 }
73
74 Type* Get() const noexcept {
75 return static_cast<Type*>(GetImpl(this->_i));
76 }
77
78 explicit operator bool() const noexcept {
79 return Get() != nullptr;
80 }
81
82 Type& operator[](std::size_t i) const {
83 auto* val = Get();
84 val += i;
85 return *val;
86 }
87
88 private:
89 std::uint64_t _i;
90};
91
92template <typename T, typename U>
93inline bool operator==(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
94 return lhs.Get() == rhs.Get();
95}
96
97template <typename T, typename U>
98inline bool operator!=(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
99 return lhs.Get() != rhs.Get();
100}
101
102template <typename T, typename U>
103inline bool operator==(const ThreadLocalPtrProxy<T>& lhs, U* rhs) noexcept {
104 return lhs.Get() == rhs;
105}
106
107template <typename T, typename U>
108inline bool operator!=(const ThreadLocalPtrProxy<T>& lhs, U* rhs) noexcept {
109 return lhs.Get() != rhs;
110}
111
112template <typename T, typename U>
113inline bool operator==(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
114 return lhs == rhs.Get();
115}
116
117template <typename T, typename U>
118inline bool operator!=(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
119 return lhs != rhs.Get();
120}
121
122template <typename T>
123inline bool operator==(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
124 return lhs.Get() == nullptr;
125}
126
127template <typename T>
128inline bool operator==(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
129 return rhs.Get() == nullptr;
130}
131
132template <typename T>
133inline bool operator!=(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
134 return lhs.Get() != nullptr;
135}
136
137template <typename T>
138inline bool operator!=(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
139 return rhs.Get() != nullptr;
140}
141
142template <typename T, typename U>
143inline bool operator<(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
144 return lhs.Get() < rhs.Get();
145}
146
147template <typename T, typename U>
148inline bool operator>(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
149 return lhs.Get() > rhs.Get();
150}
151
152template <typename T, typename U>
153inline bool operator<(const ThreadLocalPtrProxy<T>& lhs, const U* rhs) noexcept {
154 return lhs.Get() < rhs;
155}
156
157template <typename T, typename U>
158inline bool operator<(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
159 return lhs < rhs.Get();
160}
161
162template <typename T, typename U>
163inline bool operator>(const ThreadLocalPtrProxy<T>& lhs, const U* rhs) noexcept {
164 return lhs.Get() > rhs;
165}
166
167template <typename T, typename U>
168inline bool operator>(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
169 return lhs > rhs.Get();
170}
171
172template <typename T>
173inline bool operator<(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
174 return lhs.Get() < nullptr;
175}
176
177template <typename T>
178inline bool operator<(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
179 return nullptr < rhs.Get();
180}
181
182template <typename T>
183inline bool operator>(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
184 return lhs.Get() > nullptr;
185}
186
187template <typename T>
188inline bool operator>(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
189 return nullptr > rhs.Get();
190}
191
192template <typename T, typename U>
193inline bool operator>=(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
194 return lhs.Get() >= rhs.Get();
195}
196
197template <typename T, typename U>
198inline bool operator<=(const ThreadLocalPtrProxy<T>& lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
199 return lhs.Get() <= rhs.Get();
200}
201
202template <typename T, typename U>
203inline bool operator<=(const ThreadLocalPtrProxy<T>& lhs, const U* rhs) noexcept {
204 return lhs.Get() <= rhs;
205}
206
207template <typename T, typename U>
208inline bool operator<=(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
209 return lhs <= rhs.Get();
210}
211
212template <typename T, typename U>
213inline bool operator>=(const ThreadLocalPtrProxy<T>& lhs, const U* rhs) noexcept {
214 return lhs.Get() >= rhs;
215}
216
217template <typename T, typename U>
218inline bool operator>=(T* lhs, const ThreadLocalPtrProxy<U>& rhs) noexcept {
219 return lhs >= rhs.Get();
220}
221
222template <typename T>
223inline bool operator<=(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
224 return lhs.Get() <= nullptr;
225}
226
227template <typename T>
228inline bool operator<=(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
229 return nullptr <= rhs.Get();
230}
231
232template <typename T>
233inline bool operator>=(const ThreadLocalPtrProxy<T>& lhs, std::nullptr_t) noexcept {
234 return lhs.Get() >= nullptr;
235}
236
237template <typename T>
238inline bool operator>=(std::nullptr_t, const ThreadLocalPtrProxy<T>& rhs) noexcept {
239 return nullptr >= rhs.Get();
240}
241
242} // namespace yaclib::detail::fiber
ThreadLocalPtrProxy & operator=(const ThreadLocalPtrProxy &other) noexcept
ThreadLocalPtrProxy & operator=(Type *value) noexcept
ThreadLocalPtrProxy(const ThreadLocalPtrProxy &other) noexcept
ThreadLocalPtrProxy(const ThreadLocalPtrProxy< U > &other) noexcept
ThreadLocalPtrProxy & operator=(const ThreadLocalPtrProxy< U > &other) noexcept
ThreadLocalPtrProxy(ThreadLocalPtrProxy &&other) noexcept
ThreadLocalPtrProxy(ThreadLocalPtrProxy< U > &&other) noexcept
ThreadLocalPtrProxy & operator=(ThreadLocalPtrProxy &&other) noexcept
ThreadLocalPtrProxy & operator=(ThreadLocalPtrProxy< U > &&other) noexcept
bool operator<(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
void SetDefault(void *new_value, std::uint64_t i)
bool operator>=(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
void * GetImpl(std::uint64_t i)
bool operator>(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
bool operator==(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
bool operator<=(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
void Set(void *new_value, std::uint64_t i)
bool operator!=(const ThreadLocalPtrProxy< T > &lhs, const ThreadLocalPtrProxy< U > &rhs) noexcept
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25