UiObject.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Ui/Common.h>
  7. namespace anki {
  8. /// @addtogroup ui
  9. /// @{
  10. /// The base of all UI objects.
  11. class UiObject
  12. {
  13. public:
  14. UiObject(UiManager* manager)
  15. : m_manager(manager)
  16. {
  17. ANKI_ASSERT(manager);
  18. }
  19. virtual ~UiObject() = default;
  20. UiAllocator getAllocator() const;
  21. void retain() const
  22. {
  23. m_refcount.fetchAdd(1);
  24. }
  25. I32 release() const
  26. {
  27. return m_refcount.fetchSub(1);
  28. }
  29. /// Set the global IMGUI allocator.
  30. void setImAllocator(BaseMemoryPool* pool = nullptr)
  31. {
  32. pool = (pool) ? pool : &getAllocator().getMemoryPool();
  33. auto allocCallback = [](size_t size, void* userData) -> void* {
  34. BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
  35. return pool->allocate(size, 16);
  36. };
  37. auto freeCallback = [](void* ptr, void* userData) -> void {
  38. if(ptr)
  39. {
  40. BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
  41. pool->free(ptr);
  42. }
  43. };
  44. ImGui::SetAllocatorFunctions(allocCallback, freeCallback, pool);
  45. }
  46. /// Unset the global IMGUI allocator.
  47. static void unsetImAllocator()
  48. {
  49. ImGui::SetAllocatorFunctions(nullptr, nullptr, nullptr);
  50. }
  51. protected:
  52. UiManager* m_manager;
  53. mutable Atomic<I32> m_refcount = {0};
  54. };
  55. /// @}
  56. } // end namespace anki