Making block copy constructors and assignment operators private to prevent accidental copying.

This commit is contained in:
David Williams 2013-07-17 16:23:46 +02:00
parent dea7e6a4e9
commit 5b99854c02
3 changed files with 39 additions and 20 deletions

View File

@ -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 <typename VoxelType>
@ -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);

View File

@ -331,11 +331,11 @@ namespace PolyVox
CompressedBlock<VoxelType>* getCompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const;
UncompressedBlock<VoxelType>* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const;
void eraseBlock(typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itBlock) const;
void eraseBlock(typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, BlockPositionCompare>::iterator itBlock) const;
// The block data
mutable std::map<Vector3DInt32, UncompressedBlock<VoxelType>*, BlockPositionCompare> m_pUncompressedBlockCache;
mutable std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare> m_pBlocks;
mutable std::map<Vector3DInt32, CompressedBlock<VoxelType>*, BlockPositionCompare> m_pBlocks;
mutable uint32_t m_uTimestamper;
mutable Vector3DInt32 m_v3dLastAccessedBlockPos;

View File

@ -405,7 +405,7 @@ namespace PolyVox
for(int32_t z = v3dStart.getZ(); z <= v3dEnd.getZ(); z++)
{
Vector3DInt32 pos(x,y,z);
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos);
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos);
if(itBlock != m_pBlocks.end())
{
@ -436,7 +436,7 @@ namespace PolyVox
template <typename VoxelType>
void LargeVolume<VoxelType>::flushAll()
{
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator i;
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos);
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(pos);
if(itBlock == m_pBlocks.end())
{
// not loaded, not unloading
@ -548,7 +548,7 @@ namespace PolyVox
}
template <typename VoxelType>
void LargeVolume<VoxelType>::eraseBlock(typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itBlock) const
void LargeVolume<VoxelType>::eraseBlock(typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itBlock = m_pBlocks.find(v3dBlockPos);
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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<VoxelType> newBlock;
CompressedBlock<VoxelType>* newBlock = new CompressedBlock<VoxelType>;
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<VoxelType>& block = itBlock->second;
block.m_uBlockLastAccessed = ++m_uTimestamper;
CompressedBlock<VoxelType>* block = itBlock->second;
block->m_uBlockLastAccessed = ++m_uTimestamper;
//m_v3dLastAccessedBlockPos = v3dBlockPos;
return &block;
return block;
}
template <typename VoxelType>
@ -681,11 +681,11 @@ namespace PolyVox
uint32_t uSizeInBytes = sizeof(LargeVolume);
//Memory used by the blocks
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator i;
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator i;
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator i;
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin();
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, BlockPositionCompare>::iterator i;
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>*, 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);
}