From 5b99854c02a2aa46a74895ef15ecea839158c24d Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 17 Jul 2013 16:23:46 +0200 Subject: [PATCH] Making block copy constructors and assignment operators private to prevent accidental copying. --- .../include/PolyVoxCore/Impl/Block.h | 19 ++++++++++ .../include/PolyVoxCore/LargeVolume.h | 4 +-- .../include/PolyVoxCore/LargeVolume.inl | 36 +++++++++---------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h index e78ecb48..3d795e26 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h @@ -53,6 +53,13 @@ namespace PolyVox // This is so we can tell whether a uncompressed block has to be recompressed and whether // a compressed block has to be paged back to disk, or whether they can just be discarded. bool m_bDataModified; + + private: + /// Private copy constructor to prevent accisdental copying + Block(const Block& /*rhs*/) {}; + + /// Private assignment operator to prevent accisdental copying + Block& operator=(const Block& /*rhs*/) {}; }; template @@ -70,6 +77,12 @@ namespace PolyVox void setData(const uint8_t* const pData, uint32_t uDataSizeInBytes); private: + /// Private copy constructor to prevent accisdental copying + CompressedBlock(const CompressedBlock& /*rhs*/) {}; + + /// Private assignment operator to prevent accisdental copying + CompressedBlock& operator=(const CompressedBlock& /*rhs*/) {}; + // Made this private to avoid any confusion with getDataSizeInBytes(). // Users shouldn't really need this for CompressedBlock anyway. uint32_t calculateSizeInBytes(void); @@ -94,6 +107,12 @@ namespace PolyVox void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue); private: + /// Private copy constructor to prevent accisdental copying + UncompressedBlock(const UncompressedBlock& /*rhs*/) {}; + + /// Private assignment operator to prevent accisdental copying + UncompressedBlock& operator=(const UncompressedBlock& /*rhs*/) {}; + // Made this private for consistancy with CompressedBlock. // Users shouldn't really need this for UncompressedBlock anyway. uint32_t calculateSizeInBytes(void); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index 65e9dec4..01781cc6 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -331,11 +331,11 @@ namespace PolyVox CompressedBlock* getCompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; UncompressedBlock* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; - void eraseBlock(typename std::map, BlockPositionCompare>::iterator itBlock) const; + void eraseBlock(typename std::map*, BlockPositionCompare>::iterator itBlock) const; // The block data mutable std::map*, BlockPositionCompare> m_pUncompressedBlockCache; - mutable std::map, BlockPositionCompare> m_pBlocks; + mutable std::map*, BlockPositionCompare> m_pBlocks; mutable uint32_t m_uTimestamper; mutable Vector3DInt32 m_v3dLastAccessedBlockPos; diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index 731a6c01..42692706 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -405,7 +405,7 @@ namespace PolyVox for(int32_t z = v3dStart.getZ(); z <= v3dEnd.getZ(); z++) { Vector3DInt32 pos(x,y,z); - typename std::map, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos); + typename std::map*, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos); if(itBlock != m_pBlocks.end()) { @@ -436,7 +436,7 @@ namespace PolyVox template void LargeVolume::flushAll() { - typename std::map, BlockPositionCompare>::iterator i; + typename std::map*, BlockPositionCompare>::iterator i; //Replaced the for loop here as the call to //eraseBlock was invalidating the iterator. while(m_pBlocks.size() > 0) @@ -470,7 +470,7 @@ namespace PolyVox for(int32_t z = v3dStart.getZ(); z <= v3dEnd.getZ(); z++) { Vector3DInt32 pos(x,y,z); - typename std::map, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos); + typename std::map*, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos); if(itBlock == m_pBlocks.end()) { // not loaded, not unloading @@ -548,7 +548,7 @@ namespace PolyVox } template - void LargeVolume::eraseBlock(typename std::map, BlockPositionCompare>::iterator itBlock) const + void LargeVolume::eraseBlock(typename std::map*, BlockPositionCompare>::iterator itBlock) const { POLYVOX_ASSERT(false, "This function has not been implemented properly"); @@ -591,30 +591,30 @@ namespace PolyVox { Vector3DInt32 v3dBlockPos(uBlockX, uBlockY, uBlockZ); - typename std::map, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(v3dBlockPos); + typename std::map*, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(v3dBlockPos); // check whether the block is already loaded if(itBlock == m_pBlocks.end()) { //The block is not in the map, so we will have to create a new block and add it. - CompressedBlock newBlock; + CompressedBlock* newBlock = new CompressedBlock; itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first; // Now use the pager to fill the block with it's initial data. Vector3DInt32 v3dLower(v3dBlockPos.getX() << m_uBlockSideLengthPower, v3dBlockPos.getY() << m_uBlockSideLengthPower, v3dBlockPos.getZ() << m_uBlockSideLengthPower); Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uBlockSideLength-1, m_uBlockSideLength-1, m_uBlockSideLength-1); Region reg(v3dLower, v3dUpper); - m_pPager->pageIn(reg, &(itBlock->second)); + m_pPager->pageIn(reg, itBlock->second); // Paging in this new block may mean we are now using too much memory. If necessary, flush some old blocks. flushOldestExcessiveBlocks(); } //Get the block and mark that we accessed it - CompressedBlock& block = itBlock->second; - block.m_uBlockLastAccessed = ++m_uTimestamper; + CompressedBlock* block = itBlock->second; + block->m_uBlockLastAccessed = ++m_uTimestamper; //m_v3dLastAccessedBlockPos = v3dBlockPos; - return █ + return block; } template @@ -681,11 +681,11 @@ namespace PolyVox uint32_t uSizeInBytes = sizeof(LargeVolume); //Memory used by the blocks - typename std::map, BlockPositionCompare>::iterator i; + typename std::map*, BlockPositionCompare>::iterator i; for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++) { //Inaccurate - account for rest of loaded block. - uSizeInBytes += i->second.calculateSizeInBytes(); + uSizeInBytes += i->second->calculateSizeInBytes(); } //Memory used by the block cache. @@ -700,11 +700,11 @@ namespace PolyVox { uint32_t uMemoryUsage = 0; - typename std::map, BlockPositionCompare>::iterator i; + typename std::map*, BlockPositionCompare>::iterator i; for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++) { //Inaccurate - account for rest of loaded block. - uMemoryUsage += i->second.calculateSizeInBytes(); + uMemoryUsage += i->second->calculateSizeInBytes(); } return uMemoryUsage; @@ -720,11 +720,11 @@ namespace PolyVox while(calculateBlockMemoryUsage() > m_uCompressedBlockMemoryLimitInBytes) //FIXME - This calculation of size is slow and should be outside the loop. { // find the least recently used block - typename std::map, BlockPositionCompare>::iterator i; - typename std::map, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin(); + typename std::map*, BlockPositionCompare>::iterator i; + typename std::map*, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin(); for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++) { - if(i->second.m_uBlockLastAccessed < itUnloadBlock->second.m_uBlockLastAccessed) + if(i->second->m_uBlockLastAccessed < itUnloadBlock->second->m_uBlockLastAccessed) { itUnloadBlock = i; } @@ -732,7 +732,7 @@ namespace PolyVox //POLYVOX_ASSERT(itUnloadBlock->second.hasUncompressedData() == false, "This function should never flush blocks with uncompressed data."); - uMemoryToReclaim -= itUnloadBlock->second.calculateSizeInBytes(); + uMemoryToReclaim -= itUnloadBlock->second->calculateSizeInBytes(); eraseBlock(itUnloadBlock); }