DNAYaml: Make use of std::make_unique where applicable

Gets rid of a few places of raw new where it can be trivially avoided.
This commit is contained in:
Lioncash 2019-08-15 07:18:18 -04:00
parent 0532b38059
commit 46a847e885
1 changed files with 75 additions and 75 deletions

View File

@ -15,127 +15,127 @@ bool NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(bool val) { std::unique_ptr<YAMLNode> ValToNode(bool val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = val ? "True" : "False"; ret->m_scalarString = val ? "True" : "False";
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atInt8 NodeToVal(const YAMLNode* node) { atInt8 NodeToVal(const YAMLNode* node) {
return strtol(node->m_scalarString.c_str(), NULL, 0); return strtol(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atInt8 val) { std::unique_ptr<YAMLNode> ValToNode(atInt8 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), int(val)); ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atUint8 NodeToVal(const YAMLNode* node) { atUint8 NodeToVal(const YAMLNode* node) {
return strtoul(node->m_scalarString.c_str(), NULL, 0); return strtoul(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atUint8 val) { std::unique_ptr<YAMLNode> ValToNode(atUint8 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("0x{:02X}"), val); ret->m_scalarString = fmt::format(fmt("0x{:02X}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atInt16 NodeToVal(const YAMLNode* node) { atInt16 NodeToVal(const YAMLNode* node) {
return strtol(node->m_scalarString.c_str(), NULL, 0); return strtol(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atInt16 val) { std::unique_ptr<YAMLNode> ValToNode(atInt16 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), int(val)); ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atUint16 NodeToVal(const YAMLNode* node) { atUint16 NodeToVal(const YAMLNode* node) {
return strtoul(node->m_scalarString.c_str(), NULL, 0); return strtoul(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atUint16 val) { std::unique_ptr<YAMLNode> ValToNode(atUint16 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("0x{:04X}"), val); ret->m_scalarString = fmt::format(fmt("0x{:04X}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atInt32 NodeToVal(const YAMLNode* node) { atInt32 NodeToVal(const YAMLNode* node) {
return strtol(node->m_scalarString.c_str(), NULL, 0); return strtol(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atInt32 val) { std::unique_ptr<YAMLNode> ValToNode(atInt32 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), int(val)); ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atUint32 NodeToVal(const YAMLNode* node) { atUint32 NodeToVal(const YAMLNode* node) {
return strtoul(node->m_scalarString.c_str(), NULL, 0); return strtoul(node->m_scalarString.c_str(), nullptr, 0);
} }
std::unique_ptr<YAMLNode> ValToNode(atUint32 val) { std::unique_ptr<YAMLNode> ValToNode(atUint32 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("0x{:08X}"), val); ret->m_scalarString = fmt::format(fmt("0x{:08X}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atInt64 NodeToVal(const YAMLNode* node) { atInt64 NodeToVal(const YAMLNode* node) {
#if _WIN32 #if _WIN32
return _strtoi64(node->m_scalarString.c_str(), NULL, 0); return _strtoi64(node->m_scalarString.c_str(), nullptr, 0);
#else #else
return strtoq(node->m_scalarString.c_str(), NULL, 0); return strtoq(node->m_scalarString.c_str(), nullptr, 0);
#endif #endif
} }
std::unique_ptr<YAMLNode> ValToNode(atInt64 val) { std::unique_ptr<YAMLNode> ValToNode(atInt64 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), val); ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
atUint64 NodeToVal(const YAMLNode* node) { atUint64 NodeToVal(const YAMLNode* node) {
#if _WIN32 #if _WIN32
return _strtoui64(node->m_scalarString.c_str(), NULL, 0); return _strtoui64(node->m_scalarString.c_str(), nullptr, 0);
#else #else
return strtouq(node->m_scalarString.c_str(), NULL, 0); return strtouq(node->m_scalarString.c_str(), nullptr, 0);
#endif #endif
} }
std::unique_ptr<YAMLNode> ValToNode(atUint64 val) { std::unique_ptr<YAMLNode> ValToNode(atUint64 val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("0x{:016X}"), val); ret->m_scalarString = fmt::format(fmt("0x{:016X}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
float NodeToVal(const YAMLNode* node) { float NodeToVal(const YAMLNode* node) {
return strtof(node->m_scalarString.c_str(), NULL); return strtof(node->m_scalarString.c_str(), nullptr);
} }
std::unique_ptr<YAMLNode> ValToNode(float val) { std::unique_ptr<YAMLNode> ValToNode(float val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), val); ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
double NodeToVal(const YAMLNode* node) { double NodeToVal(const YAMLNode* node) {
return strtod(node->m_scalarString.c_str(), NULL); return strtod(node->m_scalarString.c_str(), nullptr);
} }
std::unique_ptr<YAMLNode> ValToNode(double val) { std::unique_ptr<YAMLNode> ValToNode(double val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = fmt::format(fmt("{}"), val); ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <typename RETURNTYPE> template <typename RETURNTYPE>
@ -165,15 +165,15 @@ atVec2f NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec2f& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec2f& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(2); ret->m_seqChildren.reserve(2);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 2; ++i) { for (size_t i = 0; i < 2; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -182,15 +182,15 @@ atVec3f NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec3f& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec3f& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(3); ret->m_seqChildren.reserve(3);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -199,15 +199,15 @@ atVec4f NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec4f& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec4f& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(4); ret->m_seqChildren.reserve(4);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -216,15 +216,15 @@ atVec2d NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec2d& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec2d& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(2); ret->m_seqChildren.reserve(2);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 2; ++i) { for (size_t i = 0; i < 2; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -233,15 +233,15 @@ atVec3d NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec3d& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec3d& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(3); ret->m_seqChildren.reserve(3);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -250,15 +250,15 @@ atVec4d NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const atVec4d& val) { std::unique_ptr<YAMLNode> ValToNode(const atVec4d& val) {
YAMLNode* ret = new YAMLNode(YAML_SEQUENCE_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SEQUENCE_NODE);
ret->m_seqChildren.reserve(4); ret->m_seqChildren.reserve(4);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); auto comp = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
comp->m_scalarString = fmt::format(fmt("{}"), f[i]); comp->m_scalarString = fmt::format(fmt("{}"), f[i]);
ret->m_seqChildren.emplace_back(comp); ret->m_seqChildren.emplace_back(std::move(comp));
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -267,10 +267,10 @@ std::unique_ptr<atUint8[]> NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(const std::unique_ptr<atUint8[]>& val, size_t byteCount) { std::unique_ptr<YAMLNode> ValToNode(const std::unique_ptr<atUint8[]>& val, size_t byteCount) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
if (val) if (val)
ret->m_scalarString = base64_encode(val.get(), byteCount); ret->m_scalarString = base64_encode(val.get(), byteCount);
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -279,9 +279,9 @@ std::string NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(std::string_view val) { std::unique_ptr<YAMLNode> ValToNode(std::string_view val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString = val; ret->m_scalarString = val;
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
template <> template <>
@ -303,48 +303,48 @@ std::wstring NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(std::wstring_view val) { std::unique_ptr<YAMLNode> ValToNode(std::wstring_view val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length()); ret->m_scalarString.reserve(val.length());
for (wchar_t ch : val) { for (wchar_t ch : val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning(fmt("invalid UTF-8 character while encoding")); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
std::unique_ptr<YAMLNode> ValToNode(std::u16string_view val) { std::unique_ptr<YAMLNode> ValToNode(std::u16string_view val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length()); ret->m_scalarString.reserve(val.length());
for (char16_t ch : val) { for (char16_t ch : val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning(fmt("invalid UTF-8 character while encoding")); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
std::unique_ptr<YAMLNode> ValToNode(std::u32string_view val) { std::unique_ptr<YAMLNode> ValToNode(std::u32string_view val) {
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); auto ret = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length()); ret->m_scalarString.reserve(val.length());
for (char32_t ch : val) { for (char32_t ch : val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning(fmt("invalid UTF-8 character while encoding")); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
} }
return std::unique_ptr<YAMLNode>(ret); return ret;
} }
static const char* ErrorString(yaml_error_type_t errt) { static const char* ErrorString(yaml_error_type_t errt) {
@ -426,9 +426,9 @@ YAMLDocWriter::YAMLDocWriter(const char* classType, athena::io::IStreamReader* r
m_subStack.emplace_back(m_rootNode.get()); m_subStack.emplace_back(m_rootNode.get());
if (classType) { if (classType) {
YAMLNode* classVal = new YAMLNode(YAML_SCALAR_NODE); auto classVal = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
classVal->m_scalarString.assign(classType); classVal->m_scalarString.assign(classType);
m_rootNode->assignMapChild("DNAType", std::unique_ptr<YAMLNode>(classVal)); m_rootNode->assignMapChild("DNAType", std::move(classVal));
} }
} }
@ -633,7 +633,7 @@ std::unique_ptr<YAMLNode> YAMLDocReader::ParseEvents(athena::io::IStreamReader*
result = yaml_parser_parse(&m_parser, &event)) { result = yaml_parser_parse(&m_parser, &event)) {
if (!result) { if (!result) {
HandleYAMLParserError(&m_parser); HandleYAMLParserError(&m_parser);
return std::unique_ptr<YAMLNode>(); return nullptr;
} }
switch (event.type) { switch (event.type) {
case YAML_SCALAR_EVENT: { case YAML_SCALAR_EVENT: {
@ -641,7 +641,7 @@ std::unique_ptr<YAMLNode> YAMLDocReader::ParseEvents(athena::io::IStreamReader*
atWarning(fmt("YAML parser stack empty; skipping scalar node")); atWarning(fmt("YAML parser stack empty; skipping scalar node"));
break; break;
} }
std::unique_ptr<YAMLNode> newScalar(new YAMLNode(YAML_SCALAR_NODE)); auto newScalar = std::make_unique<YAMLNode>(YAML_SCALAR_NODE);
newScalar->m_scalarString.assign((char*)event.data.scalar.value, event.data.scalar.length); newScalar->m_scalarString.assign((char*)event.data.scalar.value, event.data.scalar.length);
if (nodeStack.empty()) if (nodeStack.empty())
retVal = std::move(newScalar); retVal = std::move(newScalar);