Reference-counted wrapper for C structures using std::shared_ptr. More...
#include <SharedPtrWrapper.hpp>
Public Types | |
| using | SharedPtr = std::shared_ptr<StructType> |
| Type alias for std::shared_ptr<StructType>. | |
| using | WeakPtr = std::weak_ptr<StructType> |
| Type alias for std::weak_ptr<StructType>. | |
| using | SharedCacheMap = std::unordered_map<StructType*, WeakPtr> |
| Type alias for the cache map (std::unordered_map<StructType*, WeakPtr>). | |
| using | DeleteFunc = std::function<void(StructType*)> |
| Type alias for deletion function (std::function<void(StructType*)>). | |
Public Member Functions | |
| SharedPtrWrapper ()=delete | |
| SharedPtrWrapper (std::nullptr_t) | |
| Construct a null wrapper (explicit nullptr initialization). | |
| SharedPtrWrapper (StructType *struct_ptr, DeleteFunc deleteFunc) | |
| Construct from a raw pointer and deletion function. | |
| SharedPtrWrapper (SharedPtrWrapper &&other) noexcept | |
| Move constructor. | |
| SharedPtrWrapper (SharedPtr shared_ptr) | |
| Existing Shared Pointer. | |
| SharedPtrWrapper & | operator= (SharedPtrWrapper &&other) noexcept |
| Move assignment operator. | |
| SharedPtrWrapper (const SharedPtrWrapper &other) | |
| Copy constructor. | |
| SharedPtrWrapper & | operator= (const SharedPtrWrapper &other) |
| Copy assignment operator. | |
| operator StructType * () const | |
| Implicit conversion to raw C pointer. | |
| SharedPtrWrapper & | operator= (std::nullptr_t) |
| Assign nullptr to release ownership. | |
| operator bool () const | |
| Check if the wrapper holds a valid pointer. | |
| StructType * | operator-> () const |
| Arrow operator for member access. | |
Static Public Member Functions | |
| static void | null_deleter (StructType *) |
Static Protected Member Functions | |
| static SharedCacheMap & | get_shared_weak_ptr_cache () |
| static bool | is_cached (StructType *ptr) |
| static SharedPtr | get_or_view (StructType *ptr) |
| Get or create a non-owning reference to a C pointer. | |
| static SharedPtr | get_or_cache (StructType *ptr, DeleteFunc deleteFunc) |
| Get or create an owning reference to a C pointer. | |
Reference-counted wrapper for C structures using std::shared_ptr.
| StructType | The C structure type being wrapped |
SharedPtrWrapper provides automatic lifetime management for C-style structures through reference counting. This template is the foundation for building C++-style wrappers around C APIs while maintaining compatibility with the underlying C code through implicit pointer conversion.
Key features:
The template maintains an internal cache of weak pointers to ensure that the same C structure pointer always maps to the same shared_ptr instance, preventing double-deletion and ensuring consistent reference counting.
Definition at line 8 of file SharedPtrWrapper.hpp.
| using hdk::grid::SharedPtrWrapper< StructType >::DeleteFunc = std::function<void(StructType*)> |
Type alias for deletion function (std::function<void(StructType*)>).
The deletion function is called when the reference count reaches zero. Typically this is a C API cleanup function like SDL_DestroyWindow.
Definition at line 13 of file SharedPtrWrapper.hpp.
| using hdk::grid::SharedPtrWrapper< StructType >::SharedCacheMap = std::unordered_map<StructType*, WeakPtr> |
Type alias for the cache map (std::unordered_map<StructType*, WeakPtr>).
The cache ensures that multiple wrappers of the same raw pointer share the same shared_ptr instance, preventing reference counting errors.
Definition at line 12 of file SharedPtrWrapper.hpp.
| using hdk::grid::SharedPtrWrapper< StructType >::SharedPtr = std::shared_ptr<StructType> |
Type alias for std::shared_ptr<StructType>.
Definition at line 10 of file SharedPtrWrapper.hpp.
| using hdk::grid::SharedPtrWrapper< StructType >::WeakPtr = std::weak_ptr<StructType> |
Type alias for std::weak_ptr<StructType>.
Definition at line 11 of file SharedPtrWrapper.hpp.
|
delete |
Referenced by operator=(), operator=(), operator=(), SharedPtrWrapper(), and SharedPtrWrapper().
|
inline |
Construct a null wrapper (explicit nullptr initialization).
Initializes the wrapper to hold a null pointer.
Definition at line 16 of file SharedPtrWrapper.hpp.
|
inline |
Construct from a raw pointer and deletion function.
Creates a wrapper that takes ownership of the provided pointer and uses the specified deletion function for cleanup when the reference count reaches zero.
| StructType | The structure type |
| struct_ptr | The raw pointer to wrap |
| deleteFunc | The function to call for cleanup (e.g., SDL_DestroyWindow, SDL_DestroyRenderer) |
Definition at line 18 of file SharedPtrWrapper.hpp.
References get_shared_weak_ptr_cache().
|
inlinenoexcept |
Move constructor.
Efficiently transfers ownership from another wrapper instance.
| other | The source wrapper (will be moved from) |
Definition at line 23 of file SharedPtrWrapper.hpp.
References SharedPtrWrapper().
|
inline |
Existing Shared Pointer.
Construct from an existing shared_ptr.
Creates a wrapper around an already-created shared_ptr, which is useful when working with already-managed pointers or integrating with other code that uses shared_ptr.
| shared_ptr | An existing std::shared_ptr to wrap |
Definition at line 26 of file SharedPtrWrapper.hpp.
References get_shared_weak_ptr_cache(), and is_cached().
|
inline |
Copy constructor.
Creates a new wrapper that shares ownership of the same structure. Both instances will increment the reference count and share the lifetime of the underlying structure.
| other | The source wrapper to copy from |
Definition at line 40 of file SharedPtrWrapper.hpp.
References SharedPtrWrapper().
|
inlinestaticprotected |
Get or create an owning reference to a C pointer.
This protected method is used by derived classes to create owning references to C pointers. If the pointer is already in the cache, returns the cached shared_ptr. Otherwise, creates a new shared_ptr with the provided deletion function and caches it.
This ensures that the same C pointer always maps to the same shared_ptr instance, preventing reference counting errors.
| ptr | The raw pointer to wrap |
| deleteFunc | The function to call when the reference count reaches zero |
Example from derived class:
If no ptr, no need to do anything else
Definition at line 82 of file SharedPtrWrapper.hpp.
References get_shared_weak_ptr_cache(), and is_cached().
|
inlinestaticprotected |
Get or create a non-owning reference to a C pointer.
This protected method is used by derived classes to create references to C pointers without taking ownership. If the pointer is already in the cache, returns the cached shared_ptr. Otherwise, returns a new non-owning shared_ptr (with null_deleter).
This is useful when:
| ptr | The raw pointer to wrap |
Example from derived class:
if no pointer
then return a non-owning null
Definition at line 67 of file SharedPtrWrapper.hpp.
References get_shared_weak_ptr_cache(), is_cached(), and null_deleter().
|
inlinestaticprotected |
Definition at line 59 of file SharedPtrWrapper.hpp.
Referenced by get_or_cache(), get_or_view(), is_cached(), SharedPtrWrapper(), and SharedPtrWrapper().
|
inlinestaticprotected |
Definition at line 63 of file SharedPtrWrapper.hpp.
References get_shared_weak_ptr_cache().
Referenced by get_or_cache(), get_or_view(), and SharedPtrWrapper().
|
inlinestatic |
Definition at line 14 of file SharedPtrWrapper.hpp.
Referenced by get_or_view().
|
inline |
Check if the wrapper holds a valid pointer.
Example:
Definition at line 53 of file SharedPtrWrapper.hpp.
|
inline |
Implicit conversion to raw C pointer.
Allows the wrapper to be used anywhere a StructType* is expected, enabling seamless compatibility with C APIs.
Example:
Definition at line 48 of file SharedPtrWrapper.hpp.
|
inline |
Arrow operator for member access.
Allows access to structure members through the -> operator.
Example:
Definition at line 54 of file SharedPtrWrapper.hpp.
|
inline |
Copy assignment operator.
| other | The source wrapper to copy from |
Definition at line 42 of file SharedPtrWrapper.hpp.
References SharedPtrWrapper().
|
inlinenoexcept |
Move assignment operator.
| other | The source wrapper (will be moved from) |
Definition at line 34 of file SharedPtrWrapper.hpp.
References SharedPtrWrapper().
|
inline |
Assign nullptr to release ownership.
Releases ownership of the current structure, decrementing the reference count. If this was the last reference, the structure is destroyed using the registered deletion function.
Definition at line 49 of file SharedPtrWrapper.hpp.
References SharedPtrWrapper().