Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
activeobjectmgr.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <memory>
23#include "debug.h"
24#include "util/container.h"
25#include "irrlichttypes.h"
26#include "util/basic_macros.h"
27
30
31template <typename T>
33{
34 friend class ::TestClientActiveObjectMgr;
35 friend class ::TestServerActiveObjectMgr;
36
37public:
38 ActiveObjectMgr() = default;
40
42 {
44 // Note: Do not call clear() here. The derived class is already half
45 // destructed.
46 }
47
48 virtual void step(float dtime, const std::function<void(T *)> &f) = 0;
49 virtual bool registerObject(std::unique_ptr<T> obj) = 0;
50 virtual void removeObject(u16 id) = 0;
51
52 void clear()
53 {
54 // on_destruct could add new objects so this has to be a loop
55 do {
56 for (auto &it : m_active_objects.iter()) {
57 if (!it.second)
58 continue;
59 m_active_objects.remove(it.first);
60 }
61 } while (!m_active_objects.empty());
62 }
63
64 T *getActiveObject(u16 id)
65 {
66 return m_active_objects.get(id).get();
67 }
68
69protected:
70 u16 getFreeId() const
71 {
72 // try to reuse id's as late as possible
73 static thread_local u16 last_used_id = 0;
74 u16 startid = last_used_id;
75 while (!isFreeId(++last_used_id)) {
76 if (last_used_id == startid)
77 return 0;
78 }
79
80 return last_used_id;
81 }
82
83 bool isFreeId(u16 id) const
84 {
85 return id != 0 && !m_active_objects.get(id);
86 }
87
88 // Note that this is ordered to fix #10985
90};
Definition: activeobjectmgr.h:33
DISABLE_CLASS_COPY(ActiveObjectMgr)
virtual bool registerObject(std::unique_ptr< T > obj)=0
void clear()
Definition: activeobjectmgr.h:52
virtual void step(float dtime, const std::function< void(T *)> &f)=0
ActiveObjectMgr()=default
virtual ~ActiveObjectMgr()
Definition: activeobjectmgr.h:41
ModifySafeMap< u16, std::unique_ptr< T > > m_active_objects
Definition: activeobjectmgr.h:89
T * getActiveObject(u16 id)
Definition: activeobjectmgr.h:64
bool isFreeId(u16 id) const
Definition: activeobjectmgr.h:83
u16 getFreeId() const
Definition: activeobjectmgr.h:70
virtual void removeObject(u16 id)=0
Definition: container.h:339
const V & get(const K &key) const
Definition: container.h:365
auto iter()
Definition: container.h:472
bool empty() const
Definition: container.h:460
bool remove(const K &key)
Definition: container.h:439
Definition: test_clientactiveobjectmgr.cpp:54
Definition: test_serveractiveobjectmgr.cpp:31
#define SANITY_CHECK(expr)
Definition: debug.h:66