implemented lazy loading for object_ptr and belongs_to

This commit is contained in:
2026-01-15 12:40:54 +01:00
parent db8e137c11
commit 7626331866
81 changed files with 1790 additions and 927 deletions
+32 -23
View File
@@ -12,44 +12,53 @@ template <typename Type>
class object_ptr {
public:
object_ptr()
: ptr_(std::make_shared<object_proxy<Type>>()) {}
explicit object_ptr(Type *obj)
: ptr_(std::make_shared<object_proxy<Type>>(obj)) {}
object_ptr(const object_ptr &other) : ptr_(other.ptr_) {}
object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {}
: proxy_(std::make_shared<object_proxy<Type>>()) {}
explicit object_ptr(std::shared_ptr<Type> obj)
: proxy_(std::make_shared<object_proxy<Type>>(obj)) {}
explicit object_ptr(std::shared_ptr<object_proxy<Type>> obj)
: proxy_(std::move(obj)) {}
object_ptr(const object_ptr &other) = default;
object_ptr(object_ptr &&other) noexcept = default;
object_ptr &operator=(const object_ptr &other) = default;
object_ptr &operator=(object_ptr &&other) = default;
using value_type = Type;
Type* operator->() const { return ptr_->pointer(); }
Type& operator*() { return ptr_->ref(); }
const Type& operator*() const { return ptr_->ref(); }
bool operator==(const object_ptr &other) const {
return get() == other.get();
}
bool operator!=(const object_ptr &other) const { return !operator==(other); }
[[nodiscard]] bool empty() const { return ptr_->pointer() == nullptr; }
void reset(Type *obj) {
ptr_->reset(obj);
using value_type = Type;
Type *operator->() const { return get(); }
Type &operator*() { return *get(); }
const Type &operator*() const { return *get(); }
[[nodiscard]] bool empty() const { return get() == nullptr; }
Type *get() const {
return proxy_ ? proxy_->pointer() : nullptr;
}
Type* get() const { return static_cast<Type*>(ptr_->pointer()); }
void reset() { proxy_.reset(); }
operator bool() { return valid(); }
bool valid() { return ptr_ != nullptr; }
bool valid() { return proxy_ != nullptr; }
[[nodiscard]] bool has_primary_key() const { return proxy_->has_primary_key(); }
[[nodiscard]] const utils::identifier &primary_key() const { return proxy_->primary_key(); }
void primary_key(const utils::identifier &pk) { proxy_->primary_key(pk); }
[[nodiscard]] bool has_primary_key() const { return ptr_->has_primary_key(); }
[[nodiscard]] const utils::identifier& primary_key() const { return ptr_->primary_key(); }
void primary_key(const utils::identifier &pk) { ptr_->primary_key(pk); }
private:
std::shared_ptr<object_proxy<Type>> ptr_{};
std::shared_ptr<object_proxy<Type> > proxy_{};
};
template<typename>
struct is_object_ptr : std::false_type
{};
struct is_object_ptr : std::false_type {
};
template<typename Type>
struct is_object_ptr<object_ptr<Type>> : std::true_type
{};
struct is_object_ptr<object_ptr<Type> > : std::true_type {
};
}
#endif //OBJECT_PTR_HPP