libbio
Loading...
Searching...
No Matches
Pointer.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2026 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
31
32#ifndef NULL
33 #include <cstddef>
34#endif
35
36//@formatter:off
37#if BIO_CPP_VERSION >= 11
38 #include <type_traits>
39#endif
40#include <functional>
41//@formatter:on
42
43namespace bio {
44
45namespace pointer_detail {
46
62template < typename FROM, typename TO >
64{
65private:
66 struct Yes { char mPad[1]; };
67 struct No { char mPad[2]; };
68 static Yes Test(TO*);
69 static No Test(...);
70 // A FROM* value to feed the overload set. Never evaluated (sizeof context).
71 static FROM* MakeFrom();
72public:
73 static const bool sValue = (sizeof(Test(MakeFrom())) == sizeof(Yes));
74};
75
76} //pointer_detail namespace
77
113template < typename T >
115{
116public:
117 typedef T Type;
118
123 :
124 //@formatter:off
126 mHandle(0)
127 #else
128 mRaw(NULL)
129 #endif
130 //@formatter:on
131 {
132 }
133
147 :
148 //@formatter:off
150 mHandle(::bio::pointer_arena::HandleFor(raw))
151 #else
152 mRaw(raw)
153 #endif
154 //@formatter:on
155 {
156 }
157
169 template < typename U >
171 const Pointer< U >& other,
172 typename type::EnableIf<
174 int
175 >::Type = 0
176 )
177 :
178 //@formatter:off
180 mHandle(::bio::pointer_arena::HandleFor(static_cast< T* >(other.Get())))
181 #else
182 mRaw(other.Get())
183 #endif
184 //@formatter:on
185 {
186 }
187
188 // Copy ctor, copy assignment and dtor are intentionally left implicit
189 // (compiler-defaulted): a single T* member makes them a trivial bit-copy /
190 // no-op, which is what keeps Pointer< T > trivially-copyable and exactly the
191 // size/behavior of a raw T*. Declaring them would risk making the type
192 // non-trivial; do not.
193
200 {
201 //@formatter:off
202 #if BIO_COMPACT_POINTER_STORAGE
204 #else
205 mRaw = raw;
206 #endif
207 //@formatter:on
208 return *this;
209 }
210
218 template < typename U >
219 typename type::EnableIf<
221 Pointer&
223 {
224 //@formatter:off
225 #if BIO_COMPACT_POINTER_STORAGE
226 mHandle = ::bio::pointer_arena::HandleFor(static_cast< T* >(other.Get()));
227 #else
228 mRaw = other.Get();
229 #endif
230 //@formatter:on
231 return *this;
232 }
233
239 operator T*() const
240 {
241 return Get();
242 }
243
248 T* operator->() const
249 {
250 T* raw = Get();
251 ::bio::pointer_safety::AssertLive< T >(raw, __FILE__, __LINE__);
252 return raw;
253 }
254
259 T& operator*() const
260 {
261 T* raw = Get();
262 ::bio::pointer_safety::AssertLive< T >(raw, __FILE__, __LINE__);
263 return *raw;
264 }
265
271 T* Get() const
272 {
273 //@formatter:off
274 #if BIO_COMPACT_POINTER_STORAGE
275 return static_cast< T* >(const_cast< void* >(::bio::pointer_arena::ResolveFast(mHandle)));
276 #else
277 return mRaw;
278 #endif
279 //@formatter:on
280 }
281
282 //@formatter:off
283 #if BIO_COMPACT_POINTER_STORAGE
289 {
290 return mHandle;
291 }
292 #endif
293 //@formatter:on
294
295 //@formatter:off
296 #if BIO_CPP_VERSION >= 11
310 explicit operator bool() const
311 {
312 return Get() != NULL;
313 }
314 #endif
315 //@formatter:on
316
321 bool operator==(const Pointer& other) const
322 {
323 return Get() == other.Get();
324 }
325
326 template < typename U >
327 bool operator==(const Pointer< U >& other) const
328 {
329 return Get() == other.Get();
330 }
331
336 bool operator!=(const Pointer& other) const
337 {
338 return Get() != other.Get();
339 }
340
341 template < typename U >
342 bool operator!=(const Pointer< U >& other) const
343 {
344 return Get() != other.Get();
345 }
346
351 bool operator==(T* raw) const
352 {
353 return Get() == raw;
354 }
355
360 bool operator!=(T* raw) const
361 {
362 return Get() != raw;
363 }
364
371 bool operator<(const Pointer& other) const
372 {
373 return ::std::less< T* >()(Get(), other.Get());
374 }
375
376private:
377 //@formatter:off
378 #if BIO_COMPACT_POINTER_STORAGE
380 #else
381 T* mRaw;
382 #endif
383 //@formatter:on
384};
385
386//@formatter:off
387
393template < typename T >
394inline bool operator==(T* raw, const Pointer< T >& wrapped)
395{
396 return wrapped == raw;
397}
398
399template < typename T >
400inline bool operator!=(T* raw, const Pointer< T >& wrapped)
401{
402 return wrapped != raw;
403}
404
411template < typename T >
413{
414 delete wrapped.Get();
415}
416
417// NOTE: no dedicated std::nullptr_t comparison overloads. `p == nullptr`,
418// `nullptr != p`, `p == NULL`, `p == 0` all resolve via the T* comparisons
419// above (each null constant converts to T*), exactly as for a raw pointer —
420// verified at build time across cpp98..cpp20. Adding nullptr_t overloads only
421// widens the surface with no behavioral gain.
422
423//@formatter:on
424
425//@formatter:off
426#if BIO_CPP_VERSION >= 11
427 // PHASE-0 layout contract. Normal builds keep Pointer< T > byte-for-byte
428 // compatible with raw T*. The level-2 compact-pointer path is a source-only
429 // feasibility mode that swaps the storage to a 32-bit process-global handle.
430 // NB: assert on a COMPLETE pointee (int / double), never Pointer< void >:
431 // instantiating Pointer< void > would form the ill-formed `void& operator*()`
432 // return type in its signature. All object pointers are the same width, so
433 // sizeof(int*) == sizeof(void*) and the guarantee is fully covered.
434 #if BIO_COMPACT_POINTER_STORAGE
435 static_assert(
436 sizeof(Pointer< int >) == sizeof(uint32_t),
437 "compact Pointer< T > must be exactly one 32-bit handle wide.");
438 static_assert(
439 sizeof(Pointer< double >) == sizeof(uint32_t),
440 "compact Pointer< T > must be exactly one 32-bit handle wide.");
441 #else
442 static_assert(
443 sizeof(Pointer< int >) == sizeof(int*),
444 "Pointer< T > must be exactly one pointer wide (raw-pointer drop-in).");
445 static_assert(
446 sizeof(Pointer< double >) == sizeof(void*),
447 "Pointer< T > must be exactly one pointer wide (raw-pointer drop-in).");
448 #endif
449 static_assert(
450 ::std::is_standard_layout< Pointer< int > >::value,
451 "Pointer< T > must be standard-layout (raw-pointer drop-in).");
452 static_assert(
453 ::std::is_trivially_copyable< Pointer< int > >::value,
454 "Pointer< T > must be trivially-copyable / trivially-relocatable.");
455#endif
456//@formatter:on
457
458namespace type {
459
472template < typename T >
473struct IsMemcpySafeImplementation< Pointer< T > >
474{
475 static const bool sValue = true;
476};
477
498template < typename T >
499struct IsPointerImplementation< Pointer< T > >
500{
501 static const bool sValue = true;
502};
503
514template < typename T >
515struct RemovePointer< Pointer< T > >
516{
517 typedef T Type;
518};
519
520template < typename T >
521struct IsPointerWrapper< Pointer< T > >
522{
523 static const bool sValue = true;
524};
525
526} //type namespace
527
528} //bio namespace
#define BIO_COMPACT_POINTER_STORAGE
Definition OptimizeMacros.h:87
double value
Definition Potential.cpp:41
Definition Pointer.h:115
T & operator*() const
Definition Pointer.h:259
bool operator!=(const Pointer< U > &other) const
Definition Pointer.h:342
T * operator->() const
Definition Pointer.h:248
Pointer(T *raw)
Definition Pointer.h:146
Pointer()
Definition Pointer.h:122
bool operator==(const Pointer &other) const
Definition Pointer.h:321
T Type
Definition Pointer.h:117
T * Get() const
Definition Pointer.h:271
bool operator<(const Pointer &other) const
Definition Pointer.h:371
bool operator!=(const Pointer &other) const
Definition Pointer.h:336
bool operator!=(T *raw) const
Definition Pointer.h:360
bool operator==(const Pointer< U > &other) const
Definition Pointer.h:327
Pointer(const Pointer< U > &other, typename type::EnableIf< pointer_detail::IsPointerConvertible< U, T >::sValue, int >::Type=0)
Definition Pointer.h:170
type::EnableIf< pointer_detail::IsPointerConvertible< U, T >::sValue, Pointer & >::Type operator=(const Pointer< U > &other)
Definition Pointer.h:222
Pointer & operator=(T *raw)
Definition Pointer.h:199
bool operator==(T *raw) const
Definition Pointer.h:351
Handle HandleFor(const void *raw)
Definition PointerArena.cpp:2278
Definition FinalCell.h:29
void Delete(Pointer< T > wrapped)
Definition Pointer.h:412
bool operator!=(T *raw, const Pointer< T > &wrapped)
Definition Pointer.h:400
bool operator==(T *raw, const Pointer< T > &wrapped)
Definition Pointer.h:394
static const bool sValue
Definition Pointer.h:73
Definition EnableIf.h:34
static const bool sValue
Definition IsMemcpySafe.h:61
static const bool sValue
Definition IsPointer.h:38
static const bool sValue
Definition IsPointer.h:80
RemovePointerImplementation< T >::Type Type
Definition RemovePointer.h:54