implemented lazy loading for object_ptr and belongs_to
This commit is contained in:
@@ -11,9 +11,7 @@
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
class identifier_serializer
|
||||
{
|
||||
class identifier_serializer {
|
||||
public:
|
||||
virtual ~identifier_serializer() = default;
|
||||
|
||||
@@ -25,7 +23,7 @@ public:
|
||||
virtual void serialize(uint16_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint32_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(uint64_t &, const field_attributes &) = 0;
|
||||
virtual void serialize(const char*, const field_attributes &) = 0;
|
||||
virtual void serialize(const char *, const field_attributes &) = 0;
|
||||
virtual void serialize(std::string &, const field_attributes &) = 0;
|
||||
virtual void serialize(null_type_t &, const field_attributes &) = 0;
|
||||
};
|
||||
@@ -34,13 +32,13 @@ template<typename Type, class Enabled = void>
|
||||
struct identifier_type_traits;
|
||||
|
||||
template<typename Type>
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_integral_v<Type>>> {
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_integral_v<Type> > > {
|
||||
static bool is_valid(Type value) { return value > 0; }
|
||||
static std::string to_string(const Type value) { return std::to_string(value); }
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_same_v<Type, std::string>>> {
|
||||
struct identifier_type_traits<Type, std::enable_if_t<std::is_same_v<Type, std::string> > > {
|
||||
static bool is_valid(const Type &value) { return !value.empty(); }
|
||||
static std::string to_string(const Type &value) { return value; }
|
||||
};
|
||||
@@ -52,27 +50,23 @@ struct identifier_type_traits<null_type_t, void> {
|
||||
};
|
||||
|
||||
template<>
|
||||
struct identifier_type_traits<const char*, void> {
|
||||
struct identifier_type_traits<const char *, void> {
|
||||
static bool is_valid(const char *value);
|
||||
static std::string to_string(const char *value);
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename Type>
|
||||
size_t hash(const Type &value) {
|
||||
return std::hash<Type>()(value);
|
||||
}
|
||||
|
||||
size_t hash(const char *value);
|
||||
|
||||
}
|
||||
|
||||
class identifier
|
||||
{
|
||||
class identifier {
|
||||
private:
|
||||
struct base
|
||||
{
|
||||
struct base {
|
||||
base(const std::type_index &ti, basic_type type);
|
||||
base(const base &x) = delete;
|
||||
base &operator=(const base &x) = delete;
|
||||
@@ -93,25 +87,25 @@ private:
|
||||
};
|
||||
|
||||
template<class IdType>
|
||||
struct pk final : base
|
||||
{
|
||||
struct pk final : base {
|
||||
using self = pk;
|
||||
|
||||
explicit pk(const IdType &id)
|
||||
: base(std::type_index(typeid(IdType)), data_type_traits<IdType>::type(1))
|
||||
, id_(id)
|
||||
, size_(sizeof(IdType)) {}
|
||||
, id_(id)
|
||||
, size_(sizeof(IdType)) {
|
||||
}
|
||||
|
||||
[[nodiscard]] base *copy() const override {
|
||||
return new self(id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool equal_to(const base &x) const override {
|
||||
return static_cast<const pk&>(x).id_ == id_;
|
||||
return static_cast<const pk &>(x).id_ == id_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool less(const base &x) const override {
|
||||
return id_ < static_cast<const pk&>(x).id_;
|
||||
return id_ < static_cast<const pk &>(x).id_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_valid() const override {
|
||||
@@ -134,8 +128,7 @@ private:
|
||||
size_t size_{};
|
||||
};
|
||||
|
||||
struct null_pk final : base
|
||||
{
|
||||
struct null_pk final : base {
|
||||
null_pk();
|
||||
[[nodiscard]] base *copy() const override;
|
||||
[[nodiscard]] bool equal_to(const base &x) const override;
|
||||
@@ -149,26 +142,29 @@ private:
|
||||
|
||||
public:
|
||||
identifier();
|
||||
|
||||
template<typename Type>
|
||||
explicit identifier(const Type &id)
|
||||
: id_(std::make_shared<pk<Type>>(id)) {}
|
||||
: id_(std::make_shared<pk<Type> >(id)) {
|
||||
}
|
||||
|
||||
explicit identifier(const char *id)
|
||||
: id_(std::make_shared<pk<const char*>>(id)) {}
|
||||
: id_(std::make_shared<pk<const char *> >(id)) {
|
||||
}
|
||||
|
||||
identifier(const identifier &x);
|
||||
identifier &operator=(const identifier &x);
|
||||
identifier(identifier &&x) noexcept ;
|
||||
identifier(identifier &&x) noexcept;
|
||||
identifier &operator=(identifier &&x) noexcept;
|
||||
|
||||
template<typename Type>
|
||||
identifier& operator=(const Type &value)
|
||||
{
|
||||
id_ = std::make_shared<pk<Type>>(value);
|
||||
identifier &operator=(const Type &value) {
|
||||
id_ = std::make_shared<pk<Type> >(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
identifier& operator=(const char *value)
|
||||
{
|
||||
id_ = std::make_shared<pk<const char*>>(value);
|
||||
identifier &operator=(const char *value) {
|
||||
id_ = std::make_shared<pk<const char *> >(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -208,7 +204,7 @@ public:
|
||||
friend std::ostream &operator<<(std::ostream &out, const identifier &id);
|
||||
|
||||
private:
|
||||
explicit identifier(const std::shared_ptr<base>& id);
|
||||
explicit identifier(const std::shared_ptr<base> &id);
|
||||
|
||||
private:
|
||||
std::shared_ptr<base> id_;
|
||||
@@ -217,10 +213,10 @@ private:
|
||||
static identifier null_identifier{};
|
||||
|
||||
/// @cond MATADOR_DEV
|
||||
struct id_pk_hash
|
||||
{
|
||||
struct id_pk_hash {
|
||||
size_t operator()(const identifier &id) const;
|
||||
};
|
||||
|
||||
/// @endcond
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef MATADOR_IDENTIFIER_TO_VALUE_CONVERTER_HPP
|
||||
#define MATADOR_IDENTIFIER_TO_VALUE_CONVERTER_HPP
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
namespace matador::utils {
|
||||
class identifier_to_value_converter final : public identifier_serializer {
|
||||
public:
|
||||
[[nodiscard]] value convert(const identifier &id);
|
||||
|
||||
void serialize(int8_t &, const field_attributes &) override;
|
||||
void serialize(int16_t &, const field_attributes &) override;
|
||||
void serialize(int32_t &, const field_attributes &) override;
|
||||
void serialize(int64_t &, const field_attributes &) override;
|
||||
void serialize(uint8_t &, const field_attributes &) override;
|
||||
void serialize(uint16_t &, const field_attributes &) override;
|
||||
void serialize(uint32_t &, const field_attributes &) override;
|
||||
void serialize(uint64_t &, const field_attributes &) override;
|
||||
void serialize(const char *, const field_attributes &) override;
|
||||
void serialize(std::string &, const field_attributes &) override;
|
||||
void serialize(null_type_t &, const field_attributes &) override;
|
||||
|
||||
private:
|
||||
value value_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_IDENTIFIER_TO_VALUE_CONVERTER_HPP
|
||||
@@ -44,6 +44,9 @@ public:
|
||||
value(value &&x) noexcept;
|
||||
value& operator=(value &&x) noexcept;
|
||||
|
||||
bool operator==(const value& rhs) const;
|
||||
bool operator!=(const value& rhs) const;
|
||||
|
||||
template<class Type>
|
||||
std::optional<Type> as() const {
|
||||
if (std::holds_alternative<Type>(value_)) {
|
||||
|
||||
Reference in New Issue
Block a user