proxy lazy resolve progress
This commit is contained in:
parent
f1323accda
commit
839a214cef
|
|
@ -2,7 +2,6 @@
|
|||
#define OBJECT_PROXY_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
|
|
@ -13,14 +12,43 @@ public:
|
|||
[[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>
|
||||
class object_proxy final : public basic_object_proxy {
|
||||
public:
|
||||
object_proxy() = default;
|
||||
explicit object_proxy(Type* obj)
|
||||
: obj_(obj) {}
|
||||
: resolver_(std::make_unique<simple_object_resolver<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()); }
|
||||
|
||||
|
|
@ -33,16 +61,23 @@ public:
|
|||
Type& ref() { return *pointer(); }
|
||||
const Type& ref() const { return *pointer(); }
|
||||
|
||||
void reset(Type* obj) { obj_.reset(obj); }
|
||||
|
||||
private:
|
||||
Type* resolve() const {
|
||||
return resolver_(*this);
|
||||
void reset(Type* obj) {
|
||||
resolver_ = std::make_unique<simple_object_resolver<Type>>(obj);
|
||||
}
|
||||
void reset(std::unique_ptr<Type> obj) {
|
||||
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:
|
||||
std::function<Type*(const object_proxy&)> resolver_{[](const object_proxy &proxy){ return proxy.obj_.get();}};
|
||||
std::unique_ptr<Type> obj_{};
|
||||
Type* resolve() const {
|
||||
return resolver_->resolve(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<object_resolver<Type>> resolver_{std::make_unique<simple_object_resolver<Type>>()};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,30 +39,28 @@ public:
|
|||
|
||||
template<typename ValueType>
|
||||
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>
|
||||
void on_attribute(const char * /*id*/, Type &/*x*/,
|
||||
const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; }
|
||||
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {
|
||||
++column_index_;
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
|
||||
++column_index_;
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
|
||||
++column_index_;
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
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*/) {}
|
||||
|
||||
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*/) {}
|
||||
template<class ContainerType>
|
||||
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>
|
||||
void bind(const Type &obj) {
|
||||
reader_->bind(obj);
|
||||
|
|
|
|||
Loading…
Reference in New Issue