added object_proxy as the holder of the object in object_ptr

This commit is contained in:
Sascha Kühl 2025-02-22 15:39:38 +01:00
parent d0b3ce4231
commit 35f078bbc4
5 changed files with 46 additions and 18 deletions

View File

@ -81,14 +81,23 @@ public:
template<class Pointer>
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
if (const auto result = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type)))) {
columns_.push_back(fk_column_generator_.generate(id, *x, *result));
}
on_foreign_key(id, x);
}
template<class Pointer>
void on_has_one(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
on_foreign_key(id, x);
}
template <class Pointer>
void on_foreign_key(const char *id, Pointer &x) {
if (const auto result = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type)))) {
if (x.empty()) {
typename Pointer::value_type temp_val;
columns_.push_back(fk_column_generator_.generate(id, temp_val, *result));
} else {
columns_.push_back(fk_column_generator_.generate(id, *x, *result));
}
}
}
template<class ContainerType>

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_{};
};
}

View File

@ -3,6 +3,7 @@
#include "matador/utils/identifier.hpp"
#include "matador/object/object_proxy.hpp"
#include "matador/object/primary_key_resolver.hpp"
#include <memory>
@ -11,35 +12,36 @@ namespace matador::object {
template <typename Type>
class object_ptr {
public:
object_ptr() = default;
object_ptr()
: ptr_(std::make_shared<object_proxy<Type>>()) {}
explicit object_ptr(Type *obj)
: ptr_(obj)
: ptr_(std::make_shared<object_proxy<Type>>(obj))
, pk_(primary_key_resolver::resolve_object(*obj).pk) {}
explicit object_ptr(std::shared_ptr<Type> obj) : ptr_(obj) {}
object_ptr(const object_ptr &other) : ptr_(other.ptr_) {}
object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {}
object_ptr &operator=(const object_ptr &other) = default;
object_ptr &operator=(object_ptr &&other) = default;
using value_type = Type;
Type* operator->() const { return ptr_.get(); }
Type& operator*() const { return *ptr_; }
Type* operator->() const { return ptr_->pointer(); }
Type& operator*() { return ptr_->ref(); }
const Type& operator*() const { return ptr_->ref(); }
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
[[nodiscard]] bool empty() const { return ptr_->pointer() == nullptr; }
void reset(Type *obj) {
ptr_.reset(obj);
ptr_->reset(obj);
pk_ = primary_key_resolver::resolve_object(*obj).pk;
}
Type* get() { return ptr_.get(); }
const Type* get() const { return ptr_.get(); }
Type* get() const { return static_cast<Type*>(ptr_->pointer()); }
operator bool() { return valid(); }
bool valid() { return ptr_ != nullptr; }
[[nodiscard]] const utils::identifier& primary_key() const { return pk_; }
void primary_key(const utils::identifier &pk) { pk_ = pk; }
private:
std::shared_ptr<Type> ptr_{};
std::shared_ptr<object_proxy<Type>> ptr_{};
utils::identifier pk_;
};

View File

@ -63,7 +63,7 @@ public:
if (!obj->get()) {
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
}
return utils::ok(object::object_ptr<Type>{ obj.release() });
return utils::ok(object::object_ptr<Type>{ obj->release() });
}
template<typename Type>

View File

@ -3,6 +3,7 @@
#include "matador/object/primary_key_resolver.hpp"
#include "../test/models/book.hpp"
#include "../test/models/author.hpp"
#include "../test/models/product.hpp"
TEST_CASE("Test primary key resolver", "[PrimaryKeyResolver]") {