[ox/model] Cleanup alreadyExisted checks in DescWriter

This commit is contained in:
Gary Talent 2022-05-29 22:35:36 -05:00
parent a574cfc2aa
commit bc391b45fc
2 changed files with 56 additions and 63 deletions

View File

@ -47,96 +47,94 @@ static_assert([] {
return detail::indirectionLevels(i) == 1;
}(), "indirectionLevels broken: indirectionLevels(int[])");
const DescriptorType *TypeDescWriter::type(int8_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(int8_t*) noexcept {
constexpr auto String = "B:int8_t";
constexpr auto PT = PrimitiveType::SignedInteger;
constexpr auto Bytes = 1;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(int16_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(int16_t*) noexcept {
constexpr auto String = "B:int16_t";
constexpr auto PT = PrimitiveType::SignedInteger;
constexpr auto Bytes = 2;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(int32_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(int32_t*) noexcept {
constexpr auto String = "B:int32_t";
constexpr auto PT = PrimitiveType::SignedInteger;
constexpr auto Bytes = 4;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(int64_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(int64_t*) noexcept {
constexpr auto String = "B:int64_t";
constexpr auto PT = PrimitiveType::SignedInteger;
constexpr auto Bytes = 8;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(uint8_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(uint8_t*) noexcept {
constexpr auto String = "B:uint8_t";
constexpr auto PT = PrimitiveType::UnsignedInteger;
constexpr auto Bytes = 1;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(uint16_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(uint16_t*) noexcept {
constexpr auto String = "B:uint16_t";
constexpr auto PT = PrimitiveType::UnsignedInteger;
constexpr auto Bytes = 2;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(uint32_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(uint32_t*) noexcept {
constexpr auto String = "B:uint32_t";
constexpr auto PT = PrimitiveType::UnsignedInteger;
constexpr auto Bytes = 4;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(uint64_t*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(uint64_t*) noexcept {
constexpr auto String = "B:uint64_t";
constexpr auto PT = PrimitiveType::UnsignedInteger;
constexpr auto Bytes = 8;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::type(char*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(char*) noexcept {
constexpr auto String = "B:string";
constexpr auto PT = PrimitiveType::String;
return getType(String, PT, 0, alreadyExisted);
return getType(String, PT, 0);
}
const DescriptorType *TypeDescWriter::type(SerStr, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(SerStr) noexcept {
constexpr auto String = "B:string";
constexpr auto PT = PrimitiveType::String;
return getType(String, PT, 0, alreadyExisted);
return getType(String, PT, 0);
}
const DescriptorType *TypeDescWriter::type(String*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(String*) noexcept {
constexpr auto String = "B:string";
constexpr auto PT = PrimitiveType::String;
return getType(String, PT, 0, alreadyExisted);
return getType(String, PT, 0);
}
const DescriptorType *TypeDescWriter::type(bool*, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::type(bool*) noexcept {
constexpr auto String = "B:bool";
constexpr auto PT = PrimitiveType::Bool;
constexpr auto Bytes = 0;
return getType(String, PT, Bytes, alreadyExisted);
return getType(String, PT, Bytes);
}
const DescriptorType *TypeDescWriter::getType(const String &tn, PrimitiveType pt, int b, bool *alreadyExisted) noexcept {
const DescriptorType *TypeDescWriter::getType(const String &tn, PrimitiveType pt, int b) noexcept {
auto t = m_typeStore->get(tn);
if (!t.error) {
*alreadyExisted = true;
auto type = t.value;
oxAssert(type != nullptr, "TypeDescWriter::getType returning null DescriptorType");
return type;
} else {
*alreadyExisted = false;
auto dt = ox::make_unique<DescriptorType>(tn);
dt->primitiveType = pt;
dt->length = b;

View File

@ -71,40 +71,40 @@ class TypeDescWriter {
}
private:
const DescriptorType *type(int8_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(int16_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(int32_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(int64_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(int8_t *val) noexcept;
const DescriptorType *type(int16_t *val) noexcept;
const DescriptorType *type(int32_t *val) noexcept;
const DescriptorType *type(int64_t *val) noexcept;
const DescriptorType *type(uint8_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(uint16_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(uint32_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(uint64_t *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(uint8_t *val) noexcept;
const DescriptorType *type(uint16_t *val) noexcept;
const DescriptorType *type(uint32_t *val) noexcept;
const DescriptorType *type(uint64_t *val) noexcept;
const DescriptorType *type(bool *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(bool *val) noexcept;
const DescriptorType *type(char *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(char *val) noexcept;
const DescriptorType *type(SerStr val, bool *alreadyExisted) noexcept;
const DescriptorType *type(SerStr val) noexcept;
const DescriptorType *type(String *val, bool *alreadyExisted) noexcept;
const DescriptorType *type(String *val) noexcept;
template<std::size_t sz>
constexpr const DescriptorType *type(BString<sz> *val, bool *alreadyExisted) noexcept;
constexpr const DescriptorType *type(BString<sz> *val) noexcept;
template<typename T>
constexpr const DescriptorType *type(T *val, bool *alreadyExisted) noexcept;
constexpr const DescriptorType *type(T *val) noexcept;
template<typename T>
constexpr const DescriptorType *type(Vector<T> *val, bool *alreadyExisted) noexcept;
constexpr const DescriptorType *type(Vector<T> *val) noexcept;
template<typename T>
constexpr const DescriptorType *type(HashMap<String, T> *val, bool *alreadyExisted) noexcept;
constexpr const DescriptorType *type(HashMap<String, T> *val) noexcept;
template<typename U>
constexpr const DescriptorType *type(UnionView<U> val, bool *alreadyExisted) noexcept;
constexpr const DescriptorType *type(UnionView<U> val) noexcept;
const DescriptorType *getType(const String &tn, PrimitiveType t, int b, bool *alreadyExisted) noexcept;
const DescriptorType *getType(const String &tn, PrimitiveType t, int b) noexcept;
};
constexpr TypeDescWriter::TypeDescWriter(TypeStore *typeStore) noexcept: m_typeStore(typeStore) {
@ -115,11 +115,10 @@ template<typename T>
constexpr Error TypeDescWriter::field(const char *name, T *val, std::size_t) noexcept {
if (m_type) {
constexpr typename remove_pointer<decltype(val)>::type *p = nullptr;
bool alreadyExisted = false;
const auto t = type(p, &alreadyExisted);
const auto t = type(p);
oxAssert(t != nullptr, "field(const char *name, T *val, std::size_t): Type not found or generated");
if (t == nullptr) {
type(p, &alreadyExisted);
type(p);
}
m_type->fieldList.emplace_back(t, name, detail::indirectionLevels(val), t->typeName);
return OxError(0);
@ -130,8 +129,7 @@ constexpr Error TypeDescWriter::field(const char *name, T *val, std::size_t) noe
template<typename T>
constexpr Error TypeDescWriter::field(const char *name, T val) noexcept {
if (m_type) {
bool alreadyExisted = false;
const auto t = type(val, &alreadyExisted);
const auto t = type(val);
oxAssert(t != nullptr, "field(const char *name, T val): Type not found or generated");
m_type->fieldList.emplace_back(t, name, 0, t->typeName);
return OxError(0);
@ -142,8 +140,7 @@ constexpr Error TypeDescWriter::field(const char *name, T val) noexcept {
template<typename T>
constexpr Error TypeDescWriter::field(const char *name, T *val) noexcept {
if (m_type) {
bool alreadyExisted = false;
const auto t = type(val, &alreadyExisted);
const auto t = type(val);
oxAssert(t != nullptr, "field(const char *name, T *val): Type not found or generated");
m_type->fieldList.emplace_back(t, name, 0, t->typeName);
return OxError(0);
@ -152,37 +149,35 @@ constexpr Error TypeDescWriter::field(const char *name, T *val) noexcept {
}
template<std::size_t sz>
constexpr const DescriptorType *TypeDescWriter::type(BString<sz> *val, bool *alreadyExisted) noexcept {
return type(SerStr(val), alreadyExisted);
constexpr const DescriptorType *TypeDescWriter::type(BString<sz> *val) noexcept {
return type(SerStr(val));
}
template<typename T>
constexpr const DescriptorType *TypeDescWriter::type(T *val, bool *alreadyExisted) noexcept {
constexpr const DescriptorType *TypeDescWriter::type(T *val) noexcept {
auto [t, err] = m_typeStore->template get<T>();
if (!err) {
*alreadyExisted = true;
return t;
} else {
TypeDescWriter dw(m_typeStore);
oxLogError(model(&dw, val));
*alreadyExisted = false;
return dw.m_type;
}
}
template<typename T>
constexpr const DescriptorType *TypeDescWriter::type(Vector<T> *val, bool *alreadyExisted) noexcept {
return type(val->data(), alreadyExisted);
constexpr const DescriptorType *TypeDescWriter::type(Vector<T> *val) noexcept {
return type(val->data());
}
template<typename T>
constexpr const DescriptorType *TypeDescWriter::type(HashMap<String, T>*, bool *alreadyExisted) noexcept {
return type(static_cast<T*>(nullptr), alreadyExisted);
constexpr const DescriptorType *TypeDescWriter::type(HashMap<String, T>*) noexcept {
return type(static_cast<T*>(nullptr));
}
template<typename U>
constexpr const DescriptorType *TypeDescWriter::type(UnionView<U> val, bool *alreadyExisted) noexcept {
return type(val.get(), alreadyExisted);
constexpr const DescriptorType *TypeDescWriter::type(UnionView<U> val) noexcept {
return type(val.get());
}
template<typename T>