From 4a0355f065a3be357cfb0e650224903f53e70913 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Wed, 6 Sep 2017 18:40:04 +0100 Subject: cBlockArea: use unique_ptr --- src/BlockArea.h | 65 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 11 deletions(-) (limited to 'src/BlockArea.h') diff --git a/src/BlockArea.h b/src/BlockArea.h index 2e35b9436..94081f93f 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -63,7 +63,6 @@ public: } ; cBlockArea(void); - ~cBlockArea(); /** Returns true if the datatype combination is valid. Invalid combinations include BlockEntities without BlockTypes. */ @@ -366,10 +365,10 @@ public: // Clients can use these for faster access to all blocktypes. Be careful though! /** Returns the internal pointer to the block types */ - BLOCKTYPE * GetBlockTypes (void) const { return m_BlockTypes; } - NIBBLETYPE * GetBlockMetas (void) const { return m_BlockMetas; } // NOTE: one byte per block! - NIBBLETYPE * GetBlockLight (void) const { return m_BlockLight; } // NOTE: one byte per block! - NIBBLETYPE * GetBlockSkyLight(void) const { return m_BlockSkyLight; } // NOTE: one byte per block! + BLOCKTYPE * GetBlockTypes (void) const { return m_BlockTypes.get(); } + NIBBLETYPE * GetBlockMetas (void) const { return m_BlockMetas.get(); } // NOTE: one byte per block! + NIBBLETYPE * GetBlockLight (void) const { return m_BlockLight.get(); } // NOTE: one byte per block! + NIBBLETYPE * GetBlockSkyLight(void) const { return m_BlockSkyLight.get(); } // NOTE: one byte per block! size_t GetBlockCount(void) const { return static_cast(m_Size.x * m_Size.y * m_Size.z); } int MakeIndex(int a_RelX, int a_RelY, int a_RelZ) const; @@ -419,7 +418,43 @@ protected: virtual void BlockEntity(cBlockEntity * a_BlockEntity) override; } ; - typedef NIBBLETYPE * NIBBLEARRAY; + template + class cDynArray: + public std::unique_ptr + { + using Super = std::unique_ptr; + public: + // using Super::Super; + cDynArray() = default; + cDynArray(cDynArray && a_Other) : Super(std::move(a_Other)) {} + cDynArray & operator = (cDynArray && a_Other) + { + Super::operator = (std::move(a_Other)); + return *this; + } + + cDynArray(std::nullptr_t) {} + + cDynArray(T * a_Ptr): + Super(a_Ptr) + { + } + + // Allow indexing with signed types + T & operator [] (int a_Idx) const + { + ASSERT(a_Idx >= 0); + return (Super::get())[a_Idx]; + } + + T & operator [] (size_t a_Idx) const + { + return (Super::get())[a_Idx]; + } + }; + + using NIBBLEARRAY = cDynArray; + using BLOCKARRAY = cDynArray; Vector3i m_Origin; @@ -429,15 +464,23 @@ protected: cBlockArea doesn't use this value in any way. */ Vector3i m_WEOffset; - BLOCKTYPE * m_BlockTypes; - NIBBLETYPE * m_BlockMetas; // Each meta is stored as a separate byte for faster access - NIBBLETYPE * m_BlockLight; // Each light value is stored as a separate byte for faster access - NIBBLETYPE * m_BlockSkyLight; // Each light value is stored as a separate byte for faster access + BLOCKARRAY m_BlockTypes; + NIBBLEARRAY m_BlockMetas; // Each meta is stored as a separate byte for faster access + NIBBLEARRAY m_BlockLight; // Each light value is stored as a separate byte for faster access + NIBBLEARRAY m_BlockSkyLight; // Each light value is stored as a separate byte for faster access + + /** Deleter to clear the block entities before deleting the container. */ + struct sBlockEntitiesDeleter + { + void operator () (cBlockEntities * a_BlockEntities); + }; + + using cBlockEntitiesPtr = std::unique_ptr; /** The block entities contained within the area. Only valid if the area was created / read with the baBlockEntities flag. The block entities are owned by this object. */ - std::unique_ptr m_BlockEntities; + cBlockEntitiesPtr m_BlockEntities; /** Clears the data stored and prepares a fresh new block area with the specified dimensions */ bool SetSize(int a_SizeX, int a_SizeY, int a_SizeZ, int a_DataTypes); -- cgit v1.2.3