OpenMW
components/nif/recordptr.hpp
Go to the documentation of this file.
00001 /*
00002   OpenMW - The completely unofficial reimplementation of Morrowind
00003   Copyright (C) 2008-2010  Nicolay Korslund
00004   Email: < korslund@gmail.com >
00005   WWW: http://openmw.sourceforge.net/
00006 
00007   This file (record_ptr.h) is part of the OpenMW package.
00008 
00009   OpenMW is distributed as free software: you can redistribute it
00010   and/or modify it under the terms of the GNU General Public License
00011   version 3, as published by the Free Software Foundation.
00012 
00013   This program is distributed in the hope that it will be useful, but
00014   WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   version 3 along with this program. If not, see
00020   http://www.gnu.org/licenses/ .
00021 
00022  */
00023 
00024 #ifndef OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
00025 #define OPENMW_COMPONENTS_NIF_RECORDPTR_HPP
00026 
00027 #include "niffile.hpp"
00028 #include <vector>
00029 
00030 namespace Nif
00031 {
00032 
00037 template <class X>
00038 class RecordPtrT
00039 {
00040     union {
00041         intptr_t index;
00042         X* ptr;
00043     };
00044 
00045 public:
00046     RecordPtrT() : index(-2) {}
00047 
00049     void read(NIFStream *nif)
00050     {
00051         // Can only read the index once
00052         assert(index == -2);
00053 
00054         // Store the index for later
00055         index = nif->getInt();
00056         assert(index >= -1);
00057     }
00058 
00060     void post(NIFFile *nif)
00061     {
00062         if(index < 0)
00063             ptr = NULL;
00064         else
00065         {
00066             Record *r = nif->getRecord(index);
00067             // And cast it
00068             ptr = dynamic_cast<X*>(r);
00069             assert(ptr != NULL);
00070         }
00071     }
00072 
00074     const X* getPtr() const
00075     {
00076         assert(ptr != NULL);
00077         return ptr;
00078     }
00079     X* getPtr()
00080     {
00081         assert(ptr != NULL);
00082         return ptr;
00083     }
00084 
00085     const X& get() const
00086     { return *getPtr(); }
00087     X& get()
00088     { return *getPtr(); }
00089 
00091     const X* operator->() const
00092     { return getPtr(); }
00093     X* operator->()
00094     { return getPtr(); }
00095 
00097     bool empty() const
00098     { return ptr == NULL; }
00099 };
00100 
00105 template <class X>
00106 class RecordListT
00107 {
00108     typedef RecordPtrT<X> Ptr;
00109     std::vector<Ptr> list;
00110 
00111 public:
00112     void read(NIFStream *nif)
00113     {
00114         int len = nif->getInt();
00115         list.resize(len);
00116 
00117         for(size_t i=0;i < list.size();i++)
00118             list[i].read(nif);
00119     }
00120 
00121     void post(NIFFile *nif)
00122     {
00123         for(size_t i=0;i < list.size();i++)
00124             list[i].post(nif);
00125     }
00126 
00127     const Ptr& operator[](size_t index) const
00128     { return list.at(index); }
00129     Ptr& operator[](size_t index)
00130     { return list.at(index); }
00131 
00132     size_t length() const
00133     { return list.size(); }
00134 };
00135 
00136 
00137 class Node;
00138 class Extra;
00139 class Property;
00140 class NiUVData;
00141 class NiPosData;
00142 class NiVisData;
00143 class Controller;
00144 class Controlled;
00145 class NiSkinData;
00146 class NiFloatData;
00147 class NiMorphData;
00148 class NiPixelData;
00149 class NiColorData;
00150 class NiKeyframeData;
00151 class NiTriShapeData;
00152 class NiSkinInstance;
00153 class NiSourceTexture;
00154 class NiRotatingParticlesData;
00155 class NiAutoNormalParticlesData;
00156 
00157 typedef RecordPtrT<Node> NodePtr;
00158 typedef RecordPtrT<Extra> ExtraPtr;
00159 typedef RecordPtrT<NiUVData> NiUVDataPtr;
00160 typedef RecordPtrT<NiPosData> NiPosDataPtr;
00161 typedef RecordPtrT<NiVisData> NiVisDataPtr;
00162 typedef RecordPtrT<Controller> ControllerPtr;
00163 typedef RecordPtrT<Controlled> ControlledPtr;
00164 typedef RecordPtrT<NiSkinData> NiSkinDataPtr;
00165 typedef RecordPtrT<NiMorphData> NiMorphDataPtr;
00166 typedef RecordPtrT<NiPixelData> NiPixelDataPtr;
00167 typedef RecordPtrT<NiFloatData> NiFloatDataPtr;
00168 typedef RecordPtrT<NiColorData> NiColorDataPtr;
00169 typedef RecordPtrT<NiKeyframeData> NiKeyframeDataPtr;
00170 typedef RecordPtrT<NiTriShapeData> NiTriShapeDataPtr;
00171 typedef RecordPtrT<NiSkinInstance> NiSkinInstancePtr;
00172 typedef RecordPtrT<NiSourceTexture> NiSourceTexturePtr;
00173 typedef RecordPtrT<NiRotatingParticlesData> NiRotatingParticlesDataPtr;
00174 typedef RecordPtrT<NiAutoNormalParticlesData> NiAutoNormalParticlesDataPtr;
00175 
00176 typedef RecordListT<Node> NodeList;
00177 typedef RecordListT<Property> PropertyList;
00178 typedef RecordListT<NiSourceTexture> NiSourceTextureList;
00179 
00180 } // Namespace
00181 #endif