Platform.Data.Doublets C++
Loading...
Searching...
No Matches
LinksBase.h
Go to the documentation of this file.
2{
3
4 template <typename Signature>
5 thread_local std::function<Signature> GLOBAL_FUNCTION = nullptr;
6
7 template <typename TReturn, typename Signature, typename... TArgs>
8 TReturn call_last_global(TArgs... args)
9 {
10 decltype(auto) result = GLOBAL_FUNCTION<Signature>(args...);
11 return result;
12 }
13
14 template <typename Signature>
15 void set_global(std::function<Signature> function)
16 {
17 GLOBAL_FUNCTION<Signature> = std::move(function);
18 }
19
20 template <typename LinkAddressType>
21 using CUDCallback = LinkAddressType (*)(Link<LinkAddressType> before, Link<LinkAddressType> after);
22
23 template <typename LinkAddressType>
24 using EachCallback = LinkAddressType (*)(Link<LinkAddressType>);
25
26 template <typename LinkAddressType>
28 {
29 LinkAddressType index_part;
30 LinkAddressType source_part;
31 LinkAddressType target_part;
32 LinkAddressType null;
33 LinkAddressType $continue;
34 LinkAddressType $break;
35 LinkAddressType skip;
36 LinkAddressType any;
37 LinkAddressType itself;
38 LinkAddressType error;
39 Ranges::Range<LinkAddressType> internal_range;
40 Ranges::Range<LinkAddressType> external_range;
42 };
43
44 extern "C"
45 {
46 void* ByteLinks_New(const char* path);
47
48 void* UInt16Links_New(const char* path);
49
50 void* UInt32Links_New(const char* path);
51
52 void* UInt64Links_New(const char* path);
53
54 void ByteLinks_Drop(void* this_);
55
56 void UInt16Links_Drop(void* this_);
57
58 void UInt32Links_Drop(void* this_);
59
60 void UInt64Links_Drop(void* this_);
61
63
65
67
69
70 uint8_t ByteLinks_Create(void* this_, const uint8_t* query, uintptr_t len, CUDCallback<uint8_t> callback);
71
72 uint16_t UInt16Links_Create(void* this_, const uint16_t* query, uintptr_t len, CUDCallback<uint16_t> callback);
73
74 uint32_t UInt32Links_Create(void* this_, const uint32_t* query, uintptr_t len, CUDCallback<uint32_t> callback);
75
76 uint64_t UInt64Links_Create(void* this_, const uint64_t* query, uintptr_t len, CUDCallback<uint64_t> callback);
77
78 uint8_t ByteLinks_Each(void* this_, const uint8_t* query, uintptr_t len, EachCallback<uint8_t> callback);
79
80 uint16_t UInt16Links_Each(void* this_, const uint16_t* query, uintptr_t len, EachCallback<uint16_t> callback);
81
82 uint32_t UInt32Links_Each(void* this_, const uint32_t* query, uintptr_t len, EachCallback<uint32_t> callback);
83
84 uint64_t UInt64Links_Each(void* this_, const uint64_t* query, uintptr_t len, EachCallback<uint64_t> callback);
85
86 uint8_t ByteLinks_Count(void* this_, const uint8_t* query, uintptr_t len);
87
88 uint16_t UInt16Links_Count(void* this_, const uint16_t* query, uintptr_t len);
89
90 uint32_t UInt32Links_Count(void* this_, const uint32_t* query, uintptr_t len);
91
92 uint64_t UInt64Links_Count(void* this_, const uint64_t* query, uintptr_t len);
93
94 uint8_t ByteLinks_Update(void* this_, const uint8_t* restriction, uintptr_t len_r, const uint8_t* substitution,
95 uintptr_t len_s, CUDCallback<uint8_t> callback);
96
97 uint16_t UInt16Links_Update(void* this_, const uint16_t* restriction, uintptr_t len_r,
98 const uint16_t* substitution, uintptr_t len_s, CUDCallback<uint16_t> callback);
99
100 uint32_t UInt32Links_Update(void* this_, const uint32_t* restriction, uintptr_t len_r,
101 const uint32_t* substitution, uintptr_t len_s, CUDCallback<uint32_t> callback);
102
103 uint64_t UInt64Links_Update(void* this_, const uint64_t* restriction, uintptr_t len_r,
104 const uint64_t* substitution, uintptr_t len_s, CUDCallback<uint64_t> callback);
105
106 uint8_t ByteLinks_Delete(void* this_, const uint8_t* query, uintptr_t len, CUDCallback<uint8_t> callback);
107
108 uint16_t UInt16Links_Delete(void* this_, const uint16_t* query, uintptr_t len, CUDCallback<uint16_t> callback);
109
110 uint32_t UInt32Links_Delete(void* this_, const uint32_t* query, uintptr_t len, CUDCallback<uint32_t> callback);
111
112 uint64_t UInt64Links_Delete(void* this_, const uint64_t* query, uintptr_t len, CUDCallback<uint64_t> callback);
113
115 }
116
117 template <typename Stop>
118 struct stopper
119 {
120 static constexpr bool value = false;
121 };
122
123 template <typename TSelf, typename TLinkOptions, typename... TBase>
124 class LinksBase : public Interfaces::Polymorph<TSelf, TBase...>
125 {
126 private:
127 void* _ptr;
128
129 public:
130 using LinksOptionsType = TLinkOptions;
131 using LinkAddressType = typename LinksOptionsType::LinkAddressType;
132 using LinkType = typename LinksOptionsType::LinkType;
133 using WriteHandlerType = typename LinksOptionsType::WriteHandlerType;
134 using ReadHandlerType = typename LinksOptionsType::ReadHandlerType;
135 static constexpr LinksConstants<LinkAddressType> Constants = LinksOptionsType::Constants;
136
137 LinksBase(std::string_view path)
138 {
139 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
140 {
141 _ptr = ByteLinks_New(path.data());
142 }
143 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
144 {
145 _ptr = UInt16Links_New(path.data());
146 }
147 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
148 {
149 _ptr = UInt32Links_New(path.data());
150 }
151 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
152 {
153 _ptr = UInt64Links_New(path.data());
154 }
155 else
156 {
157 static_assert(
159 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
160 }
161 }
162
164 {
165 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
166 {
167 if (_ptr != nullptr)
168 {
169 ByteLinks_Drop(_ptr);
170 }
171 }
172 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
173 {
174 if (_ptr != nullptr)
175 {
176 UInt16Links_Drop(_ptr);
177 }
178 }
179 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
180 {
181 if (_ptr != nullptr)
182 {
183 UInt32Links_Drop(_ptr);
184 }
185 }
186 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
187 {
188 if (_ptr != nullptr)
189 {
190 UInt64Links_Drop(_ptr);
191 }
192 }
193 else
194 {
195 static_assert(
197 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
198 }
199 }
200
201 LinkAddressType Create(const LinkType& substitution, const WriteHandlerType& handler)
202 {
203 auto substitutionLength = std::ranges::size(substitution);
204 auto substitutionPtr = std::ranges::data(substitution);
205 auto callback = [&](Link<LinkAddressType> before, Link<LinkAddressType> after) -> LinkAddressType {
206 Link beforeLink{before.Index, before.Source, before.Target};
207 Link afterLink{after.Index, after.Source, after.Target};
208 return handler(beforeLink, afterLink);
209 };
211 set_global<Signature>(callback);
212 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
213 {
214 return ByteLinks_Create(_ptr, substitutionPtr, substitutionLength,
216 }
217 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
218 {
219 return UInt16Links_Create(_ptr, substitutionPtr, substitutionLength,
221 }
222 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
223 {
224 return UInt32Links_Create(_ptr, substitutionPtr, substitutionLength,
226 }
227 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
228 {
229 return UInt64Links_Create(_ptr, substitutionPtr, substitutionLength,
231 }
232 else
233 {
234 static_assert(
236 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
237 }
238 };
239
240 LinkAddressType Update(const LinkType& restriction, const LinkType& substitution,
241 const WriteHandlerType& handler)
242 {
243 auto restrictionLength = std::ranges::size(restriction);
244 auto restrictionPtr{std::ranges::data(restriction)};
245 auto substitutionLength = std::ranges::size(substitution);
246 auto substitutionPtr{std::ranges::data(substitution)};
247 auto callback = [&](Link<LinkAddressType> before, Link<LinkAddressType> after) {
248 Link beforeLink{before.Index, before.Source, before.Target};
249 Link afterLink{after.Index, after.Source, after.Target};
250 return handler(beforeLink, afterLink);
251 };
253 set_global<Signature>(callback);
254 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
255 {
256 return ByteLinks_Update(_ptr, restrictionPtr, restrictionLength, substitutionPtr, substitutionLength,
258 }
259 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
260 {
261 return UInt16Links_Update(_ptr, restrictionPtr, restrictionLength, substitutionPtr, substitutionLength,
263 }
264 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
265 {
266 return UInt32Links_Update(_ptr, restrictionPtr, restrictionLength, substitutionPtr, substitutionLength,
268 }
269 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
270 {
271 return UInt64Links_Update(_ptr, restrictionPtr, restrictionLength, substitutionPtr, substitutionLength,
273 }
274 else
275 {
276 static_assert(
278 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
279 }
280 }
281
282 LinkAddressType Delete(const LinkType& restriction, const WriteHandlerType& handler)
283 {
284 auto restrictionLength = std::ranges::size(restriction);
285 auto restrictionPtr = std::ranges::data(restriction);
286 auto callback = [&](Link<LinkAddressType> before, Link<LinkAddressType> after) {
287 Link beforeLink{before.Index, before.Source, before.Target};
288 Link afterLink{after.Index, after.Source, after.Target};
289 return handler(beforeLink, afterLink);
290 };
292 set_global<Signature>(callback);
293 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
294 {
295 return ByteLinks_Delete(_ptr, restrictionPtr, restrictionLength,
297 }
298 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
299 {
300 return UInt16Links_Delete(_ptr, restrictionPtr, restrictionLength,
302 }
303 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
304 {
305 return UInt32Links_Delete(_ptr, restrictionPtr, restrictionLength,
307 }
308 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
309 {
310 return UInt64Links_Delete(_ptr, restrictionPtr, restrictionLength,
312 }
313 else
314 {
315 static_assert(
317 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
318 }
319 }
320
321 LinkAddressType Each(const LinkType& restriction, const ReadHandlerType& handler) const
322 {
323 using Signature = LinkAddressType(Link<LinkAddressType>);
324 auto restrictionLength = std::ranges::size(restriction);
325 auto restrictionPtr = std::ranges::data(restriction);
326 auto callback = [&](Link<LinkAddressType> link) {
327 return handler(Link{link.Index, link.Source, link.Target});
328 };
329 set_global<Signature>(callback);
330 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
331 {
332 return ByteLinks_Each(_ptr, restrictionPtr, restrictionLength,
334 }
335 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
336 {
337 return UInt16Links_Each(_ptr, restrictionPtr, restrictionLength,
339 }
340 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
341 {
342 return UInt32Links_Each(_ptr, restrictionPtr, restrictionLength,
344 }
345 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
346 {
347 return UInt64Links_Each(_ptr, restrictionPtr, restrictionLength,
349 }
350 else
351 {
352 static_assert(
354 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
355 }
356 }
357
358 LinkAddressType Count(const LinkType& restriction) const
359 {
360 auto restrictionLength = std::ranges::size(restriction);
361 auto restrictionPtr = std::ranges::data(restriction);
362 if constexpr (std::same_as<LinkAddressType, std::uint8_t>)
363 {
364 return ByteLinks_Count(_ptr, restrictionPtr, restrictionLength);
365 }
366 else if constexpr (std::same_as<LinkAddressType, std::uint16_t>)
367 {
368 return UInt16Links_Count(_ptr, restrictionPtr, restrictionLength);
369 }
370 else if constexpr (std::same_as<LinkAddressType, std::uint32_t>)
371 {
372 return UInt32Links_Count(_ptr, restrictionPtr, restrictionLength);
373 }
374 else if constexpr (std::same_as<LinkAddressType, std::uint64_t>)
375 {
376 return UInt64Links_Count(_ptr, restrictionPtr, restrictionLength);
377 }
378 else
379 {
380 static_assert(
382 "LinkAddressType must be one of std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t");
383 }
384 }
385
386 // Extensions
387 };
388} // namespace Platform::Data::Doublets::Ffi
typename LinksOptionsType::WriteHandlerType WriteHandlerType
Definition LinksBase.h:133
TLinkOptions LinksOptionsType
Definition LinksBase.h:130
LinkAddressType Update(const LinkType &restriction, const LinkType &substitution, const WriteHandlerType &handler)
Definition LinksBase.h:240
typename LinksOptionsType::ReadHandlerType ReadHandlerType
Definition LinksBase.h:134
LinkAddressType Count(const LinkType &restriction) const
Definition LinksBase.h:358
~LinksBase()
Definition LinksBase.h:163
static constexpr LinksConstants< LinkAddressType > Constants
Definition LinksBase.h:135
LinkAddressType Each(const LinkType &restriction, const ReadHandlerType &handler) const
Definition LinksBase.h:321
typename LinksOptionsType::LinkAddressType LinkAddressType
Definition LinksBase.h:131
LinkAddressType Delete(const LinkType &restriction, const WriteHandlerType &handler)
Definition LinksBase.h:282
LinksBase(std::string_view path)
Definition LinksBase.h:137
LinkAddressType Create(const LinkType &substitution, const WriteHandlerType &handler)
Definition LinksBase.h:201
typename LinksOptionsType::LinkType LinkType
Definition LinksBase.h:132
Definition Links.h:2
uint32_t UInt32Links_Each(void *this_, const uint32_t *query, uintptr_t len, EachCallback< uint32_t > callback)
uint64_t UInt64Links_Delete(void *this_, const uint64_t *query, uintptr_t len, CUDCallback< uint64_t > callback)
uint64_t UInt64Links_Count(void *this_, const uint64_t *query, uintptr_t len)
thread_local std::function< Signature > GLOBAL_FUNCTION
Definition LinksBase.h:5
void ByteLinks_Drop(void *this_)
uint8_t ByteLinks_Create(void *this_, const uint8_t *query, uintptr_t len, CUDCallback< uint8_t > callback)
void set_global(std::function< Signature > function)
Definition LinksBase.h:15
uint64_t UInt64Links_Create(void *this_, const uint64_t *query, uintptr_t len, CUDCallback< uint64_t > callback)
uint16_t UInt16Links_Update(void *this_, const uint16_t *restriction, uintptr_t len_r, const uint16_t *substitution, uintptr_t len_s, CUDCallback< uint16_t > callback)
FfiConstants< uint16_t > UInt16Links_GetConstants(void *this_)
uint64_t UInt64Links_Update(void *this_, const uint64_t *restriction, uintptr_t len_r, const uint64_t *substitution, uintptr_t len_s, CUDCallback< uint64_t > callback)
uint16_t UInt16Links_Create(void *this_, const uint16_t *query, uintptr_t len, CUDCallback< uint16_t > callback)
uint32_t UInt32Links_Count(void *this_, const uint32_t *query, uintptr_t len)
uint16_t UInt16Links_Delete(void *this_, const uint16_t *query, uintptr_t len, CUDCallback< uint16_t > callback)
uint8_t ByteLinks_Count(void *this_, const uint8_t *query, uintptr_t len)
uint16_t UInt16Links_Count(void *this_, const uint16_t *query, uintptr_t len)
FfiConstants< uint32_t > UInt32Links_GetConstants(void *this_)
void UInt64Links_Drop(void *this_)
void * UInt32Links_New(const char *path)
uint16_t UInt16Links_Each(void *this_, const uint16_t *query, uintptr_t len, EachCallback< uint16_t > callback)
LinkAddressType(*)(Link< LinkAddressType > before, Link< LinkAddressType > after) CUDCallback
Definition LinksBase.h:21
TReturn call_last_global(TArgs... args)
Definition LinksBase.h:8
FfiConstants< uint64_t > UInt64Links_GetConstants(void *this_)
void * UInt64Links_New(const char *path)
uint64_t UInt64Links_Each(void *this_, const uint64_t *query, uintptr_t len, EachCallback< uint64_t > callback)
LinkAddressType(*)(Link< LinkAddressType >) EachCallback
Definition LinksBase.h:24
uint8_t ByteLinks_Each(void *this_, const uint8_t *query, uintptr_t len, EachCallback< uint8_t > callback)
FfiConstants< uint8_t > ByteLinks_GetConstants(void *this_)
uint8_t ByteLinks_Update(void *this_, const uint8_t *restriction, uintptr_t len_r, const uint8_t *substitution, uintptr_t len_s, CUDCallback< uint8_t > callback)
uint32_t UInt32Links_Create(void *this_, const uint32_t *query, uintptr_t len, CUDCallback< uint32_t > callback)
void UInt16Links_Drop(void *this_)
uint32_t UInt32Links_Delete(void *this_, const uint32_t *query, uintptr_t len, CUDCallback< uint32_t > callback)
uint32_t UInt32Links_Update(void *this_, const uint32_t *restriction, uintptr_t len_r, const uint32_t *substitution, uintptr_t len_s, CUDCallback< uint32_t > callback)
void * ByteLinks_New(const char *path)
void * UInt16Links_New(const char *path)
uint8_t ByteLinks_Delete(void *this_, const uint8_t *query, uintptr_t len, CUDCallback< uint8_t > callback)
void UInt32Links_Drop(void *this_)
LinkAddressType target_part
Definition LinksBase.h:31
bool _opt_marker
Definition LinksBase.h:41
LinkAddressType any
Definition LinksBase.h:36
LinkAddressType $break
Definition LinksBase.h:34
LinkAddressType error
Definition LinksBase.h:38
LinkAddressType null
Definition LinksBase.h:32
Ranges::Range< LinkAddressType > external_range
Definition LinksBase.h:40
LinkAddressType skip
Definition LinksBase.h:35
Ranges::Range< LinkAddressType > internal_range
Definition LinksBase.h:39
LinkAddressType $continue
Definition LinksBase.h:33
LinkAddressType itself
Definition LinksBase.h:37
LinkAddressType source_part
Definition LinksBase.h:30
LinkAddressType index_part
Definition LinksBase.h:29
Definition LinksBase.h:119
static constexpr bool value
Definition LinksBase.h:120