added object_proxy as the holder of the object in object_ptr

This commit is contained in:
2025-02-22 15:39:38 +01:00
parent d0b3ce4231
commit 35f078bbc4
5 changed files with 46 additions and 18 deletions
+19 -3
View File
@@ -9,17 +9,33 @@ class basic_object_proxy {
public:
virtual ~basic_object_proxy() = default;
virtual void *get() const = 0;
[[nodiscard]] virtual void *get() const = 0;
};
template<class Type>
class object_proxy : public basic_object_proxy {
public:
void *get() const override { return static_cast<Type *>(this)->get(); }
object_proxy() = default;
explicit object_proxy(Type* obj)
: obj_(obj) {}
explicit object_proxy(std::unique_ptr<Type> obj)
: obj_(std::move(obj)) {}
[[nodiscard]] void *get() const override { return static_cast<void*>(obj_.get()); }
Type* operator->() const { return obj_.get(); }
Type& operator*() { return *obj_; }
const Type& operator*() const { return *obj_; }
Type* pointer() const { return obj_.get(); }
Type& ref() { return *obj_; }
const Type& ref() const { return *obj_; }
void reset(Type* obj) { obj_.reset(obj); }
private:
std::shared_ptr<Type> obj_{};
std::unique_ptr<Type> obj_{};
};
}