ControlledLifetimeResource.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2019-2024 The RmlUi Team, and contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef RMLUI_CORE_CONTROLLEDLIFETIMERESOURCE_H
  28. #define RMLUI_CORE_CONTROLLEDLIFETIMERESOURCE_H
  29. #include "../../Include/RmlUi/Core/Debug.h"
  30. #include "../../Include/RmlUi/Core/Traits.h"
  31. namespace Rml {
  32. template <typename T>
  33. class ControlledLifetimeResource : NonCopyMoveable {
  34. public:
  35. ControlledLifetimeResource() = default;
  36. ~ControlledLifetimeResource() noexcept
  37. {
  38. #if defined(RMLUI_PLATFORM_WIN32) && !defined(RMLUI_STATIC_LIB)
  39. RMLUI_ASSERTMSG(!pointer || intentionally_leaked, "Resource was not properly shut down.");
  40. #endif
  41. }
  42. explicit operator bool() const noexcept { return pointer != nullptr; }
  43. void Initialize()
  44. {
  45. RMLUI_ASSERTMSG(!pointer, "Resource already initialized.");
  46. pointer = new T();
  47. }
  48. void InitializeIfEmpty()
  49. {
  50. if (!pointer)
  51. Initialize();
  52. else
  53. SetIntentionallyLeaked(false);
  54. }
  55. void Leak() { SetIntentionallyLeaked(true); }
  56. void Shutdown()
  57. {
  58. RMLUI_ASSERTMSG(pointer, "Shutting down resource that was not initialized, or has been shut down already.");
  59. RMLUI_ASSERTMSG(!intentionally_leaked, "Shutting down resource that was marked as leaked.");
  60. delete pointer;
  61. pointer = nullptr;
  62. }
  63. T* operator->()
  64. {
  65. RMLUI_ASSERTMSG(pointer, "Resource used before it was initialized, or after it was shut down.");
  66. return pointer;
  67. }
  68. private:
  69. #ifdef RMLUI_DEBUG
  70. void SetIntentionallyLeaked(bool leaked) { intentionally_leaked = leaked; }
  71. bool intentionally_leaked = false;
  72. #else
  73. void SetIntentionallyLeaked(bool /*leaked*/) {}
  74. #endif
  75. T* pointer = nullptr;
  76. };
  77. } // namespace Rml
  78. #endif