activeobjectmgr.h 2.3 KB

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