[ox/model] Remove ModelValue::operator= and add iterators to ModelObject

This commit is contained in:
Gary Talent 2022-07-09 01:02:30 -05:00
parent f8d2b9e1a7
commit 3ca252320f

View File

@ -52,7 +52,7 @@ class ModelValue {
private: private:
Type m_type = Type::Undefined; Type m_type = Type::Undefined;
union { union {
bool b{}; bool b;
int8_t i8; int8_t i8;
uint8_t ui8; uint8_t ui8;
int16_t i16; int16_t i16;
@ -61,7 +61,7 @@ class ModelValue {
uint32_t ui32; uint32_t ui32;
int64_t i64; int64_t i64;
uint64_t ui64; uint64_t ui64;
String *str; String *str = nullptr;
ModelObject *obj; ModelObject *obj;
ModelUnion *uni; ModelUnion *uni;
ModelValueVector *vec; ModelValueVector *vec;
@ -69,33 +69,34 @@ class ModelValue {
template<typename T> template<typename T>
consteval static Type getType() noexcept { consteval static Type getType() noexcept {
if constexpr(is_bool_v<T>) { using U = typename ox::remove_reference<T>::type;
if constexpr(is_bool_v<U>) {
return Type::Bool; return Type::Bool;
} else if constexpr(is_integer_v<T>) { } else if constexpr(is_integer_v<U>) {
if (!is_signed_v<T> && sizeof(T) == 1) { if (!is_signed_v<U> && sizeof(U) == 1) {
return Type::UnsignedInteger8; return Type::UnsignedInteger8;
} else if constexpr(!is_signed_v<T> && sizeof(T) == 2) { } else if constexpr(!is_signed_v<U> && sizeof(U) == 2) {
return Type::UnsignedInteger16; return Type::UnsignedInteger16;
} else if constexpr(!is_signed_v<T> && sizeof(T) == 4) { } else if constexpr(!is_signed_v<U> && sizeof(U) == 4) {
return Type::UnsignedInteger32; return Type::UnsignedInteger32;
} else if constexpr(!is_signed_v<T> && sizeof(T) == 8) { } else if constexpr(!is_signed_v<U> && sizeof(U) == 8) {
return Type::UnsignedInteger64; return Type::UnsignedInteger64;
} else if constexpr(is_signed_v<T> && sizeof(T) == 1) { } else if constexpr(is_signed_v<U> && sizeof(U) == 1) {
return Type::SignedInteger8; return Type::SignedInteger8;
} else if constexpr(is_signed_v<T> && sizeof(T) == 2) { } else if constexpr(is_signed_v<U> && sizeof(U) == 2) {
return Type::SignedInteger16; return Type::SignedInteger16;
} else if constexpr(is_signed_v<T> && sizeof(T) == 4) { } else if constexpr(is_signed_v<U> && sizeof(U) == 4) {
return Type::SignedInteger32; return Type::SignedInteger32;
} else if constexpr(is_signed_v<T> && sizeof(T) == 8) { } else if constexpr(is_signed_v<U> && sizeof(U) == 8) {
return Type::SignedInteger64; return Type::SignedInteger64;
} }
} else if constexpr(is_same_v<T, ModelUnion>) { } else if constexpr(is_same_v<U, ModelUnion>) {
return Type::Union; return Type::Union;
} else if constexpr(is_same_v<T, ModelObject>) { } else if constexpr(is_same_v<U, ModelObject>) {
return Type::Object; return Type::Object;
} else if constexpr(is_same_v<T, String>) { } else if constexpr(is_same_v<U, String>) {
return Type::String; return Type::String;
} else if constexpr(is_same_v<T, ModelValueVector>) { } else if constexpr(is_same_v<U, ModelValueVector>) {
return Type::Vector; return Type::Vector;
} else { } else {
return Type::Undefined; return Type::Undefined;
@ -192,10 +193,6 @@ class ModelValue {
constexpr ModelValue &operator=(ModelValue &&val) noexcept; constexpr ModelValue &operator=(ModelValue &&val) noexcept;
constexpr ModelValue &operator=(const auto &val) noexcept;
constexpr ModelValue &operator=(auto &&val) noexcept;
private: private:
constexpr void freeResources() noexcept; constexpr void freeResources() noexcept;
@ -356,6 +353,66 @@ class ModelObject {
m_typeVersion = other.m_typeVersion; m_typeVersion = other.m_typeVersion;
} }
[[nodiscard]]
constexpr auto begin() noexcept {
return m_fieldsOrder.begin();
}
[[nodiscard]]
constexpr auto end() noexcept {
return m_fieldsOrder.end();
}
[[nodiscard]]
constexpr auto begin() const noexcept {
return m_fieldsOrder.begin();
}
[[nodiscard]]
constexpr auto end() const noexcept {
return m_fieldsOrder.end();
}
[[nodiscard]]
constexpr auto rbegin() noexcept {
return m_fieldsOrder.rbegin();
}
[[nodiscard]]
constexpr auto rend() noexcept {
return m_fieldsOrder.rend();
}
[[nodiscard]]
constexpr auto rbegin() const noexcept {
return m_fieldsOrder.rbegin();
}
[[nodiscard]]
constexpr auto rend() const noexcept {
return m_fieldsOrder.rend();
}
[[nodiscard]]
constexpr auto cbegin() const noexcept {
return m_fieldsOrder.cbegin();
}
[[nodiscard]]
constexpr auto cend() const noexcept {
return m_fieldsOrder.cend();
}
[[nodiscard]]
constexpr auto crbegin() const noexcept {
return m_fieldsOrder.crbegin();
}
[[nodiscard]]
constexpr auto crend() const noexcept {
return m_fieldsOrder.crend();
}
constexpr auto &operator=(const ModelObject &other) noexcept { constexpr auto &operator=(const ModelObject &other) noexcept {
if (&other == this) [[unlikely]] { if (&other == this) [[unlikely]] {
return *this; return *this;
@ -474,9 +531,9 @@ class ModelUnion {
m_unionIdx = i; m_unionIdx = i;
} }
constexpr void set(int i, auto val) noexcept { constexpr auto set(int i, auto val) noexcept {
m_unionIdx = i; m_unionIdx = i;
*m_fieldsOrder[i] = val; return m_fieldsOrder[static_cast<std::size_t>(i)]->value.set(val);
} }
constexpr void set(std::size_t i, auto val) noexcept { constexpr void set(std::size_t i, auto val) noexcept {
@ -712,7 +769,7 @@ constexpr Error ModelValue::set(T &&v) noexcept {
auto &value = getValue<type>(*this); auto &value = getValue<type>(*this);
if constexpr(type == Type::Vector || type == Type::Object || if constexpr(type == Type::Vector || type == Type::Object ||
type == Type::Union || type == Type::String) { type == Type::Union || type == Type::String) {
safeDelete(value); safeDelete(&value);
} }
value = ox::forward<T>(v); value = ox::forward<T>(v);
return OxError(0); return OxError(0);
@ -794,22 +851,6 @@ constexpr ModelValue &ModelValue::operator=(ModelValue &&other) noexcept {
return *this; return *this;
} }
constexpr ModelValue &ModelValue::operator=(const auto &val) noexcept {
if (this == &val) {
return *this;
}
set(val);
return *this;
}
constexpr ModelValue &ModelValue::operator=(auto &&val) noexcept {
if (this == &val) {
return *this;
}
set(ox::forward(val));
return *this;
}
constexpr void ModelValue::freeResources() noexcept { constexpr void ModelValue::freeResources() noexcept {
switch (m_type) { switch (m_type) {
case Type::Undefined: case Type::Undefined: