80 lines
2.4 KiB
C++
80 lines
2.4 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() const { return valid(); }
|
|
[[nodiscard]] bool valid() const { 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); }
|
|
|
|
[[nodiscard]] bool is_persistent() const { return proxy_->is_persistent(); }
|
|
[[nodiscard]] bool is_transient() const { return !proxy_->is_transient(); }
|
|
[[nodiscard]] bool is_detached() const { return !proxy_->is_detached(); }
|
|
[[nodiscard]] bool is_removed() const { return !proxy_->is_removed(); }
|
|
|
|
void change_state(object_state s) {
|
|
if (proxy_) {
|
|
proxy_->change_state(s);
|
|
}
|
|
}
|
|
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 {
|
|
};
|
|
|
|
template<class Type, typename... Args>
|
|
object_ptr<Type> make_object(Args &&... args) {
|
|
return object_ptr<Type>(std::make_shared<Type>(std::forward<Args>(args)...));
|
|
}
|
|
}
|
|
|
|
#endif //OBJECT_PTR_HPP
|