6#include <unordered_map>
11 using WeakPtr = std::weak_ptr<StructType>;
17 : shared_struct(nullptr) { }
19 : shared_struct(struct_ptr, deleteFunc) {
24 : shared_struct(std::move(other.shared_struct)) { }
29 shared_struct = shared_ptr;
30 if ((shared_ptr.get() !=
nullptr) && !
is_cached(shared_ptr.get())) {
36 shared_struct = std::move(other.shared_struct);
41 : shared_struct(other.shared_struct) { }
44 shared_struct = other.shared_struct;
48 operator StructType*()
const {
return shared_struct.get(); }
50 shared_struct.reset();
53 operator bool()
const {
return shared_struct !=
nullptr; }
54 StructType*
operator->()
const {
return shared_struct.get(); }
65 return cache.find(ptr) != cache.end() && !cache[ptr].expired();
89 return cache[ptr].lock();
92 cache[ptr] = new_shared;
std::unordered_map< StructType *, WeakPtr > SharedCacheMap
Type alias for the cache map (std::unordered_map<StructType*, WeakPtr>).
static SharedPtr get_or_cache(StructType *ptr, DeleteFunc deleteFunc)
Get or create an owning reference to a C pointer.
std::shared_ptr< StructType > SharedPtr
Type alias for std::shared_ptr<StructType>.
SharedPtrWrapper(std::nullptr_t)
Construct a null wrapper (explicit nullptr initialization).
static SharedCacheMap & get_shared_weak_ptr_cache()
static void null_deleter(StructType *)
SharedPtrWrapper & operator=(SharedPtrWrapper &&other) noexcept
Move assignment operator.
std::function< void(StructType *)> DeleteFunc
Type alias for deletion function (std::function<void(StructType*)>).
SharedPtrWrapper(SharedPtrWrapper &&other) noexcept
Move constructor.
static bool is_cached(StructType *ptr)
SharedPtrWrapper & operator=(const SharedPtrWrapper &other)
Copy assignment operator.
SharedPtrWrapper()=delete
static SharedPtr get_or_view(StructType *ptr)
Get or create a non-owning reference to a C pointer.
SharedPtrWrapper(StructType *struct_ptr, DeleteFunc deleteFunc)
Construct from a raw pointer and deletion function.
StructType * operator->() const
Arrow operator for member access.
std::weak_ptr< StructType > WeakPtr
Type alias for std::weak_ptr<StructType>.
SharedPtrWrapper(SharedPtr shared_ptr)
Existing Shared Pointer.
SharedPtrWrapper & operator=(std::nullptr_t)
Assign nullptr to release ownership.
SharedPtrWrapper(const SharedPtrWrapper &other)
Copy constructor.