mirror of https://github.com/OpenMW/openmw.git
Merge ESM::RefNum and ESM4::FormId
parent
2365ba2ce0
commit
f09a689a4f
@ -0,0 +1,15 @@
|
||||
#include "formid.hpp"
|
||||
|
||||
std::string ESM::FormId::toString() const
|
||||
{
|
||||
return std::to_string(mIndex) + "_" + std::to_string(mContentFile);
|
||||
}
|
||||
|
||||
uint32_t ESM::FormId::toUint32() const
|
||||
{
|
||||
if (isSet() && !hasContentFile())
|
||||
throw std::runtime_error("Generated FormId can not be converted to 32bit format");
|
||||
if (mContentFile > 0xfe)
|
||||
throw std::runtime_error("FormId with mContentFile > 0xFE can not be converted to 32bit format");
|
||||
return (mIndex & 0xffffff) | ((hasContentFile() ? mContentFile : 0xff) << 24);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
#ifndef COMPONENT_ESM_FORMID_H
|
||||
#define COMPONENT_ESM_FORMID_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
namespace ESM
|
||||
{
|
||||
using FormId32 = uint32_t;
|
||||
struct FormId
|
||||
{
|
||||
uint32_t mIndex = 0;
|
||||
int32_t mContentFile = -1;
|
||||
|
||||
bool hasContentFile() const { return mContentFile >= 0; }
|
||||
bool isSet() const { return mIndex != 0 || mContentFile != -1; }
|
||||
|
||||
// Used in ESM4 as a null reference
|
||||
bool isZero() const { return mIndex == 0 && mContentFile == 0; }
|
||||
|
||||
std::string toString() const;
|
||||
FormId32 toUint32() const;
|
||||
static FormId fromUint32(FormId32 v) { return { v & 0xffffff, static_cast<int32_t>(v >> 24) }; }
|
||||
};
|
||||
|
||||
inline constexpr bool operator==(const FormId& left, const FormId& right)
|
||||
{
|
||||
return left.mIndex == right.mIndex && left.mContentFile == right.mContentFile;
|
||||
}
|
||||
|
||||
inline constexpr bool operator<(const FormId& left, const FormId& right)
|
||||
{
|
||||
return std::tie(left.mIndex, left.mContentFile) < std::tie(right.mIndex, right.mContentFile);
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& stream, const FormId& formId)
|
||||
{
|
||||
return stream << formId.toString();
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
// Needed to use ESM::FormId as a key in std::unordered_map
|
||||
template <>
|
||||
struct hash<ESM::FormId>
|
||||
{
|
||||
size_t operator()(const ESM::FormId& formId) const
|
||||
{
|
||||
static_assert(sizeof(ESM::FormId) == sizeof(size_t));
|
||||
size_t s;
|
||||
memcpy(&s, &formId, sizeof(size_t));
|
||||
return hash<size_t>()(s);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // COMPONENT_ESM_FORMID_H
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue