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

View File

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