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 "debug.h"
9#include "util/container.h"
10#include "irrlichttypes.h"
11#include "util/basic_macros.h"
12
15
16template <typename T>
18{
19 friend class ::TestServerActiveObjectMgr;
20
21public:
22 ActiveObjectMgr() = default;
24
26 {
28 // Note: Do not call clear() here. The derived class is already half
29 // destructed.
30 }
31
32 virtual void step(float dtime, const std::function<void(T *)> &f) = 0;
33 virtual bool registerObject(std::unique_ptr<T> obj) = 0;
34 virtual void removeObject(u16 id) = 0;
35
36 void clear()
37 {
38 // on_destruct could add new objects so this has to be a loop
39 do {
40 for (auto &it : m_active_objects.iter()) {
41 if (!it.second)
42 continue;
43 m_active_objects.remove(it.first);
44 }
45 } while (!m_active_objects.empty());
46 }
47
48 T *getActiveObject(u16 id)
49 {
50 return m_active_objects.get(id).get();
51 }
52
53protected:
54 u16 getFreeId() const
55 {
56 // try to reuse id's as late as possible
57 static thread_local u16 last_used_id = 0;
58 u16 startid = last_used_id;
59 while (!isFreeId(++last_used_id)) {
60 if (last_used_id == startid)
61 return 0;
62 }
63
64 return last_used_id;
65 }
66
67 bool isFreeId(u16 id) const
68 {
69 return id != 0 && !m_active_objects.get(id);
70 }
71
72 // Note that this is ordered to fix #10985
74};
Definition activeobjectmgr.h:18
DISABLE_CLASS_COPY(ActiveObjectMgr)
virtual bool registerObject(std::unique_ptr< T > obj)=0
void clear()
Definition activeobjectmgr.h:36
virtual void step(float dtime, const std::function< void(T *)> &f)=0
ActiveObjectMgr()=default
virtual ~ActiveObjectMgr()
Definition activeobjectmgr.h:25
ModifySafeMap< u16, std::unique_ptr< T > > m_active_objects
Definition activeobjectmgr.h:73
T * getActiveObject(u16 id)
Definition activeobjectmgr.h:48
bool isFreeId(u16 id) const
Definition activeobjectmgr.h:67
u16 getFreeId() const
Definition activeobjectmgr.h:54
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:51