proxy lazy resolve progress

This commit is contained in:
Sascha Kühl 2025-08-21 16:04:40 +02:00
parent f1323accda
commit 839a214cef
2 changed files with 53 additions and 23 deletions

View File

@ -2,7 +2,6 @@
#define OBJECT_PROXY_HPP #define OBJECT_PROXY_HPP
#include <memory> #include <memory>
#include <functional>
namespace matador::object { namespace matador::object {
@ -13,14 +12,43 @@ public:
[[nodiscard]] virtual void *get() const = 0; [[nodiscard]] virtual void *get() const = 0;
}; };
template < typename Type >
class object_proxy;
template < typename Type >
class object_resolver {
public:
virtual ~object_resolver() = default;
virtual Type* resolve(const object_proxy<Type> &proxy) const = 0;
};
template < typename Type >
class simple_object_resolver final : public object_resolver<Type> {
public:
simple_object_resolver() = default;
explicit simple_object_resolver(Type* obj)
: obj_(obj) {}
explicit simple_object_resolver(std::unique_ptr<Type> obj)
: obj_(std::move(obj)) {}
Type* resolve(const object_proxy<Type> &/*proxy*/) const override {
return obj_.get();
}
private:
std::unique_ptr<Type> obj_{};
};
template<class Type> template<class Type>
class object_proxy final : public basic_object_proxy { class object_proxy final : public basic_object_proxy {
public: public:
object_proxy() = default; object_proxy() = default;
explicit object_proxy(Type* obj) explicit object_proxy(Type* obj)
: obj_(obj) {} : resolver_(std::make_unique<simple_object_resolver<Type>>(obj)) {}
explicit object_proxy(std::unique_ptr<Type> obj) explicit object_proxy(std::unique_ptr<Type> obj)
: obj_(std::move(obj)) {} : resolver_(std::move(obj)) {}
[[nodiscard]] void *get() const override { return static_cast<void*>(pointer()); } [[nodiscard]] void *get() const override { return static_cast<void*>(pointer()); }
@ -33,16 +61,23 @@ public:
Type& ref() { return *pointer(); } Type& ref() { return *pointer(); }
const Type& ref() const { return *pointer(); } const Type& ref() const { return *pointer(); }
void reset(Type* obj) { obj_.reset(obj); } void reset(Type* obj) {
resolver_ = std::make_unique<simple_object_resolver<Type>>(obj);
private: }
Type* resolve() const { void reset(std::unique_ptr<Type> obj) {
return resolver_(*this); resolver_ = std::make_unique<simple_object_resolver<Type>>(std::move(obj));
}
void reset(std::unique_ptr<object_resolver<Type>> &&resolver) {
resolver_ = std::move(resolver);
} }
private: private:
std::function<Type*(const object_proxy&)> resolver_{[](const object_proxy &proxy){ return proxy.obj_.get();}}; Type* resolve() const {
std::unique_ptr<Type> obj_{}; return resolver_->resolve(*this);
}
private:
std::unique_ptr<object_resolver<Type>> resolver_{std::make_unique<simple_object_resolver<Type>>()};
}; };
} }

View File

@ -39,30 +39,28 @@ public:
template<typename ValueType> template<typename ValueType>
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes); void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) { ++column_index_; } void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {
++column_index_;
}
template<class Type> template<class Type>
void on_attribute(const char * /*id*/, Type &/*x*/, void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {
const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; } ++column_index_;
}
template<class Pointer> template<class Pointer>
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
++column_index_; ++column_index_;
} }
template<class Pointer> template<class Pointer>
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
++column_index_; ++column_index_;
} }
template<class ContainerType> template<class ContainerType>
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType> template<class ContainerType>
static void on_has_many_to_many(const char *id, ContainerType &c, const char * /*join_column*/, static void on_has_many_to_many(const char *id, ContainerType &c, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType> template<class ContainerType>
static void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {} static void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
@ -142,9 +140,6 @@ public:
} }
} }
// template<class ContainerType>
// void on_has_many(const char *, ContainerType &, const utils::foreign_attributes &/*attr*/) {}
template<class Type> template<class Type>
void bind(const Type &obj) { void bind(const Type &obj) {
reader_->bind(obj); reader_->bind(obj);