OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
recordptr.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
2 #define OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
3 
4 #include "niffile.hpp"
5 #include "nifstream.hpp"
6 #include <vector>
7 
8 namespace Nif
9 {
10 
15 template <class X>
17 {
18  union {
20  X* ptr;
21  };
22 
23 public:
24  RecordPtrT() : index(-2) {}
25 
26  RecordPtrT(X* ptr) : ptr(ptr) {}
27 
29  void read(NIFStream *nif)
30  {
31  // Can only read the index once
32  assert(index == -2);
33 
34  // Store the index for later
35  index = nif->getInt();
36  assert(index >= -1);
37  }
38 
40  void post(NIFFile *nif)
41  {
42  if(index < 0)
43  ptr = NULL;
44  else
45  {
46  Record *r = nif->getRecord(index);
47  // And cast it
48  ptr = dynamic_cast<X*>(r);
49  assert(ptr != NULL);
50  }
51  }
52 
54  const X* getPtr() const
55  {
56  assert(ptr != NULL);
57  return ptr;
58  }
59  X* getPtr()
60  {
61  assert(ptr != NULL);
62  return ptr;
63  }
64 
65  const X& get() const
66  { return *getPtr(); }
67  X& get()
68  { return *getPtr(); }
69 
71  const X* operator->() const
72  { return getPtr(); }
74  { return getPtr(); }
75 
77  bool empty() const
78  { return ptr == NULL; }
79 };
80 
85 template <class X>
87 {
88  typedef RecordPtrT<X> Ptr;
89  std::vector<Ptr> list;
90 
91 public:
92  RecordListT() = default;
93 
94  RecordListT(std::vector<Ptr> list)
95  : list(std::move(list))
96  {}
97 
98  void read(NIFStream *nif)
99  {
100  int len = nif->getInt();
101  list.resize(len);
102 
103  for(size_t i=0;i < list.size();i++)
104  list[i].read(nif);
105  }
106 
107  void post(NIFFile *nif)
108  {
109  for(size_t i=0;i < list.size();i++)
110  list[i].post(nif);
111  }
112 
113  const Ptr& operator[](size_t index) const
114  { return list.at(index); }
115  Ptr& operator[](size_t index)
116  { return list.at(index); }
117 
118  size_t length() const
119  { return list.size(); }
120 };
121 
122 
123 class Node;
124 class Extra;
125 class Property;
126 class NiUVData;
127 class NiPosData;
128 class NiVisData;
129 class Controller;
130 class Controlled;
131 class NiSkinData;
132 class NiFloatData;
133 struct NiMorphData;
134 class NiPixelData;
135 class NiColorData;
136 struct NiKeyframeData;
137 class NiTriShapeData;
138 class NiSkinInstance;
139 class NiSourceTexture;
140 class NiRotatingParticlesData;
142 
143 typedef RecordPtrT<Node> NodePtr;
161 
165 
166 } // Namespace
167 #endif
Definition: niffile.hpp:40
Definition: recordptr.hpp:86
X * operator->()
Definition: recordptr.hpp:73
RecordPtrT< Controller > ControllerPtr
Definition: recordptr.hpp:148
RecordPtrT< Extra > ExtraPtr
Definition: recordptr.hpp:144
X * ptr
Definition: recordptr.hpp:20
int getInt()
Definition: nifstream.hpp:111
const Ptr & operator[](size_t index) const
Definition: recordptr.hpp:113
Base class for all records.
Definition: record.hpp:101
RecordPtrT< NiPixelData > NiPixelDataPtr
Definition: recordptr.hpp:152
RecordPtrT< NiSourceTexture > NiSourceTexturePtr
Definition: recordptr.hpp:158
RecordPtrT< NiFloatData > NiFloatDataPtr
Definition: recordptr.hpp:153
Record * getRecord(size_t index) const override
Get a given record.
Definition: niffile.hpp:91
std::vector< Ptr > list
Definition: recordptr.hpp:89
RecordPtrT< NiSkinData > NiSkinDataPtr
Definition: recordptr.hpp:150
size_t length() const
Definition: recordptr.hpp:118
RecordPtrT< NiAutoNormalParticlesData > NiAutoNormalParticlesDataPtr
Definition: recordptr.hpp:160
RecordPtrT< NiColorData > NiColorDataPtr
Definition: recordptr.hpp:154
const X * operator->() const
Syntactic sugar.
Definition: recordptr.hpp:71
RecordListT< Property > PropertyList
Definition: recordptr.hpp:163
const X * getPtr() const
Look up the actual object from the index.
Definition: recordptr.hpp:54
Definition: data.hpp:56
void post(NIFFile *nif)
Resolve index to pointer.
Definition: recordptr.hpp:40
void read(NIFStream *nif)
Read the index from the nif.
Definition: recordptr.hpp:29
RecordPtrT< NiUVData > NiUVDataPtr
Definition: recordptr.hpp:145
RecordPtrT< Controlled > ControlledPtr
Definition: recordptr.hpp:149
RecordPtrT(X *ptr)
Definition: recordptr.hpp:26
RecordPtrT< X > Ptr
Definition: recordptr.hpp:88
RecordPtrT< NiTriShapeData > NiTriShapeDataPtr
Definition: recordptr.hpp:156
RecordPtrT< NiRotatingParticlesData > NiRotatingParticlesDataPtr
Definition: recordptr.hpp:159
RecordPtrT< NiSkinInstance > NiSkinInstancePtr
Definition: recordptr.hpp:157
RecordPtrT< NiKeyframeData > NiKeyframeDataPtr
Definition: recordptr.hpp:155
RecordListT(std::vector< Ptr > list)
Definition: recordptr.hpp:94
RecordPtrT< NiMorphData > NiMorphDataPtr
Definition: recordptr.hpp:151
void post(NIFFile *nif)
Definition: recordptr.hpp:107
Definition: recordptr.hpp:16
RecordPtrT< NiVisData > NiVisDataPtr
Definition: recordptr.hpp:147
RecordListT< NiSourceTexture > NiSourceTextureList
Definition: recordptr.hpp:164
RecordPtrT< NiPosData > NiPosDataPtr
Definition: recordptr.hpp:146
RecordListT< Node > NodeList
Definition: recordptr.hpp:162
intptr_t index
Definition: recordptr.hpp:19
Definition: nifstream.hpp:83
RecordPtrT< Node > NodePtr
Definition: recordptr.hpp:141
void read(NIFStream *nif)
Definition: recordptr.hpp:98
RecordListT()=default
X * getPtr()
Definition: recordptr.hpp:59
bool empty() const
Pointers are allowed to be empty.
Definition: recordptr.hpp:77
RecordPtrT()
Definition: recordptr.hpp:24
Ptr & operator[](size_t index)
Definition: recordptr.hpp:115