hdk-grid 0.0.1
Header-only foundational library for the Hollow Development Kit.
Loading...
Searching...
No Matches
SharedPtrWrapper.hpp
Go to the documentation of this file.
1#pragma once
4#include <functional>
5#include <memory>
6#include <unordered_map>
7namespace hdk::grid {
8 template <typename StructType> class SharedPtrWrapper {
9 public:
10 using SharedPtr = std::shared_ptr<StructType>;
11 using WeakPtr = std::weak_ptr<StructType>;
12 using SharedCacheMap = std::unordered_map<StructType*, WeakPtr>;
13 using DeleteFunc = std::function<void(StructType*)>;
14 static void null_deleter(StructType*) { }
15 SharedPtrWrapper() = delete;
16 SharedPtrWrapper(std::nullptr_t)
17 : shared_struct(nullptr) { }
18 SharedPtrWrapper(StructType* struct_ptr, DeleteFunc deleteFunc)
19 : shared_struct(struct_ptr, deleteFunc) {
20 // Cache the shared pointer to ensure consistent reference counting for the same raw pointer.
21 get_shared_weak_ptr_cache()[struct_ptr] = shared_struct;
22 }
24 : shared_struct(std::move(other.shared_struct)) { }
25
27
28 {
29 shared_struct = shared_ptr;
30 if ((shared_ptr.get() != nullptr) && !is_cached(shared_ptr.get())) {
31 get_shared_weak_ptr_cache()[shared_ptr.get()] = shared_struct;
32 }
33 }
35 if (this != &other) {
36 shared_struct = std::move(other.shared_struct);
37 }
38 return *this;
39 }
41 : shared_struct(other.shared_struct) { }
43 if (this != &other) {
44 shared_struct = other.shared_struct;
45 }
46 return *this;
47 }
48 operator StructType*() const { return shared_struct.get(); }
49 SharedPtrWrapper& operator=(std::nullptr_t) {
50 shared_struct.reset();
51 return *this;
52 }
53 operator bool() const { return shared_struct != nullptr; }
54 StructType* operator->() const { return shared_struct.get(); }
55 private: // internal use only.
56 SharedPtr shared_struct { nullptr };
57
58 protected: // For use by derived classes.
60 static SharedCacheMap cache_map;
61 return cache_map;
62 }
63 static bool is_cached(StructType* ptr) {
64 auto& cache = get_shared_weak_ptr_cache();
65 return cache.find(ptr) != cache.end() && !cache[ptr].expired();
66 }
67 static SharedPtr get_or_view(StructType* ptr) {
69 if (!ptr) {
71 return SharedPtr(nullptr, null_deleter);
72 }
73
74 if (is_cached(ptr)) {
75 return get_shared_weak_ptr_cache()[ptr].lock();
76 } else {
77 SharedPtr new_shared(ptr, null_deleter);
78 // cache[ptr] = new_shared;
79 return new_shared;
80 }
81 }
82 static SharedPtr get_or_cache(StructType* ptr, DeleteFunc deleteFunc) {
84 if (!ptr) {
85 return SharedPtr(nullptr, deleteFunc);
86 }
87 auto& cache = get_shared_weak_ptr_cache();
88 if (is_cached(ptr)) {
89 return cache[ptr].lock();
90 } else {
91 SharedPtr new_shared(ptr, deleteFunc);
92 cache[ptr] = new_shared;
93 return new_shared;
94 }
95 }
96 };
97}
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.
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.