Check FormIdRefId value in constructor

7344-support-launching-the-example-suite
elsid 2 years ago
parent 4738f0ff4d
commit c97df7d770
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -177,6 +177,11 @@ namespace ESM
EXPECT_FALSE(formIdRefId < stringView);
}
TEST(ESMRefIdTest, formIdRefIdIndexShouldHaveOnly24SignificantBits)
{
EXPECT_THROW(FormIdRefId(FormId{ .mIndex = 1 << 25, .mContentFile = 0 }), std::invalid_argument);
}
TEST(ESMRefIdTest, canBeUsedAsMapKeyWithLookupByStringView)
{
const std::map<RefId, int, std::less<>> map({ { RefId::stringRefId("a"), 42 } });

@ -1,18 +1,24 @@
#include "formidrefid.hpp"
#include <cassert>
#include <ostream>
#include "serializerefid.hpp"
namespace ESM
{
namespace
{
std::uint64_t truncate(FormId value)
{
return (static_cast<std::uint64_t>(value.mContentFile) << 24) | value.mIndex;
}
}
std::string FormIdRefId::toString() const
{
std::string result;
assert((mValue.mIndex & 0xff000000) == 0);
size_t v = (static_cast<size_t>(mValue.mContentFile) << 24) | mValue.mIndex;
result.resize(getHexIntegralSize(v) + 2, '\0');
const std::uint64_t v = truncate(mValue);
result.resize(getHexIntegralSizeWith0x(v), '\0');
serializeHexIntegral(v, 0, result);
return result;
}
@ -20,8 +26,7 @@ namespace ESM
std::string FormIdRefId::toDebugString() const
{
std::string result;
assert((mValue.mIndex & 0xff000000) == 0);
size_t v = (static_cast<size_t>(mValue.mContentFile) << 24) | mValue.mIndex;
const std::uint64_t v = truncate(mValue);
serializeRefIdValue(v, formIdRefIdPrefix, result);
return result;
}

@ -3,6 +3,7 @@
#include <functional>
#include <iosfwd>
#include <stdexcept>
#include <components/esm/formid.hpp>
@ -13,9 +14,11 @@ namespace ESM
public:
constexpr FormIdRefId() = default;
constexpr explicit FormIdRefId(ESM::FormId value) noexcept
constexpr explicit FormIdRefId(ESM::FormId value)
: mValue(value)
{
if ((mValue.mIndex & 0xff000000) != 0)
throw std::invalid_argument("Invalid FormIdRefId index value: " + std::to_string(mValue.mIndex));
}
ESM::FormId getValue() const { return mValue; }

@ -63,7 +63,7 @@ namespace ESM
static RefId stringRefId(std::string_view value);
// Constructs RefId from FormId storing the value in-place.
static RefId formIdRefId(FormId value) noexcept { return RefId(FormIdRefId(value)); }
static RefId formIdRefId(FormId value) { return RefId(FormIdRefId(value)); }
// Constructs RefId from uint64 storing the value in-place. Should be used for generated records where id is a
// global counter.
@ -87,7 +87,7 @@ namespace ESM
{
}
constexpr RefId(FormIdRefId value) noexcept
constexpr RefId(FormIdRefId value)
: mValue(value)
{
}

Loading…
Cancel
Save