1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00
OpenMW/apps/openmw/mwworld/ptrregistry.hpp
elsid 86db640c8b
Increment revision on clearing PtrRegistry
Setting it to 0 may not lead to the change detected by clients. Potentially
mRevision may already have value 0. For example on overflow.
2023-05-28 13:04:20 +02:00

60 lines
1.3 KiB
C++

#ifndef OPENMW_APPS_OPENMW_MWWORLD_PTRREGISTRY_H
#define OPENMW_APPS_OPENMW_MWWORLD_PTRREGISTRY_H
#include "ptr.hpp"
#include "components/esm3/cellref.hpp"
#include <unordered_map>
namespace MWWorld
{
class PtrRegistry
{
public:
std::size_t getRevision() const { return mRevision; }
ESM::RefNum getLastGenerated() const { return mLastGenerated; }
auto begin() const { return mIndex.cbegin(); }
auto end() const { return mIndex.cend(); }
Ptr getOrDefault(ESM::RefNum refNum) const
{
const auto it = mIndex.find(refNum);
if (it != mIndex.end())
return it->second;
return Ptr();
}
void setLastGenerated(ESM::RefNum v) { mLastGenerated = v; }
void clear()
{
mIndex.clear();
mLastGenerated = ESM::RefNum{};
++mRevision;
}
void insert(const Ptr& ptr)
{
mIndex[ptr.getCellRef().getOrAssignRefNum(mLastGenerated)] = ptr;
++mRevision;
}
void remove(const Ptr& ptr)
{
mIndex.erase(ptr.getCellRef().getRefNum());
++mRevision;
}
private:
std::size_t mRevision = 0;
std::unordered_map<ESM::RefNum, Ptr> mIndex;
ESM::RefNum mLastGenerated;
};
}
#endif