Fix implicitly-converted vector serialization

This commit is contained in:
Jack Andersen 2018-02-23 20:14:19 -10:00
parent 62b6d6792a
commit 17a0959dbd
3 changed files with 25 additions and 12 deletions

View File

@ -76,6 +76,15 @@ static inline constexpr bool __IsPODType()
std::is_convertible_v<T&, atVec4d&>; std::is_convertible_v<T&, atVec4d&>;
} }
template <class T>
using __CastPODType = typename std::conditional_t<std::is_convertible_v<T&, atVec2f&>, atVec2f,
std::conditional_t<std::is_convertible_v<T&, atVec3f&>, atVec3f,
std::conditional_t<std::is_convertible_v<T&, atVec4f&>, atVec4f,
std::conditional_t<std::is_convertible_v<T&, atVec2d&>, atVec2d,
std::conditional_t<std::is_convertible_v<T&, atVec3d&>, atVec3d,
std::conditional_t<std::is_convertible_v<T&, atVec4d&>, atVec4d,
T>>>>>>;
template <Endian DNAE> template <Endian DNAE>
static inline uint16_t __Read16(IStreamReader& r) static inline uint16_t __Read16(IStreamReader& r)
{ {
@ -138,7 +147,8 @@ struct BinarySize
/* Accessed via Enumerate, header */ /* Accessed via Enumerate, header */
s += 6; s += 6;
} }
BinarySize<PropType::None>::Do<T, DNAE>(id, var, s); using CastT = __CastPODType<T>;
BinarySize<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), s);
} }
template <class T, Endian DNAE> template <class T, Endian DNAE>
static typename std::enable_if_t<__IsDNARecord<T>() && PropOp != PropType::None> static typename std::enable_if_t<__IsDNARecord<T>() && PropOp != PropType::None>
@ -334,7 +344,8 @@ struct Read
static typename std::enable_if_t<__IsPODType<T>()> static typename std::enable_if_t<__IsPODType<T>()>
Do(const PropId& id, T& var, StreamT& r) Do(const PropId& id, T& var, StreamT& r)
{ {
Read<PropType::None>::Do<T, DNAE>(id, var, r); using CastT = __CastPODType<T>;
Read<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), r);
} }
template <class T, Endian DNAE> template <class T, Endian DNAE>
static typename std::enable_if_t<__IsDNARecord<T>() && PropOp == PropType::None> static typename std::enable_if_t<__IsDNARecord<T>() && PropOp == PropType::None>
@ -504,6 +515,7 @@ struct Write
static typename std::enable_if_t<__IsPODType<T>()> static typename std::enable_if_t<__IsPODType<T>()>
Do(const PropId& id, T& var, StreamT& w) Do(const PropId& id, T& var, StreamT& w)
{ {
using CastT = __CastPODType<T>;
if (PropOp != PropType::None) if (PropOp != PropType::None)
{ {
/* Accessed via Enumerate, header */ /* Accessed via Enumerate, header */
@ -512,10 +524,10 @@ struct Write
else else
__Write32<DNAE>(w, id.rcrc32); __Write32<DNAE>(w, id.rcrc32);
size_t binarySize = 0; size_t binarySize = 0;
BinarySize<PropType::None>::Do<T, DNAE>(id, var, binarySize); BinarySize<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), binarySize);
__Write16<DNAE>(w, atUint16(binarySize)); __Write16<DNAE>(w, atUint16(binarySize));
} }
Write<PropType::None>::Do<T, DNAE>(id, var, w); Write<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), w);
} }
template <class T, Endian DNAE> template <class T, Endian DNAE>
static typename std::enable_if_t<__IsDNARecord<T>() && PropOp != PropType::None> static typename std::enable_if_t<__IsDNARecord<T>() && PropOp != PropType::None>
@ -664,7 +676,8 @@ struct ReadYaml
static typename std::enable_if_t<__IsPODType<T>()> static typename std::enable_if_t<__IsPODType<T>()>
Do(const PropId& id, T& var, StreamT& r) Do(const PropId& id, T& var, StreamT& r)
{ {
ReadYaml<PropType::None>::Do<T, DNAE>(id, var, r); using CastT = __CastPODType<T>;
ReadYaml<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), r);
} }
template <class T, Endian DNAE> template <class T, Endian DNAE>
static typename std::enable_if_t<__IsDNARecord<T>()> static typename std::enable_if_t<__IsDNARecord<T>()>
@ -802,7 +815,8 @@ struct WriteYaml
static typename std::enable_if_t<__IsPODType<T>()> static typename std::enable_if_t<__IsPODType<T>()>
Do(const PropId& id, T& var, StreamT& w) Do(const PropId& id, T& var, StreamT& w)
{ {
WriteYaml<PropType::None>::Do<T, DNAE>(id, var, w); using CastT = __CastPODType<T>;
WriteYaml<PropType::None>::Do<CastT, DNAE>(id, static_cast<CastT&>(var), w);
} }
template <class T, Endian DNAE> template <class T, Endian DNAE>
static typename std::enable_if_t<__IsDNARecord<T>()> static typename std::enable_if_t<__IsDNARecord<T>()>

View File

@ -28,8 +28,7 @@ struct YAMLNode
yaml_node_type_t m_type; yaml_node_type_t m_type;
std::string m_scalarString; std::string m_scalarString;
std::vector<std::unique_ptr<YAMLNode>> m_seqChildren; std::vector<std::unique_ptr<YAMLNode>> m_seqChildren;
std::vector<std::pair<std::string, std::unique_ptr < YAMLNode>>> std::vector<std::pair<std::string, std::unique_ptr<YAMLNode>>> m_mapChildren;
m_mapChildren;
YAMLNodeStyle m_style = YAMLNodeStyle::Any; YAMLNodeStyle m_style = YAMLNodeStyle::Any;
YAMLNode(yaml_node_type_t type) : m_type(type) {} YAMLNode(yaml_node_type_t type) : m_type(type) {}

View File

@ -584,7 +584,7 @@ YAMLDocWriter::RecordRAII YAMLDocWriter::enterSubRecord(const char* name)
return {}; return {};
YAMLNode* newNode = new YAMLNode(YAML_MAPPING_NODE); YAMLNode* newNode = new YAMLNode(YAML_MAPPING_NODE);
if (curSub->m_type == YAML_MAPPING_NODE) if (curSub->m_type == YAML_MAPPING_NODE)
curSub->assignMapChild(name?std::string(name):std::string(), std::unique_ptr<YAMLNode>(newNode)); curSub->assignMapChild(name?name:std::string_view{}, std::unique_ptr<YAMLNode>(newNode));
else if (curSub->m_type == YAML_SEQUENCE_NODE) else if (curSub->m_type == YAML_SEQUENCE_NODE)
curSub->m_seqChildren.emplace_back(newNode); curSub->m_seqChildren.emplace_back(newNode);
m_subStack.push_back(newNode); m_subStack.push_back(newNode);
@ -629,7 +629,7 @@ YAMLDocWriter::VectorRAII YAMLDocWriter::enterSubVector(const char* name)
return {}; return {};
YAMLNode* newNode = new YAMLNode(YAML_SEQUENCE_NODE); YAMLNode* newNode = new YAMLNode(YAML_SEQUENCE_NODE);
if (curSub->m_type == YAML_MAPPING_NODE) if (curSub->m_type == YAML_MAPPING_NODE)
curSub->assignMapChild(name?std::string(name):std::string(), std::unique_ptr<YAMLNode>(newNode)); curSub->assignMapChild(name?name:std::string_view{}, std::unique_ptr<YAMLNode>(newNode));
else if (curSub->m_type == YAML_SEQUENCE_NODE) else if (curSub->m_type == YAML_SEQUENCE_NODE)
curSub->m_seqChildren.emplace_back(newNode); curSub->m_seqChildren.emplace_back(newNode);
m_subStack.push_back(newNode); m_subStack.push_back(newNode);
@ -647,7 +647,7 @@ void YAMLDocWriter::writeVal(const char* name, const INTYPE& val)
{ {
YAMLNode* curSub = m_subStack.back(); YAMLNode* curSub = m_subStack.back();
if (curSub->m_type == YAML_MAPPING_NODE) if (curSub->m_type == YAML_MAPPING_NODE)
curSub->assignMapChild(name?name:std::string(), std::move(ValToNode(val))); curSub->assignMapChild(name?name:std::string_view{}, std::move(ValToNode(val)));
else if (curSub->m_type == YAML_SEQUENCE_NODE) else if (curSub->m_type == YAML_SEQUENCE_NODE)
curSub->m_seqChildren.emplace_back(std::move(ValToNode(val))); curSub->m_seqChildren.emplace_back(std::move(ValToNode(val)));
} }
@ -669,7 +669,7 @@ void YAMLDocWriter::writeVal(const char* name, const INTYPE& val, size_t byteCou
{ {
YAMLNode* curSub = m_subStack.back(); YAMLNode* curSub = m_subStack.back();
if (curSub->m_type == YAML_MAPPING_NODE) if (curSub->m_type == YAML_MAPPING_NODE)
curSub->assignMapChild(name?name:std::string(), std::move(ValToNode(val, byteCount))); curSub->assignMapChild(name?name:std::string_view{}, std::move(ValToNode(val, byteCount)));
else if (curSub->m_type == YAML_SEQUENCE_NODE) else if (curSub->m_type == YAML_SEQUENCE_NODE)
curSub->m_seqChildren.emplace_back(std::move(ValToNode(val, byteCount))); curSub->m_seqChildren.emplace_back(std::move(ValToNode(val, byteCount)));
} }