Use template specialization for writeHNT to write raw arrays

To avoid passing explicit size argument where it's possible.
pull/578/head
elsid 5 years ago
parent 3bd2c114a7
commit ef4a7089e4
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -356,7 +356,7 @@ void CSMDoc::WriteCellCollectionStage::perform (int stage, Messages& messages)
istream >> ignore >> moved.mTarget[0] >> moved.mTarget[1]; istream >> ignore >> moved.mTarget[0] >> moved.mTarget[1];
refRecord.mRefNum.save (writer, false, "MVRF"); refRecord.mRefNum.save (writer, false, "MVRF");
writer.writeHNT ("CNDT", moved.mTarget, 8); writer.writeHNT ("CNDT", moved.mTarget);
} }
refRecord.save (writer, false, false, ref.mState == CSMWorld::RecordBase::State_Deleted); refRecord.save (writer, false, false, ref.mState == CSMWorld::RecordBase::State_Deleted);

@ -82,17 +82,23 @@ class ESMWriter
endRecord(name); endRecord(name);
} }
private: template<typename T, std::size_t size>
void writeHNT(const std::string& name, const T (&data)[size])
{
startSubRecord(name);
writeT(data);
endRecord(name);
}
// Prevent using writeHNT with strings. This already happened by accident and results in // Prevent using writeHNT with strings. This already happened by accident and results in
// state being discarded without any error on writing or reading it. :( // state being discarded without any error on writing or reading it. :(
// writeHNString and friends must be used instead. // writeHNString and friends must be used instead.
void writeHNT(const std::string& name, const std::string& data) void writeHNT(const std::string& name, const std::string& data) = delete;
{
} void writeT(const std::string& data) = delete;
void writeT(const std::string& data)
{ template<typename T, std::size_t size>
} void writeHNT(const std::string& name, const T (&data)[size], int) = delete;
public:
template<typename T> template<typename T>
void writeHNT(const std::string& name, const T& data, int size) void writeHNT(const std::string& name, const T& data, int size)
@ -108,6 +114,12 @@ public:
write((char*)&data, sizeof(T)); write((char*)&data, sizeof(T));
} }
template<typename T, std::size_t size>
void writeT(const T (&data)[size])
{
write(reinterpret_cast<const char*>(data), size * sizeof(T));
}
template<typename T> template<typename T>
void writeT(const T& data, size_t size) void writeT(const T& data, size_t size)
{ {

@ -127,7 +127,7 @@ namespace ESM
if (mLandData) if (mLandData)
{ {
if (mDataTypes & Land::DATA_VNML) { if (mDataTypes & Land::DATA_VNML) {
esm.writeHNT("VNML", mLandData->mNormals, sizeof(mLandData->mNormals)); esm.writeHNT("VNML", mLandData->mNormals);
} }
if (mDataTypes & Land::DATA_VHGT) { if (mDataTypes & Land::DATA_VHGT) {
VHGT offsets; VHGT offsets;
@ -175,18 +175,18 @@ namespace ESM
wnam[row * LAND_GLOBAL_MAP_LOD_SIZE_SQRT + col] = static_cast<signed char>(height); wnam[row * LAND_GLOBAL_MAP_LOD_SIZE_SQRT + col] = static_cast<signed char>(height);
} }
} }
esm.writeHNT("WNAM", wnam, 81); esm.writeHNT("WNAM", wnam);
} }
if (mLandData) if (mLandData)
{ {
if (mDataTypes & Land::DATA_VCLR) { if (mDataTypes & Land::DATA_VCLR) {
esm.writeHNT("VCLR", mLandData->mColours, 3*LAND_NUM_VERTS); esm.writeHNT("VCLR", mLandData->mColours);
} }
if (mDataTypes & Land::DATA_VTEX) { if (mDataTypes & Land::DATA_VTEX) {
uint16_t vtex[LAND_NUM_TEXTURES]; uint16_t vtex[LAND_NUM_TEXTURES];
transposeTextureData(mLandData->mTextures, vtex); transposeTextureData(mLandData->mTextures, vtex);
esm.writeHNT("VTEX", vtex, sizeof(vtex)); esm.writeHNT("VTEX", vtex);
} }
} }

@ -58,7 +58,7 @@ void ESM::Player::save (ESMWriter &esm) const
mCellId.save (esm); mCellId.save (esm);
esm.writeHNT ("LKEP", mLastKnownExteriorPosition, 12); esm.writeHNT ("LKEP", mLastKnownExteriorPosition);
if (mHasMark) if (mHasMark)
{ {

Loading…
Cancel
Save