query/include/matador/object/object_proxy.hpp

50 lines
1.2 KiB
C++

#ifndef OBJECT_PROXY_HPP
#define OBJECT_PROXY_HPP
#include <memory>
#include <functional>
namespace matador::object {
class basic_object_proxy {
public:
virtual ~basic_object_proxy() = default;
[[nodiscard]] virtual void *get() const = 0;
};
template<class Type>
class object_proxy final : public basic_object_proxy {
public:
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*>(pointer()); }
Type* operator->() const { return pointer(); }
Type& operator*() { return *pointer(); }
const Type& operator*() const { return *pointer(); }
Type* pointer() const { return resolve(); }
Type& ref() { return *pointer(); }
const Type& ref() const { return *pointer(); }
void reset(Type* obj) { obj_.reset(obj); }
private:
Type* resolve() const {
return resolver_(*this);
}
private:
std::function<Type*(const object_proxy&)> resolver_{[](const object_proxy &proxy){ return proxy.obj_.get();}};
std::unique_ptr<Type> obj_{};
};
}
#endif //OBJECT_PROXY_HPP