mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-11 01:13:23 +00:00
Currently, we use a peculiar mapping of ESM classes by their std::type_info::name. This mapping is an undefined behaviour because std::type_info::name is strictly implementation defined. It could return a non-unique value on some platforms. With this PR we use the unsigned int sRecordId of the ESM class as a more efficient lookup type that does not build on undefined behaviour. We can expect marginally faster save-game loading with these changes as well.
129 lines
3.8 KiB
C++
129 lines
3.8 KiB
C++
#ifndef GAME_MWWORLD_LIVECELLREF_H
|
|
#define GAME_MWWORLD_LIVECELLREF_H
|
|
|
|
#include "cellref.hpp"
|
|
|
|
#include "refdata.hpp"
|
|
|
|
namespace ESM
|
|
{
|
|
struct ObjectState;
|
|
}
|
|
|
|
namespace MWWorld
|
|
{
|
|
class Ptr;
|
|
class ESMStore;
|
|
class Class;
|
|
|
|
/// Used to create pointers to hold any type of LiveCellRef<> object.
|
|
struct LiveCellRefBase
|
|
{
|
|
const Class *mClass;
|
|
|
|
/** Information about this instance, such as 3D location and rotation
|
|
* and individual type-dependent data.
|
|
*/
|
|
MWWorld::CellRef mRef;
|
|
|
|
/** runtime-data */
|
|
RefData mData;
|
|
|
|
LiveCellRefBase(unsigned int type, const ESM::CellRef &cref=ESM::CellRef());
|
|
/* Need this for the class to be recognized as polymorphic */
|
|
virtual ~LiveCellRefBase() { }
|
|
|
|
virtual void load (const ESM::ObjectState& state) = 0;
|
|
///< Load state into a LiveCellRef, that has already been initialised with base and class.
|
|
///
|
|
/// \attention Must not be called with an invalid \a state.
|
|
|
|
virtual void save (ESM::ObjectState& state) const = 0;
|
|
///< Save LiveCellRef state into \a state.
|
|
|
|
virtual std::string getTypeDescription() const { return ""; }
|
|
|
|
protected:
|
|
|
|
void loadImp (const ESM::ObjectState& state);
|
|
///< Load state into a LiveCellRef, that has already been initialised with base and
|
|
/// class.
|
|
///
|
|
/// \attention Must not be called with an invalid \a state.
|
|
|
|
void saveImp (ESM::ObjectState& state) const;
|
|
///< Save LiveCellRef state into \a state.
|
|
|
|
static bool checkStateImp (const ESM::ObjectState& state);
|
|
///< Check if state is valid and report errors.
|
|
///
|
|
/// \return Valid?
|
|
///
|
|
/// \note Does not check if the RefId exists.
|
|
};
|
|
|
|
inline bool operator== (const LiveCellRefBase& cellRef, const ESM::RefNum refNum)
|
|
{
|
|
return cellRef.mRef.getRefNum()==refNum;
|
|
}
|
|
|
|
/// A reference to one object (of any type) in a cell.
|
|
///
|
|
/// Constructing this with a CellRef instance in the constructor means that
|
|
/// in practice (where D is RefData) the possibly mutable data is copied
|
|
/// across to mData. If later adding data (such as position) to CellRef
|
|
/// this would have to be manually copied across.
|
|
template <typename X>
|
|
struct LiveCellRef : public LiveCellRefBase
|
|
{
|
|
LiveCellRef(const ESM::CellRef& cref, const X* b = nullptr)
|
|
: LiveCellRefBase(X::sRecordId, cref), mBase(b)
|
|
{}
|
|
|
|
LiveCellRef(const X* b = nullptr)
|
|
: LiveCellRefBase(X::sRecordId), mBase(b)
|
|
{}
|
|
|
|
// The object that this instance is based on.
|
|
const X* mBase;
|
|
|
|
void load (const ESM::ObjectState& state) override;
|
|
///< Load state into a LiveCellRef, that has already been initialised with base and class.
|
|
///
|
|
/// \attention Must not be called with an invalid \a state.
|
|
|
|
void save (ESM::ObjectState& state) const override;
|
|
///< Save LiveCellRef state into \a state.
|
|
|
|
std::string getTypeDescription() const override { return X::getRecordType(); }
|
|
|
|
static bool checkState (const ESM::ObjectState& state);
|
|
///< Check if state is valid and report errors.
|
|
///
|
|
/// \return Valid?
|
|
///
|
|
/// \note Does not check if the RefId exists.
|
|
};
|
|
|
|
template <typename X>
|
|
void LiveCellRef<X>::load (const ESM::ObjectState& state)
|
|
{
|
|
loadImp (state);
|
|
}
|
|
|
|
template <typename X>
|
|
void LiveCellRef<X>::save (ESM::ObjectState& state) const
|
|
{
|
|
saveImp (state);
|
|
}
|
|
|
|
template <typename X>
|
|
bool LiveCellRef<X>::checkState (const ESM::ObjectState& state)
|
|
{
|
|
return checkStateImp (state);
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|