1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-21 21:17:49 +02:00

Simplified integer serialization code

This commit is contained in:
Ivan Savenko 2024-05-16 18:53:49 +00:00
parent 84bc6c42db
commit 42f7a48fa4
5 changed files with 16 additions and 33 deletions

View File

@ -197,7 +197,7 @@ public:
void serialize(Handler & h)
{
std::string key;
auto sz = stringsLocalizations.size();
int64_t sz = stringsLocalizations.size();
h & sz;
if(h.saving)
{

View File

@ -270,7 +270,7 @@ void CGCreature::initObj(CRandomGenerator & rand)
}
}
temppower = stacks[SlotID(0)]->count * static_cast<ui64>(1000);
temppower = stacks[SlotID(0)]->count * static_cast<int64_t>(1000);
refusedJoining = false;
}

View File

@ -35,7 +35,7 @@ public:
ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
bool neverFlees = false; //if true, the troops will never flee
bool notGrowingTeam = false; //if true, number of units won't grow
ui64 temppower = 0; //used to handle fractional stack growth for tiny stacks
int64_t temppower = 0; //used to handle fractional stack growth for tiny stacks
bool refusedJoining = false;

View File

@ -177,12 +177,9 @@ public:
return * this;
}
template< typename IntegerType>
void loadEncodedInteger(IntegerType & value)
int64_t loadEncodedInteger()
{
using UnsignedType = std::make_unsigned_t<IntegerType>;
UnsignedType valueUnsigned = 0;
uint64_t valueUnsigned = 0;
uint_fast8_t offset = 0;
for (;;)
@ -198,18 +195,11 @@ public:
else
{
valueUnsigned |= (byteValue & 0x3f) << offset;
if constexpr(std::is_signed_v<IntegerType>)
{
bool isNegative = (byteValue & 0x40) != 0;
if (isNegative)
value = -valueUnsigned;
else
value = valueUnsigned;
}
bool isNegative = (byteValue & 0x40) != 0;
if (isNegative)
return -static_cast<int64_t>(valueUnsigned);
else
value = valueUnsigned;
return;
return valueUnsigned;
}
}
}
@ -229,8 +219,9 @@ public:
}
else
{
static_assert(!std::is_same_v<uint64_t, T>, "Serialization of unsigned 64-bit value may not work in some cases");
if (hasFeature(Version::COMPACT_INTEGER_SERIALIZATION))
loadEncodedInteger(data);
data = loadEncodedInteger();
else
this->read(static_cast<void *>(&data), sizeof(data), reverseEndianness);
}

View File

@ -141,15 +141,9 @@ public:
return * this;
}
template< typename IntegerType>
void saveEncodedInteger(const IntegerType & value)
void saveEncodedInteger(int64_t value)
{
using UnsignedType = std::make_unsigned_t<IntegerType>;
UnsignedType valueUnsigned;
if constexpr(std::is_signed_v<IntegerType>)
valueUnsigned = std::abs(value);
else
valueUnsigned = value;
uint64_t valueUnsigned = std::abs(value);
while (valueUnsigned > 0x3f)
{
@ -159,11 +153,9 @@ public:
}
uint8_t lastByteValue = valueUnsigned & 0x3f;
if constexpr(std::is_signed_v<IntegerType>)
{
if (value < 0)
lastByteValue |= 0x40;
}
if (value < 0)
lastByteValue |= 0x40;
save(lastByteValue);
}