Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
activeobjectmgr.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
4
5#pragma once
6
7#include <memory>
8#include "util/container.h"
9#include "irrlichttypes.h"
10#include "util/basic_macros.h"
11
14
15template <typename T>
17{
18 friend class ::TestServerActiveObjectMgr;
19
20public:
21 ActiveObjectMgr() = default;
23
25 {
27 // Note: Do not call clear() here. The derived class is already half
28 // destructed.
29 }
30
31 virtual void step(float dtime, const std::function<void(T *)> &f) = 0;
32 virtual bool registerObject(std::unique_ptr<T> obj) = 0;
33 virtual void removeObject(u16 id) = 0;
34
35 void clear()
36 {
37 // on_destruct could add new objects so this has to be a loop
38 do {
39 for (auto &it : m_active_objects.iter()) {
40 if (!it.second)
41 continue;
42 m_active_objects.remove(it.first);
43 }
44 } while (!m_active_objects.empty());
45 }
46
47 T *getActiveObject(u16 id)
48 {
49 return m_active_objects.get(id).get();
50 }
51
52protected:
53 u16 getFreeId() const
54 {
55 // try to reuse id's as late as possible
56 static thread_local u16 last_used_id = 0;
57 u16 startid = last_used_id;
58 while (!isFreeId(++last_used_id)) {
59 if (last_used_id == startid)
60 return 0;
61 }
62
63 return last_used_id;
64 }
65
66 bool isFreeId(u16 id) const
67 {
68 return id != 0 && !m_active_objects.get(id);
69 }
70
71 // Note that this is ordered to fix #10985
73};
Definition activeobjectmgr.h:17
DISABLE_CLASS_COPY(ActiveObjectMgr)
virtual bool registerObject(std::unique_ptr< T > obj)=0
void clear()
Definition activeobjectmgr.h:35
virtual void step(float dtime, const std::function< void(T *)> &f)=0
ActiveObjectMgr()=default
virtual ~ActiveObjectMgr()
Definition activeobjectmgr.h:24
ModifySafeMap< u16, std::unique_ptr< T > > m_active_objects
Definition activeobjectmgr.h:72
T * getActiveObject(u16 id)
Definition activeobjectmgr.h:47
bool isFreeId(u16 id) const
Definition activeobjectmgr.h:66
u16 getFreeId() const
Definition activeobjectmgr.h:53
virtual void removeObject(u16 id)=0
Definition container.h:324
const V & get(const K &key) const
Definition container.h:350
auto iter()
Definition container.h:457
bool empty() const
Definition container.h:445
bool remove(const K &key)
Definition container.h:424
Definition test_clientactiveobjectmgr.cpp:42
Definition test_serveractiveobjectmgr.cpp:16
#define SANITY_CHECK(expr)
Definition debug.h:50