mirror of
https://github.com/libAthena/athena.git
synced 2025-12-08 13:15:05 +00:00
Numerous code style improvements
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include "athena/IStreamReader.hpp"
|
||||
#include "athena/IStreamWriter.hpp"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
namespace athena::io {
|
||||
|
||||
/**
|
||||
@@ -127,7 +129,7 @@ struct DNAV : DNA<DNAE> {
|
||||
virtual void read(athena::io::IStreamReader& r) = 0;
|
||||
virtual void write(athena::io::IStreamWriter& w) const = 0;
|
||||
virtual void binarySize(size_t& s) const = 0;
|
||||
virtual const char* DNATypeV() const = 0;
|
||||
virtual std::string_view DNATypeV() const = 0;
|
||||
};
|
||||
|
||||
template <Endian DNAE>
|
||||
|
||||
@@ -15,20 +15,20 @@
|
||||
namespace athena::io {
|
||||
|
||||
struct PropId {
|
||||
const char* name = nullptr;
|
||||
std::string_view name;
|
||||
uint32_t rcrc32 = 0xffffffff;
|
||||
uint64_t crc64 = 0x0;
|
||||
template <class T>
|
||||
constexpr T opget() const;
|
||||
constexpr PropId() = default;
|
||||
constexpr explicit PropId(const char* name, uint32_t rcrc32, uint64_t crc64)
|
||||
constexpr explicit PropId(std::string_view name, uint32_t rcrc32, uint64_t crc64)
|
||||
: name(name), rcrc32(rcrc32), crc64(crc64) {}
|
||||
constexpr explicit PropId(const char* name)
|
||||
constexpr explicit PropId(std::string_view name)
|
||||
: name(name)
|
||||
, rcrc32(athena::checksums::literals::rcrc32_rec(0xFFFFFFFF, name))
|
||||
, crc64(athena::checksums::literals::crc64_rec(0xFFFFFFFFFFFFFFFF, name)) {}
|
||||
constexpr PropId(const char* name, uint32_t rcrc32)
|
||||
: name(name), rcrc32(rcrc32), crc64(athena::checksums::literals::crc64_rec(0xFFFFFFFFFFFFFFFF, name)) {}
|
||||
, rcrc32(athena::checksums::literals::rcrc32_rec(0xFFFFFFFF, name.data()))
|
||||
, crc64(athena::checksums::literals::crc64_rec(0xFFFFFFFFFFFFFFFF, name.data())) {}
|
||||
constexpr PropId(std::string_view name, uint32_t rcrc32)
|
||||
: name(name), rcrc32(rcrc32), crc64(athena::checksums::literals::crc64_rec(0xFFFFFFFFFFFFFFFF, name.data())) {}
|
||||
};
|
||||
|
||||
template <>
|
||||
@@ -628,7 +628,7 @@ struct ReadYaml {
|
||||
if (auto __v = r.enterSubVector(id.name, _count)) {
|
||||
vector.reserve(_count);
|
||||
for (size_t i = 0; i < _count; ++i)
|
||||
vector.push_back(r.readBool(nullptr));
|
||||
vector.push_back(r.readBool());
|
||||
}
|
||||
/* Horrible reference abuse (but it works) */
|
||||
const_cast<S&>(count) = vector.size();
|
||||
@@ -733,7 +733,7 @@ struct WriteYaml {
|
||||
/* libc++ specializes vector<bool> as a bitstream */
|
||||
if (auto __v = w.enterSubVector(id.name))
|
||||
for (const T& v : vector)
|
||||
w.writeBool(nullptr, v);
|
||||
w.writeBool(v);
|
||||
}
|
||||
static void Do(const PropId& id, std::unique_ptr<atUint8[]>& buf, size_t count, StreamT& w) {
|
||||
w.writeUBytes(id.name, buf, count);
|
||||
@@ -944,7 +944,7 @@ void __BinarySizeProp64(const T& obj, size_t& s) {
|
||||
} \
|
||||
template <class Op> \
|
||||
void Enumerate(typename Op::StreamT& s); \
|
||||
static const char* DNAType();
|
||||
static std::string_view DNAType();
|
||||
|
||||
#define AT_DECL_DNA \
|
||||
AT_DECL_DNA_DO \
|
||||
@@ -985,7 +985,7 @@ void __BinarySizeProp64(const T& obj, size_t& s) {
|
||||
void read(athena::io::IStreamReader& r) override { athena::io::__Read(*this, r); } \
|
||||
void write(athena::io::IStreamWriter& w) const override { athena::io::__Write(*this, w); } \
|
||||
void binarySize(size_t& s) const override { athena::io::__BinarySize(*this, s); } \
|
||||
const char* DNATypeV() const override { return DNAType(); }
|
||||
std::string_view DNATypeV() const override { return DNAType(); }
|
||||
|
||||
#define AT_DECL_DNAV_NO_TYPE \
|
||||
AT_DECL_DNA_DO \
|
||||
@@ -1055,7 +1055,7 @@ void __BinarySizeProp64(const T& obj, size_t& s) {
|
||||
void Enumerate(typename Op::StreamT& s); \
|
||||
template <class Op> \
|
||||
bool Lookup(uint64_t hash, typename Op::StreamT& s); \
|
||||
static const char* DNAType(); \
|
||||
static std::string_view DNAType(); \
|
||||
void read(athena::io::IStreamReader& r) { athena::io::__Read(*this, r); } \
|
||||
void write(athena::io::IStreamWriter& w) const { athena::io::__Write(*this, w); } \
|
||||
void binarySize(size_t& s) const { athena::io::__BinarySize(*this, s); } \
|
||||
|
||||
@@ -12,12 +12,12 @@
|
||||
namespace athena::io {
|
||||
|
||||
template <class T>
|
||||
const char* __GetDNAName(const T& dna, std::enable_if_t<athena::io::__IsDNAVRecord_v<T>>* = nullptr) {
|
||||
inline std::string_view __GetDNAName(const T& dna, std::enable_if_t<athena::io::__IsDNAVRecord_v<T>>* = nullptr) {
|
||||
return dna.DNATypeV();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const char* __GetDNAName(const T& dna, std::enable_if_t<!athena::io::__IsDNAVRecord_v<T>>* = nullptr) {
|
||||
inline std::string_view __GetDNAName(const T& dna, std::enable_if_t<!athena::io::__IsDNAVRecord_v<T>>* = nullptr) {
|
||||
return dna.DNAType();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,16 +27,16 @@ struct YAMLNode {
|
||||
|
||||
YAMLNode(yaml_node_type_t type) : m_type(type) {}
|
||||
|
||||
const YAMLNode* findMapChild(std::string_view key) const {
|
||||
YAMLNode* findMapChild(std::string_view key) const {
|
||||
for (const auto& item : m_mapChildren)
|
||||
if (!item.first.compare(key))
|
||||
if (item.first == key)
|
||||
return item.second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void assignMapChild(std::string_view key, std::unique_ptr<YAMLNode>&& node) {
|
||||
for (auto& item : m_mapChildren)
|
||||
if (!item.first.compare(key)) {
|
||||
if (item.first == key) {
|
||||
item.second = std::move(node);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ public:
|
||||
|
||||
bool parse(athena::io::IStreamReader* reader);
|
||||
|
||||
bool ClassTypeOperation(std::function<bool(const char* dnaType)> func);
|
||||
bool ValidateClassType(const char* expectedType);
|
||||
bool ClassTypeOperation(std::function<bool(std::string_view dnaType)> func);
|
||||
bool ValidateClassType(std::string_view expectedType);
|
||||
|
||||
const YAMLNode* getRootNode() const { return m_rootNode.get(); }
|
||||
const YAMLNode* getCurNode() const { return m_subStack.empty() ? nullptr : m_subStack.back(); }
|
||||
@@ -59,10 +59,10 @@ public:
|
||||
};
|
||||
friend class RecordRAII;
|
||||
|
||||
RecordRAII enterSubRecord(const char* name);
|
||||
RecordRAII enterSubRecord(std::string_view name = {});
|
||||
|
||||
template <class T>
|
||||
void enumerate(const char* name, T& record, std::enable_if_t<__IsDNARecord_v<T>>* = nullptr) {
|
||||
void enumerate(std::string_view name, T& record, std::enable_if_t<__IsDNARecord_v<T>>* = nullptr) {
|
||||
if (auto rec = enterSubRecord(name))
|
||||
record.read(*this);
|
||||
}
|
||||
@@ -82,10 +82,11 @@ public:
|
||||
};
|
||||
friend class VectorRAII;
|
||||
|
||||
VectorRAII enterSubVector(const char* name, size_t& countOut);
|
||||
VectorRAII enterSubVector(std::string_view name, size_t& countOut);
|
||||
VectorRAII enterSubVector(size_t& countOut) { return enterSubVector({}, countOut); }
|
||||
|
||||
template <class T>
|
||||
size_t enumerate(const char* name, std::vector<T>& vector,
|
||||
size_t enumerate(std::string_view name, std::vector<T>& vector,
|
||||
std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, atVec2f> &&
|
||||
!std::is_same_v<T, atVec3f> && !std::is_same_v<T, atVec4f>>* = nullptr) {
|
||||
size_t countOut;
|
||||
@@ -94,7 +95,7 @@ public:
|
||||
vector.reserve(countOut);
|
||||
for (size_t i = 0; i < countOut; ++i) {
|
||||
vector.emplace_back();
|
||||
if (auto rec = enterSubRecord(nullptr))
|
||||
if (auto rec = enterSubRecord())
|
||||
vector.back().read(*this);
|
||||
}
|
||||
}
|
||||
@@ -102,7 +103,7 @@ public:
|
||||
}
|
||||
|
||||
template <class T>
|
||||
size_t enumerate(const char* name, std::vector<T>& vector,
|
||||
size_t enumerate(std::string_view name, std::vector<T>& vector,
|
||||
std::enable_if_t<std::is_arithmetic_v<T> || std::is_same_v<T, atVec2f> ||
|
||||
std::is_same_v<T, atVec3f> || std::is_same_v<T, atVec4f>>* = nullptr) {
|
||||
size_t countOut;
|
||||
@@ -116,53 +117,53 @@ public:
|
||||
}
|
||||
|
||||
template <class T>
|
||||
size_t enumerate(const char* name, std::vector<T>& vector, std::function<void(YAMLDocReader&, T&)> readf) {
|
||||
size_t enumerate(std::string_view name, std::vector<T>& vector, std::function<void(YAMLDocReader&, T&)> readf) {
|
||||
size_t countOut;
|
||||
if (auto v = enterSubVector(name, countOut)) {
|
||||
vector.clear();
|
||||
vector.reserve(countOut);
|
||||
for (size_t i = 0; i < countOut; ++i) {
|
||||
vector.emplace_back();
|
||||
if (auto rec = enterSubRecord(nullptr))
|
||||
if (auto rec = enterSubRecord())
|
||||
readf(*this, vector.back());
|
||||
}
|
||||
}
|
||||
return countOut;
|
||||
}
|
||||
|
||||
bool hasVal(const char* name) const {
|
||||
bool hasVal(std::string_view name) const {
|
||||
if (m_subStack.size()) {
|
||||
const YAMLNode* mnode = m_subStack.back();
|
||||
if (mnode->m_type == YAML_MAPPING_NODE && name)
|
||||
if (mnode->m_type == YAML_MAPPING_NODE && !name.empty())
|
||||
for (const auto& item : mnode->m_mapChildren)
|
||||
if (!item.first.compare(name))
|
||||
if (item.first == name)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename RETURNTYPE>
|
||||
RETURNTYPE readVal(const char* name);
|
||||
bool readBool(const char* name);
|
||||
atInt8 readByte(const char* name);
|
||||
atUint8 readUByte(const char* name);
|
||||
atInt16 readInt16(const char* name);
|
||||
atUint16 readUint16(const char* name);
|
||||
atInt32 readInt32(const char* name);
|
||||
atUint32 readUint32(const char* name);
|
||||
atInt64 readInt64(const char* name);
|
||||
atUint64 readUint64(const char* name);
|
||||
float readFloat(const char* name);
|
||||
double readDouble(const char* name);
|
||||
atVec2f readVec2f(const char* name);
|
||||
atVec3f readVec3f(const char* name);
|
||||
atVec4f readVec4f(const char* name);
|
||||
atVec2d readVec2d(const char* name);
|
||||
atVec3d readVec3d(const char* name);
|
||||
atVec4d readVec4d(const char* name);
|
||||
std::unique_ptr<atUint8[]> readUBytes(const char* name);
|
||||
std::string readString(const char* name);
|
||||
std::wstring readWString(const char* name);
|
||||
RETURNTYPE readVal(std::string_view name = {});
|
||||
bool readBool(std::string_view name = {});
|
||||
atInt8 readByte(std::string_view name = {});
|
||||
atUint8 readUByte(std::string_view name = {});
|
||||
atInt16 readInt16(std::string_view name = {});
|
||||
atUint16 readUint16(std::string_view name = {});
|
||||
atInt32 readInt32(std::string_view name = {});
|
||||
atUint32 readUint32(std::string_view name = {});
|
||||
atInt64 readInt64(std::string_view name = {});
|
||||
atUint64 readUint64(std::string_view name = {});
|
||||
float readFloat(std::string_view name = {});
|
||||
double readDouble(std::string_view name = {});
|
||||
atVec2f readVec2f(std::string_view name = {});
|
||||
atVec3f readVec3f(std::string_view name = {});
|
||||
atVec4f readVec4f(std::string_view name = {});
|
||||
atVec2d readVec2d(std::string_view name = {});
|
||||
atVec3d readVec3d(std::string_view name = {});
|
||||
atVec4d readVec4d(std::string_view name = {});
|
||||
std::unique_ptr<atUint8[]> readUBytes(std::string_view name = {});
|
||||
std::string readString(std::string_view name = {});
|
||||
std::wstring readWString(std::string_view name = {});
|
||||
};
|
||||
|
||||
} // namespace athena::io
|
||||
|
||||
@@ -20,7 +20,8 @@ class YAMLDocWriter {
|
||||
void _leaveSubVector();
|
||||
|
||||
public:
|
||||
explicit YAMLDocWriter(const char* classType, athena::io::IStreamReader* reader = nullptr);
|
||||
explicit YAMLDocWriter(std::string_view classType, athena::io::IStreamReader* reader = nullptr);
|
||||
explicit YAMLDocWriter(athena::io::IStreamReader* reader = nullptr) : YAMLDocWriter({}, reader) {}
|
||||
~YAMLDocWriter();
|
||||
|
||||
yaml_emitter_t* getEmitter() { return &m_emitter; }
|
||||
@@ -44,10 +45,10 @@ public:
|
||||
};
|
||||
friend class RecordRAII;
|
||||
|
||||
RecordRAII enterSubRecord(const char* name);
|
||||
RecordRAII enterSubRecord(std::string_view name = {});
|
||||
|
||||
template <class T>
|
||||
void enumerate(const char* name, T& record, std::enable_if_t<__IsDNARecord_v<T>>* = nullptr) {
|
||||
void enumerate(std::string_view name, T& record, std::enable_if_t<__IsDNARecord_v<T>>* = nullptr) {
|
||||
if (auto rec = enterSubRecord(name))
|
||||
record.write(*this);
|
||||
}
|
||||
@@ -67,64 +68,90 @@ public:
|
||||
};
|
||||
friend class VectorRAII;
|
||||
|
||||
VectorRAII enterSubVector(const char* name);
|
||||
VectorRAII enterSubVector(std::string_view name = {});
|
||||
|
||||
template <class T>
|
||||
void
|
||||
enumerate(const char* name, const std::vector<T>& vector,
|
||||
enumerate(std::string_view name, const std::vector<T>& vector,
|
||||
std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, atVec2f> && !std::is_same_v<T, atVec3f> &&
|
||||
!std::is_same_v<T, atVec4f> && !std::is_same_v<T, atVec2d> &&
|
||||
!std::is_same_v<T, atVec3d> && !std::is_same_v<T, atVec4d>>* = nullptr) {
|
||||
if (auto v = enterSubVector(name))
|
||||
for (const T& item : vector)
|
||||
if (auto rec = enterSubRecord(nullptr))
|
||||
if (auto rec = enterSubRecord())
|
||||
item.write(*this);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void enumerate(const char* name, const std::vector<T>& vector,
|
||||
void enumerate(std::string_view name, const std::vector<T>& vector,
|
||||
std::enable_if_t<std::is_arithmetic_v<T> || std::is_same_v<T, atVec2f> || std::is_same_v<T, atVec3f> ||
|
||||
std::is_same_v<T, atVec4f> || std::is_same_v<T, atVec2d> ||
|
||||
std::is_same_v<T, atVec3d> || std::is_same_v<T, atVec4d>>* = nullptr) {
|
||||
if (auto v = enterSubVector(name))
|
||||
for (T item : vector)
|
||||
writeVal<T>(nullptr, item);
|
||||
writeVal<T>(item);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void enumerate(const char* name, const std::vector<T>& vector, std::function<void(YAMLDocWriter&, const T&)> writef) {
|
||||
void enumerate(std::string_view name, const std::vector<T>& vector, std::function<void(YAMLDocWriter&, const T&)> writef) {
|
||||
if (auto v = enterSubVector(name))
|
||||
for (const T& item : vector)
|
||||
if (auto rec = enterSubRecord(nullptr))
|
||||
if (auto rec = enterSubRecord())
|
||||
writef(*this, item);
|
||||
}
|
||||
|
||||
template <typename INTYPE>
|
||||
void writeVal(const char* name, const INTYPE& val);
|
||||
void writeVal(std::string_view name, const INTYPE& val);
|
||||
template <typename INTYPE>
|
||||
void writeVal(const char* name, const INTYPE& val, size_t byteCount);
|
||||
void writeBool(const char* name, const bool& val);
|
||||
void writeByte(const char* name, const atInt8& val);
|
||||
void writeUByte(const char* name, const atUint8& val);
|
||||
void writeInt16(const char* name, const atInt16& val);
|
||||
void writeUint16(const char* name, const atUint16& val);
|
||||
void writeInt32(const char* name, const atInt32& val);
|
||||
void writeUint32(const char* name, const atUint32& val);
|
||||
void writeInt64(const char* name, const atInt64& val);
|
||||
void writeUint64(const char* name, const atUint64& val);
|
||||
void writeFloat(const char* name, const float& val);
|
||||
void writeDouble(const char* name, const double& val);
|
||||
void writeVec2f(const char* name, const atVec2f& val);
|
||||
void writeVec3f(const char* name, const atVec3f& val);
|
||||
void writeVec4f(const char* name, const atVec4f& val);
|
||||
void writeVec2d(const char* name, const atVec2d& val);
|
||||
void writeVec3d(const char* name, const atVec3d& val);
|
||||
void writeVec4d(const char* name, const atVec4d& val);
|
||||
void writeUBytes(const char* name, const std::unique_ptr<atUint8[]>& val, size_t byteCount);
|
||||
void writeString(const char* name, std::string_view val);
|
||||
void writeWString(const char* name, std::wstring_view val);
|
||||
void writeU16String(const char* name, std::u16string_view val);
|
||||
void writeU32String(const char* name, std::u32string_view val);
|
||||
void writeVal(const INTYPE& val) { writeVal<INTYPE>(std::string_view{}, val); }
|
||||
template <typename INTYPE>
|
||||
void writeVal(std::string_view name, const INTYPE& val, size_t byteCount);
|
||||
template <typename INTYPE>
|
||||
void writeVal(const INTYPE& val, size_t byteCount) { writeVal<INTYPE>(std::string_view{}, val, byteCount); }
|
||||
void writeBool(std::string_view name, const bool& val);
|
||||
void writeBool(const bool& val) { writeBool({}, val); }
|
||||
void writeByte(std::string_view name, const atInt8& val);
|
||||
void writeByte(const atInt8& val) { writeByte({}, val); }
|
||||
void writeUByte(std::string_view name, const atUint8& val);
|
||||
void writeUByte(const atUint8& val) { writeUByte({}, val); }
|
||||
void writeInt16(std::string_view name, const atInt16& val);
|
||||
void writeInt16(const atInt16& val) { writeInt16({}, val); }
|
||||
void writeUint16(std::string_view name, const atUint16& val);
|
||||
void writeUint16(const atUint16& val) { writeUint16({}, val); }
|
||||
void writeInt32(std::string_view name, const atInt32& val);
|
||||
void writeInt32(const atInt32& val) { writeInt32({}, val); }
|
||||
void writeUint32(std::string_view name, const atUint32& val);
|
||||
void writeUint32(const atUint32& val) { writeUint32({}, val); }
|
||||
void writeInt64(std::string_view name, const atInt64& val);
|
||||
void writeInt64(const atInt64& val) { writeInt64({}, val); }
|
||||
void writeUint64(std::string_view name, const atUint64& val);
|
||||
void writeUint64(const atUint64& val) { writeUint64({}, val); }
|
||||
void writeFloat(std::string_view name, const float& val);
|
||||
void writeFloat(const float& val) { writeFloat({}, val); }
|
||||
void writeDouble(std::string_view name, const double& val);
|
||||
void writeDouble(const double& val) { writeDouble({}, val); }
|
||||
void writeVec2f(std::string_view name, const atVec2f& val);
|
||||
void writeVec2f(const atVec2f& val) { writeVec2f({}, val); }
|
||||
void writeVec3f(std::string_view name, const atVec3f& val);
|
||||
void writeVec3f(const atVec3f& val) { writeVec3f({}, val); }
|
||||
void writeVec4f(std::string_view name, const atVec4f& val);
|
||||
void writeVec4f(const atVec4f& val) { writeVec4f({}, val); }
|
||||
void writeVec2d(std::string_view name, const atVec2d& val);
|
||||
void writeVec2d(const atVec2d& val) { writeVec2d({}, val); }
|
||||
void writeVec3d(std::string_view name, const atVec3d& val);
|
||||
void writeVec3d(const atVec3d& val) { writeVec3d({}, val); }
|
||||
void writeVec4d(std::string_view name, const atVec4d& val);
|
||||
void writeVec4d(const atVec4d& val) { writeVec4d({}, val); }
|
||||
void writeUBytes(std::string_view name, const std::unique_ptr<atUint8[]>& val, size_t byteCount);
|
||||
void writeUBytes(const std::unique_ptr<atUint8[]>& val, size_t byteCount) { writeUBytes({}, val, byteCount); }
|
||||
void writeString(std::string_view name, std::string_view val);
|
||||
void writeString(std::string_view val) { writeString({}, val); }
|
||||
void writeWString(std::string_view name, std::wstring_view val);
|
||||
void writeWString(std::wstring_view val) { writeWString({}, val); }
|
||||
void writeU16String(std::string_view name, std::u16string_view val);
|
||||
void writeU16String(std::u16string_view val) { writeU16String({}, val); }
|
||||
void writeU32String(std::string_view name, std::u32string_view val);
|
||||
void writeU32String(std::u32string_view val) { writeU32String({}, val); }
|
||||
|
||||
void setStyle(YAMLNodeStyle s);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user