1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 08:53:52 +00:00
openmw/components/nif/recordptr.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
5.8 KiB
C++
Raw Normal View History

#ifndef OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
#define OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
2010-01-06 11:28:37 +00:00
#include "niffile.hpp"
#include "nifstream.hpp"
2010-01-06 14:00:08 +00:00
#include <vector>
2010-01-06 11:28:37 +00:00
namespace Nif
{
2010-01-06 14:00:08 +00:00
/** A reference to another record. It is read as an index from the
NIF, and later looked up in the index table to get an actual
pointer.
*/
2010-01-06 11:28:37 +00:00
template <class X>
class RecordPtrT
{
union
{
intptr_t index;
X* ptr;
};
2010-01-06 11:28:37 +00:00
public:
RecordPtrT()
: index(-2)
{
}
2010-01-06 14:00:08 +00:00
2018-07-08 19:22:34 +00:00
RecordPtrT(X* ptr)
: ptr(ptr)
{
}
/// Read the index from the nif
void read(NIFStream* nif)
{
// Can only read the index once
assert(index == -2);
2010-01-06 11:28:37 +00:00
// Store the index for later
index = nif->getInt();
assert(index >= -1);
}
/// Resolve index to pointer
void post(Reader& nif)
{
if (index < 0)
2018-10-09 06:21:12 +00:00
ptr = nullptr;
else
2022-09-22 18:26:05 +00:00
{
Record* r = nif.getRecord(index);
// And cast it
ptr = dynamic_cast<X*>(r);
2018-10-09 06:21:12 +00:00
assert(ptr != nullptr);
2022-09-22 18:26:05 +00:00
}
}
/// Look up the actual object from the index
const X* getPtr() const
2022-09-22 18:26:05 +00:00
{
2018-10-09 06:21:12 +00:00
assert(ptr != nullptr);
return ptr;
2022-09-22 18:26:05 +00:00
}
X* getPtr()
{
2018-10-09 06:21:12 +00:00
assert(ptr != nullptr);
return ptr;
}
const X& get() const { return *getPtr(); }
2012-07-11 13:37:17 +00:00
X& get() { return *getPtr(); }
2010-01-06 14:00:08 +00:00
/// Syntactic sugar
2018-07-08 19:22:34 +00:00
const X* operator->() const { return getPtr(); }
X* operator->() { return getPtr(); }
2018-07-08 19:22:34 +00:00
/// Pointers are allowed to be empty
bool empty() const { return ptr == nullptr; }
2022-09-22 18:26:05 +00:00
};
2018-07-08 19:22:34 +00:00
/** A list of references to other records. These are read as a list,
and later converted to pointers as needed. Not an optimized
2010-01-06 14:00:08 +00:00
implementation.
*/
2010-01-06 14:00:08 +00:00
template <class X>
using RecordListT = std::vector<RecordPtrT<X>>;
2022-09-22 18:26:05 +00:00
template <class T>
void readRecordList(NIFStream* nif, RecordListT<T>& list)
{
const int length = nif->getInt();
2022-09-22 18:26:05 +00:00
2022-09-18 14:00:00 +00:00
if (length < 0)
throw std::runtime_error("Negative NIF record list length: " + std::to_string(length));
list.resize(static_cast<std::size_t>(length));
2022-09-22 18:26:05 +00:00
for (auto& value : list)
value.read(nif);
}
2022-09-22 18:26:05 +00:00
template <class T>
void postRecordList(Reader& nif, RecordListT<T>& list)
{
for (auto& value : list)
value.post(nif);
}
2022-09-22 18:26:05 +00:00
2020-12-15 22:06:05 +00:00
struct Node;
struct Extra;
struct Property;
struct NiUVData;
struct NiPosData;
struct NiVisData;
struct Controller;
struct Named;
struct NiSkinData;
struct NiFloatData;
struct NiMorphData;
2020-12-15 22:06:05 +00:00
struct NiPixelData;
struct NiColorData;
struct NiKeyframeData;
2020-12-15 22:06:05 +00:00
struct NiTriStripsData;
struct NiSkinInstance;
struct NiSourceTexture;
struct NiPalette;
struct NiParticleModifier;
2020-11-07 00:40:21 +00:00
struct NiBoolData;
2020-11-07 01:04:46 +00:00
struct NiSkinPartition;
struct BSShaderTextureSet;
struct NiGeometryData;
struct BSShaderProperty;
2020-12-15 22:06:05 +00:00
struct NiAlphaProperty;
struct NiCollisionObject;
struct bhkWorldObject;
struct bhkShape;
struct bhkSerializable;
struct hkPackedNiTriStripsData;
struct NiAccumulator;
2022-09-14 21:58:10 +00:00
struct NiInterpolator;
2023-02-07 21:26:40 +00:00
struct NiStringPalette;
struct NiControllerManager;
struct NiBlendInterpolator;
2023-02-08 20:35:22 +00:00
struct NiDefaultAVObjectPalette;
struct NiControllerSequence;
2022-09-22 18:26:05 +00:00
using NodePtr = RecordPtrT<Node>;
using ExtraPtr = RecordPtrT<Extra>;
using NiUVDataPtr = RecordPtrT<NiUVData>;
using NiPosDataPtr = RecordPtrT<NiPosData>;
using NiVisDataPtr = RecordPtrT<NiVisData>;
using ControllerPtr = RecordPtrT<Controller>;
using NamedPtr = RecordPtrT<Named>;
using NiSkinDataPtr = RecordPtrT<NiSkinData>;
using NiMorphDataPtr = RecordPtrT<NiMorphData>;
using NiPixelDataPtr = RecordPtrT<NiPixelData>;
using NiFloatDataPtr = RecordPtrT<NiFloatData>;
using NiColorDataPtr = RecordPtrT<NiColorData>;
using NiKeyframeDataPtr = RecordPtrT<NiKeyframeData>;
using NiSkinInstancePtr = RecordPtrT<NiSkinInstance>;
using NiSourceTexturePtr = RecordPtrT<NiSourceTexture>;
using NiPalettePtr = RecordPtrT<NiPalette>;
using NiParticleModifierPtr = RecordPtrT<NiParticleModifier>;
2020-11-07 00:40:21 +00:00
using NiBoolDataPtr = RecordPtrT<NiBoolData>;
2020-11-07 01:04:46 +00:00
using NiSkinPartitionPtr = RecordPtrT<NiSkinPartition>;
using BSShaderTextureSetPtr = RecordPtrT<BSShaderTextureSet>;
using NiGeometryDataPtr = RecordPtrT<NiGeometryData>;
using BSShaderPropertyPtr = RecordPtrT<BSShaderProperty>;
using NiAlphaPropertyPtr = RecordPtrT<NiAlphaProperty>;
using NiCollisionObjectPtr = RecordPtrT<NiCollisionObject>;
using bhkWorldObjectPtr = RecordPtrT<bhkWorldObject>;
using bhkShapePtr = RecordPtrT<bhkShape>;
using hkPackedNiTriStripsDataPtr = RecordPtrT<hkPackedNiTriStripsData>;
using NiAccumulatorPtr = RecordPtrT<NiAccumulator>;
2022-09-14 21:58:10 +00:00
using NiInterpolatorPtr = RecordPtrT<NiInterpolator>;
2023-02-07 21:26:40 +00:00
using NiStringPalettePtr = RecordPtrT<NiStringPalette>;
using NiControllerManagerPtr = RecordPtrT<NiControllerManager>;
using NiBlendInterpolatorPtr = RecordPtrT<NiBlendInterpolator>;
2023-02-08 20:35:22 +00:00
using NiDefaultAVObjectPalettePtr = RecordPtrT<NiDefaultAVObjectPalette>;
2022-09-22 18:26:05 +00:00
using NodeList = RecordListT<Node>;
using PropertyList = RecordListT<Property>;
using ExtraList = RecordListT<Extra>;
using NiSourceTextureList = RecordListT<NiSourceTexture>;
using NiInterpolatorList = RecordListT<NiInterpolator>;
using NiTriStripsDataList = RecordListT<NiTriStripsData>;
using bhkShapeList = RecordListT<bhkShape>;
using bhkSerializableList = RecordListT<bhkSerializable>;
2023-02-08 20:35:22 +00:00
using NiControllerSequenceList = RecordListT<NiControllerSequence>;
2010-01-06 11:28:37 +00:00
} // Namespace
#endif