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