master
Piotr Dziwinski 2015-09-06 13:23:20 +02:00
parent 44a1f85891
commit 86678d4f7a
2 changed files with 45 additions and 26 deletions

View File

@ -45,9 +45,14 @@ CObjectManager::CObjectManager(Gfx::CEngine* engine,
Gfx::COldModelManager* oldModelManager,
Gfx::CModelManager* modelManager,
Gfx::CParticle* particle)
: m_objectFactory(MakeUnique<CObjectFactory>(engine, terrain, oldModelManager, modelManager, particle))
, m_nextId(0)
, m_shouldCleanRemovedObjects(false)
: m_objectFactory(MakeUnique<CObjectFactory>(engine,
terrain,
oldModelManager,
modelManager,
particle)),
m_nextId(0),
m_activeObjectIterators(0),
m_shouldCleanRemovedObjects(false)
{
}
@ -75,8 +80,14 @@ bool CObjectManager::DeleteObject(CObject* instance)
return false;
}
void CObjectManager::CleanRemovedObjects()
void CObjectManager::CleanRemovedObjectsIfNeeded()
{
if (m_activeObjectIterators != 0)
return;
if (! m_shouldCleanRemovedObjects)
return;
auto it = m_objects.begin();
if (it != m_objects.end())
{

View File

@ -66,13 +66,21 @@ using CObjectMapCIt = std::map<int, std::unique_ptr<CObject>>::const_iterator;
class CObjectIteratorProxy
{
private:
friend class CObjectContainerProxy;
CObjectIteratorProxy(CObjectMapCIt currentIt, CObjectMapCIt endIt)
: m_currentIt(currentIt)
, m_endIt(endIt)
{}
public:
inline CObject* operator*()
CObject* operator*()
{
return m_currentIt->second.get();
}
inline void operator++()
void operator++()
{
do
{
@ -81,19 +89,11 @@ public:
while (m_currentIt != m_endIt && m_currentIt->second == nullptr);
}
inline bool operator!=(const CObjectIteratorProxy& other)
bool operator!=(const CObjectIteratorProxy& other)
{
return m_currentIt != other.m_currentIt;
}
private:
friend class CObjectContainerProxy;
CObjectIteratorProxy(CObjectMapCIt currentIt, CObjectMapCIt endIt)
: m_currentIt(currentIt)
, m_endIt(endIt)
{}
private:
CObjectMapCIt m_currentIt;
CObjectMapCIt m_endIt;
@ -104,22 +104,31 @@ class CObjectContainerProxy
private:
friend class CObjectManager;
inline CObjectContainerProxy(const CObjectMap& map)
: m_map(map)
{}
CObjectContainerProxy(const CObjectMap& map, int& activeIteratorsCounter)
: m_map(map),
m_activeIteratorsCounter(activeIteratorsCounter)
{
++m_activeIteratorsCounter;
}
public:
inline CObjectIteratorProxy begin() const
~CObjectContainerProxy()
{
--m_activeIteratorsCounter;
}
CObjectIteratorProxy begin() const
{
return CObjectIteratorProxy(m_map.begin(), m_map.end());
}
inline CObjectIteratorProxy end() const
CObjectIteratorProxy end() const
{
return CObjectIteratorProxy(m_map.end(), m_map.end());
}
private:
const CObjectMap& m_map;
int& m_activeIteratorsCounter;
};
/**
@ -167,12 +176,10 @@ public:
int CountObjectsImplementing(ObjectInterfaceType interface);
//! Returns all objects
inline CObjectContainerProxy GetAllObjects()
CObjectContainerProxy GetAllObjects()
{
if (m_shouldCleanRemovedObjects)
CleanRemovedObjects();
return CObjectContainerProxy(m_objects);
CleanRemovedObjectsIfNeeded();
return CObjectContainerProxy(m_objects, m_activeObjectIterators);
}
//! Finds an object, like radar() in CBot
@ -241,11 +248,12 @@ public:
//@}
private:
void CleanRemovedObjects();
void CleanRemovedObjectsIfNeeded();
private:
CObjectMap m_objects;
std::unique_ptr<CObjectFactory> m_objectFactory;
int m_nextId;
int m_activeObjectIterators;
bool m_shouldCleanRemovedObjects;
};