new GameObject[] instead of uninitialized byte buffer
This commit is contained in:
@@ -7,7 +7,7 @@ GameObjectPool::GameObjectPool(u32 size)
|
||||
useful_assert(size % 64 == 0, "size of GameObjectPool must be a multiple of 64");
|
||||
this->size = size;
|
||||
first_unused_index = 0;
|
||||
buffer = new char[size*sizeof(GameObject)];
|
||||
buffer = new GameObject[size];
|
||||
used_indices = new u64[size/64];
|
||||
// std::memset(buffer, 0, size*sizeof(GameObject));
|
||||
std::memset(used_indices, 0, size/8);
|
||||
@@ -15,13 +15,8 @@ GameObjectPool::GameObjectPool(u32 size)
|
||||
|
||||
GameObjectPool::~GameObjectPool()
|
||||
{
|
||||
// int i = 0;
|
||||
for(auto&& p : *this){
|
||||
// std::cout<<"~GameObjectPool i="<<i++<<std::endl;
|
||||
p.second.~GameObject();
|
||||
}
|
||||
delete (char*)buffer;
|
||||
delete used_indices;
|
||||
delete[] buffer;
|
||||
delete[] used_indices;
|
||||
}
|
||||
|
||||
bool GameObjectPool::isIndexUsed(u32 index)
|
||||
@@ -81,7 +76,7 @@ GameObject& GameObjectPool::get(u32 index)
|
||||
throw UsefulException(format("index %i is out of size %i", index, size));
|
||||
if(!isIndexUsed(index))
|
||||
throw UsefulException(format("there is no object at index %i", index));
|
||||
return ((GameObject*)buffer)[index];
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
||||
@@ -89,7 +84,9 @@ std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
||||
u32 i = first_unused_index;
|
||||
if(i == u32(-1))
|
||||
throw UsefulException("can't put new GameObject to GameObjectPool because it's full");
|
||||
GameObject& r = ( ((GameObject*)buffer)[i] = std::move(new_obj) );
|
||||
|
||||
buffer[i] = std::move(new_obj);
|
||||
GameObject& r = buffer[i];
|
||||
used_indices[i/64] |= u64(1)<<(i%64); // mark index bit as used
|
||||
first_unused_index = getNearestUnusedIndex(i+1);
|
||||
return std::pair<u32, GameObject&>(i, r);
|
||||
@@ -101,27 +98,29 @@ void GameObjectPool::erase(u32 index)
|
||||
throw UsefulException(format("index %i is out of size %i", index, size));
|
||||
if(!isIndexUsed(index))
|
||||
throw UsefulException(format("there is no object at index %i", index));
|
||||
((GameObject*)buffer)[index].~GameObject();
|
||||
|
||||
buffer[index] = GameObject();
|
||||
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
||||
if(index < first_unused_index)
|
||||
first_unused_index = index;
|
||||
}
|
||||
|
||||
GameObjectPool::iterator::iterator(GameObjectPool* p, u32 index)
|
||||
: p(p), index(index)
|
||||
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
||||
: pool(pool), index(index)
|
||||
{
|
||||
}
|
||||
|
||||
std::pair<u32, GameObject&> GameObjectPool::iterator::operator*()
|
||||
{
|
||||
if(index >= p->size)
|
||||
if(index >= pool->size)
|
||||
throw UsefulException("can't get value of end() iterator");
|
||||
GameObject& r = ((GameObject*)p->buffer)[index];
|
||||
|
||||
GameObject& r = pool->buffer[index];
|
||||
return std::pair<u32, GameObject&>(index, r);
|
||||
}
|
||||
|
||||
GameObjectPool::iterator& GameObjectPool::iterator::operator++()
|
||||
{
|
||||
index = p->getNearestUsedIndex(index+1);
|
||||
index = pool->getNearestUsedIndex(index+1);
|
||||
return *this;
|
||||
}
|
||||
Reference in New Issue
Block a user