From b4b134d55bdf1a60ba5355bc4942b3730204a410 Mon Sep 17 00:00:00 2001 From: parax0 Date: Sun, 13 Dec 2015 14:06:24 -0700 Subject: [PATCH] Added a macro for declaring resource types to allow TResPtr to work without dynamic_cast --- Core/TResPtr.h | 11 +++++++---- Resource/CAnimSet.cpp | 5 ----- Resource/CAnimSet.h | 2 +- Resource/CCollisionMeshGroup.cpp | 5 ----- Resource/CCollisionMeshGroup.h | 2 +- Resource/CFont.cpp | 5 ----- Resource/CFont.h | 2 +- Resource/CGameArea.cpp | 5 ----- Resource/CGameArea.h | 2 +- Resource/CResource.cpp | 5 ----- Resource/CResource.h | 18 +++++++++++++++++- Resource/CScan.cpp | 5 ----- Resource/CScan.h | 2 +- Resource/CStringTable.cpp | 5 ----- Resource/CStringTable.h | 2 +- Resource/CTexture.cpp | 5 ----- Resource/CTexture.h | 2 +- Resource/CWorld.cpp | 5 ----- Resource/CWorld.h | 2 +- Resource/model/CBasicModel.cpp | 5 ----- Resource/model/CBasicModel.h | 2 +- 21 files changed, 33 insertions(+), 64 deletions(-) diff --git a/Core/TResPtr.h b/Core/TResPtr.h index d55198ec..131f4968 100644 --- a/Core/TResPtr.h +++ b/Core/TResPtr.h @@ -60,16 +60,19 @@ public: TResPtr& operator=(void *pPtr) { // todo: this probably crashes if you try to pass a non-CResource-derived pointer. is there a safer way? - // dynamic cast may be slow, but it's the only way I can think of to do type-checking without making a - // zillion redundant static functions on every resource class if (mpRes) mpRes->Release(); CResource *pRes = (CResource*) pPtr; - mpRes = dynamic_cast((CResource*) pRes); - if (mpRes) + if (ResType::StaticType() == pRes->Type()) + { + mpRes = (ResType*) pRes; mpRes->Lock(); + } + + else + mpRes = nullptr; return *this; } diff --git a/Resource/CAnimSet.cpp b/Resource/CAnimSet.cpp index 856dde85..9b79b4c9 100644 --- a/Resource/CAnimSet.cpp +++ b/Resource/CAnimSet.cpp @@ -9,11 +9,6 @@ CAnimSet::~CAnimSet() { } -EResType CAnimSet::Type() -{ - return eAnimSet; -} - u32 CAnimSet::getNodeCount() { return nodes.size(); diff --git a/Resource/CAnimSet.h b/Resource/CAnimSet.h index cec0a902..0da4818b 100644 --- a/Resource/CAnimSet.h +++ b/Resource/CAnimSet.h @@ -10,6 +10,7 @@ // will expand later! this is where animation support will come in class CAnimSet : public CResource { + DECLARE_RESOURCE_TYPE(eAnimSet) friend class CAnimSetLoader; struct SNode @@ -26,7 +27,6 @@ class CAnimSet : public CResource public: CAnimSet(); ~CAnimSet(); - EResType Type(); u32 getNodeCount(); TString getNodeName(u32 node); diff --git a/Resource/CCollisionMeshGroup.cpp b/Resource/CCollisionMeshGroup.cpp index a79dff9b..92123008 100644 --- a/Resource/CCollisionMeshGroup.cpp +++ b/Resource/CCollisionMeshGroup.cpp @@ -10,11 +10,6 @@ CCollisionMeshGroup::~CCollisionMeshGroup() delete *it; } -EResType CCollisionMeshGroup::Type() -{ - return eCollisionMeshGroup; -} - u32 CCollisionMeshGroup::NumMeshes() { return mMeshes.size(); diff --git a/Resource/CCollisionMeshGroup.h b/Resource/CCollisionMeshGroup.h index 4e294cfc..c357c097 100644 --- a/Resource/CCollisionMeshGroup.h +++ b/Resource/CCollisionMeshGroup.h @@ -8,12 +8,12 @@ class CCollisionMeshGroup : public CResource { + DECLARE_RESOURCE_TYPE(eCollisionMeshGroup) std::vector mMeshes; public: CCollisionMeshGroup(); ~CCollisionMeshGroup(); - EResType Type(); u32 NumMeshes(); CCollisionMesh* MeshByIndex(u32 index); diff --git a/Resource/CFont.cpp b/Resource/CFont.cpp index dd526338..5af7706d 100644 --- a/Resource/CFont.cpp +++ b/Resource/CFont.cpp @@ -16,11 +16,6 @@ CFont::~CFont() { } -EResType CFont::Type() -{ - return eFont; -} - inline float PtsToFloat(s32 pt) { // This is a bit of an arbitrary number but it works diff --git a/Resource/CFont.h b/Resource/CFont.h index 7a4b06b5..dc51b25f 100644 --- a/Resource/CFont.h +++ b/Resource/CFont.h @@ -18,6 +18,7 @@ class CRenderer; class CFont : public CResource { + DECLARE_RESOURCE_TYPE(eFont) friend class CFontLoader; static CDynamicVertexBuffer smGlyphVertices; // This is the vertex buffer used to draw glyphs. It has two attributes - Pos and Tex0. Tex0 should be updated for each glyph. static CIndexBuffer smGlyphIndices; // This is the index buffer used to draw glyphs. It uses a triangle strip. @@ -59,7 +60,6 @@ class CFont : public CResource public: CFont(); ~CFont(); - EResType Type(); CResource* MakeCopy(CResCache *pCopyCache); CVector2f RenderString(const TString& String, CRenderer *pRenderer, float AspectRatio, CVector2f Position = CVector2f(0,0), diff --git a/Resource/CGameArea.cpp b/Resource/CGameArea.cpp index 6a70d60f..fc1f5f4f 100644 --- a/Resource/CGameArea.cpp +++ b/Resource/CGameArea.cpp @@ -27,11 +27,6 @@ CGameArea::~CGameArea() delete mLightLayers[lyr][lit]; } -EResType CGameArea::Type() -{ - return eArea; -} - void CGameArea::AddWorldModel(CModel *mdl) { mTerrainModels.push_back(mdl); diff --git a/Resource/CGameArea.h b/Resource/CGameArea.h index 7d9f158a..7695b519 100644 --- a/Resource/CGameArea.h +++ b/Resource/CGameArea.h @@ -16,6 +16,7 @@ class CScriptObject; class CGameArea : public CResource { + DECLARE_RESOURCE_TYPE(eArea) friend class CAreaLoader; u32 mVertexCount; @@ -41,7 +42,6 @@ class CGameArea : public CResource public: CGameArea(); ~CGameArea(); - EResType Type(); void AddWorldModel(CModel *mdl); void MergeTerrain(); diff --git a/Resource/CResource.cpp b/Resource/CResource.cpp index 66033858..bbee8cc0 100644 --- a/Resource/CResource.cpp +++ b/Resource/CResource.cpp @@ -11,11 +11,6 @@ CResource::~CResource() { } -EResType CResource::Type() -{ - return eResource; -} - TString CResource::Source() { return mResSource.GetFileName(); diff --git a/Resource/CResource.h b/Resource/CResource.h index e7f3fd3d..140e4cf4 100644 --- a/Resource/CResource.h +++ b/Resource/CResource.h @@ -9,8 +9,25 @@ class CResCache; +// This macro creates functions that allow us to easily identify this resource type. +// Must be included on every CResource subclass. +#define DECLARE_RESOURCE_TYPE(ResTypeEnum) \ +public: \ + virtual EResType Type() const \ + { \ + return ResTypeEnum; \ + } \ + \ + static EResType StaticType() \ + { \ + return ResTypeEnum; \ + } \ + \ +private: \ + class CResource { + DECLARE_RESOURCE_TYPE(eResource) friend class CResCache; TString mResSource; @@ -20,7 +37,6 @@ class CResource public: CResource(); virtual ~CResource(); - virtual EResType Type(); TString Source(); TString FullSource(); CUniqueID ResID(); diff --git a/Resource/CScan.cpp b/Resource/CScan.cpp index ae37d359..01fe7a22 100644 --- a/Resource/CScan.cpp +++ b/Resource/CScan.cpp @@ -13,11 +13,6 @@ CScan::~CScan() { } -EResType CScan::Type() -{ - return eScan; -} - EGame CScan::Version() { return mVersion; diff --git a/Resource/CScan.h b/Resource/CScan.h index 1380ab4c..fff1edc7 100644 --- a/Resource/CScan.h +++ b/Resource/CScan.h @@ -8,6 +8,7 @@ class CScan : public CResource { + DECLARE_RESOURCE_TYPE(eScan) friend class CScanLoader; public: @@ -32,7 +33,6 @@ private: public: CScan(); ~CScan(); - EResType Type(); EGame Version(); CStringTable* ScanText(); bool IsImportant(); diff --git a/Resource/CStringTable.cpp b/Resource/CStringTable.cpp index 506bc764..e5b70b25 100644 --- a/Resource/CStringTable.cpp +++ b/Resource/CStringTable.cpp @@ -8,11 +8,6 @@ CStringTable::~CStringTable() { } -EResType CStringTable::Type() -{ - return eStringTable; -} - CResource* CStringTable::MakeCopy(CResCache*) { // Not using parameter 1 (CResCache* - pResCache) diff --git a/Resource/CStringTable.h b/Resource/CStringTable.h index 4c1147a1..56bd33f0 100644 --- a/Resource/CStringTable.h +++ b/Resource/CStringTable.h @@ -9,6 +9,7 @@ class CStringTable : public CResource { + DECLARE_RESOURCE_TYPE(eStringTable) friend class CStringLoader; std::vector mStringNames; @@ -24,7 +25,6 @@ class CStringTable : public CResource public: CStringTable(); ~CStringTable(); - EResType Type(); CResource* MakeCopy(CResCache *pCopyCache); // Getters diff --git a/Resource/CTexture.cpp b/Resource/CTexture.cpp index 4585cd6e..20884d91 100644 --- a/Resource/CTexture.cpp +++ b/Resource/CTexture.cpp @@ -53,11 +53,6 @@ CTexture::~CTexture() DeleteBuffers(); } -EResType CTexture::Type() -{ - return eTexture; -} - bool CTexture::BufferGL() { glGenTextures(1, &mTextureID); diff --git a/Resource/CTexture.h b/Resource/CTexture.h index 9a197160..2550d5db 100644 --- a/Resource/CTexture.h +++ b/Resource/CTexture.h @@ -10,6 +10,7 @@ class CTexture : public CResource { + DECLARE_RESOURCE_TYPE(eTexture) friend class CTextureDecoder; friend class CTextureEncoder; @@ -31,7 +32,6 @@ public: CTexture(const CTexture& Source); CTexture(u32 Width, u32 Height); ~CTexture(); - EResType Type(); bool BufferGL(); void Bind(u32 GLTextureUnit); diff --git a/Resource/CWorld.cpp b/Resource/CWorld.cpp index 74723c52..f8b26131 100644 --- a/Resource/CWorld.cpp +++ b/Resource/CWorld.cpp @@ -16,11 +16,6 @@ CWorld::~CWorld() { } -EResType CWorld::Type() -{ - return eWorld; -} - void CWorld::SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex) { // The AreaIndex parameter is a placeholder until an improved world loader is implemented. diff --git a/Resource/CWorld.h b/Resource/CWorld.h index 2ad78e84..864ceb1b 100644 --- a/Resource/CWorld.h +++ b/Resource/CWorld.h @@ -10,6 +10,7 @@ class CWorld : public CResource { + DECLARE_RESOURCE_TYPE(eWorld) friend class CWorldLoader; // Instances of CResource pointers are placeholders for unimplemented resource types (eg CMapWorld) @@ -82,7 +83,6 @@ class CWorld : public CResource public: CWorld(); ~CWorld(); - EResType Type(); void SetAreaLayerInfo(CGameArea *pArea, u32 AreaIndex); diff --git a/Resource/model/CBasicModel.cpp b/Resource/model/CBasicModel.cpp index 3070f718..6db326ac 100644 --- a/Resource/model/CBasicModel.cpp +++ b/Resource/model/CBasicModel.cpp @@ -16,11 +16,6 @@ CBasicModel::~CBasicModel() delete mSurfaces[iSurf]; } -EResType CBasicModel::Type() -{ - return eModel; -} - u32 CBasicModel::GetVertexCount() { return mVertexCount; diff --git a/Resource/model/CBasicModel.h b/Resource/model/CBasicModel.h index c8de8141..1ee60170 100644 --- a/Resource/model/CBasicModel.h +++ b/Resource/model/CBasicModel.h @@ -8,6 +8,7 @@ class CBasicModel : public CResource { + DECLARE_RESOURCE_TYPE(eModel) protected: CAABox mAABox; u32 mVertexCount; @@ -22,7 +23,6 @@ protected: public: CBasicModel(); ~CBasicModel(); - EResType Type(); u32 GetVertexCount(); u32 GetTriangleCount();