65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
#ifndef OBJECT_PTR_HPP
|
|
#define OBJECT_PTR_HPP
|
|
|
|
#include "matador/utils/identifier.hpp"
|
|
|
|
#include "matador/object/object_proxy.hpp"
|
|
|
|
#include <memory>
|
|
|
|
namespace matador::object {
|
|
template <typename Type>
|
|
class object_ptr {
|
|
public:
|
|
object_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;
|
|
|
|
bool operator==(const object_ptr &other) const {
|
|
return get() == other.get();
|
|
}
|
|
bool operator!=(const object_ptr &other) const { return !operator==(other); }
|
|
|
|
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;
|
|
}
|
|
|
|
void reset() { proxy_.reset(); }
|
|
|
|
operator bool() { return valid(); }
|
|
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); }
|
|
|
|
private:
|
|
std::shared_ptr<object_proxy<Type> > proxy_{};
|
|
};
|
|
|
|
template<typename>
|
|
struct is_object_ptr : std::false_type {
|
|
};
|
|
|
|
template<typename Type>
|
|
struct is_object_ptr<object_ptr<Type> > : std::true_type {
|
|
};
|
|
}
|
|
|
|
#endif //OBJECT_PTR_HPP
|