1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

ActiveObjectMgr fixes (#13560)

This commit is contained in:
DS 2023-10-09 17:13:04 +02:00 committed by GitHub
parent 929a13a9a0
commit 11ec75c2ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 204 additions and 153 deletions

View file

@ -20,7 +20,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <map>
#include <memory>
#include <vector>
#include "debug.h"
#include "irrlichttypes.h"
#include "util/basic_macros.h"
class TestClientActiveObjectMgr;
class TestServerActiveObjectMgr;
@ -32,14 +36,40 @@ class ActiveObjectMgr
friend class ::TestServerActiveObjectMgr;
public:
ActiveObjectMgr() = default;
DISABLE_CLASS_COPY(ActiveObjectMgr);
virtual ~ActiveObjectMgr()
{
SANITY_CHECK(m_active_objects.empty());
// Note: Do not call clear() here. The derived class is already half
// destructed.
}
virtual void step(float dtime, const std::function<void(T *)> &f) = 0;
virtual bool registerObject(T *obj) = 0;
virtual bool registerObject(std::unique_ptr<T> obj) = 0;
virtual void removeObject(u16 id) = 0;
void clear()
{
while (!m_active_objects.empty())
removeObject(m_active_objects.begin()->first);
}
T *getActiveObject(u16 id)
{
auto n = m_active_objects.find(id);
return (n != m_active_objects.end() ? n->second : nullptr);
auto it = m_active_objects.find(id);
return it != m_active_objects.end() ? it->second.get() : nullptr;
}
std::vector<u16> getAllIds() const
{
std::vector<u16> ids;
ids.reserve(m_active_objects.size());
for (auto &it : m_active_objects) {
ids.push_back(it.first);
}
return ids;
}
protected:
@ -61,5 +91,8 @@ protected:
return id != 0 && m_active_objects.find(id) == m_active_objects.end();
}
std::map<u16, T *> m_active_objects; // ordered to fix #10985
// ordered to fix #10985
// Note: ActiveObjects can access the ActiveObjectMgr. Only erase objects using
// removeObject()!
std::map<u16, std::unique_ptr<T>> m_active_objects;
};