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