ResourceWrapper.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #pragma once
  2. #include <stdexcept>
  3. #include <type_traits>
  4. #include <utility>
  5. namespace ARL {
  6. template<typename __ResourceTraits>
  7. class ResourceWrapper {
  8. public:
  9. using HandleType = typename __ResourceTraits::HandleType;
  10. static_assert(
  11. std::is_trivial_v<HandleType> && std::is_standard_layout_v<HandleType>,
  12. "HandleType must be a POD type."
  13. );
  14. private:
  15. HandleType m_Handle;
  16. public:
  17. ResourceWrapper() noexcept :
  18. m_Handle(__ResourceTraits::InvalidValue) {}
  19. ResourceWrapper(const HandleType& Handle) noexcept :
  20. m_Handle(Handle) {}
  21. ResourceWrapper(__ResourceTraits) noexcept :
  22. m_Handle(__ResourceTraits::InvalidValue) {}
  23. ResourceWrapper(__ResourceTraits, const HandleType& Handle) noexcept :
  24. m_Handle(Handle) {}
  25. ResourceWrapper(const ResourceWrapper& Other) = delete;
  26. ResourceWrapper(ResourceWrapper&& Other) noexcept :
  27. m_Handle(std::move(Other.m_Handle))
  28. {
  29. Other.m_Handle = __ResourceTraits::InvalidValue;
  30. }
  31. ResourceWrapper& operator=(const ResourceWrapper& Other) = delete;
  32. ResourceWrapper& operator=(ResourceWrapper&& Other) noexcept {
  33. if (this != std::addressof(Other)) {
  34. if (IsValid()) {
  35. __ResourceTraits::Release(m_Handle);
  36. }
  37. m_Handle = std::move(Other.m_Handle);
  38. Other.m_Handle = __ResourceTraits::InvalidValue;
  39. }
  40. return *this;
  41. }
  42. [[nodiscard]]
  43. operator HandleType() const noexcept { // NOLINT: Allow implicit conversion.
  44. return m_Handle;
  45. }
  46. template<typename __AsType>
  47. [[nodiscard]]
  48. __AsType As() const noexcept {
  49. return reinterpret_cast<__AsType>(m_Handle);
  50. }
  51. template<bool __Enable = std::is_pointer_v<HandleType>>
  52. [[nodiscard]]
  53. std::enable_if_t<__Enable, HandleType> operator->() const noexcept {
  54. return m_Handle;
  55. }
  56. [[nodiscard]]
  57. bool IsValid() const noexcept {
  58. return __ResourceTraits::IsValid(m_Handle);
  59. }
  60. [[nodiscard]]
  61. HandleType Get() const noexcept {
  62. return m_Handle;
  63. }
  64. [[nodiscard]]
  65. HandleType* GetAddressOf() noexcept {
  66. return &m_Handle;
  67. }
  68. template<typename __ReturnType>
  69. [[nodiscard]]
  70. __ReturnType GetAddressOfAs() noexcept {
  71. return reinterpret_cast<__ReturnType>(&m_Handle);
  72. }
  73. void TakeOver(const HandleType& Handle) {
  74. if (IsValid() == false) {
  75. m_Handle = Handle;
  76. } else {
  77. throw std::runtime_error("ResourceWrapper is already in use.");
  78. }
  79. }
  80. void Discard() noexcept {
  81. m_Handle = __ResourceTraits::InvalidValue;
  82. }
  83. [[nodiscard]]
  84. HandleType Transfer() noexcept {
  85. auto t = m_Handle;
  86. m_Handle = __ResourceTraits::InvalidValue;
  87. return t;
  88. }
  89. template<typename __ReturnType>
  90. [[nodiscard]]
  91. __ReturnType TransferAs() noexcept {
  92. static_assert(
  93. std::is_trivial_v<__ReturnType> && std::is_standard_layout_v<__ReturnType>,
  94. "__ReturnType should also be a POD type, just like HandleType."
  95. );
  96. auto t = reinterpret_cast<__ReturnType>(m_Handle);
  97. m_Handle = __ResourceTraits::InvalidValue;
  98. return t;
  99. }
  100. void Release() {
  101. if (IsValid()) {
  102. __ResourceTraits::Release(m_Handle);
  103. m_Handle = __ResourceTraits::InvalidValue;
  104. }
  105. }
  106. void ReleaseAndTakeOver(const HandleType& Handle) {
  107. if (IsValid()) {
  108. __ResourceTraits::Release(m_Handle);
  109. }
  110. m_Handle = Handle;
  111. }
  112. [[nodiscard]]
  113. HandleType* ReleaseAndGetAddressOf() {
  114. if (IsValid()) {
  115. __ResourceTraits::Release(m_Handle);
  116. m_Handle = __ResourceTraits::InvalidValue;
  117. }
  118. return GetAddressOf();
  119. }
  120. template<typename __ReturnType>
  121. __ReturnType ReleaseAndGetAddressOfAs() {
  122. if (IsValid()) {
  123. __ResourceTraits::Release(m_Handle);
  124. m_Handle = __ResourceTraits::InvalidValue;
  125. }
  126. return GetAddressOfAs<__ReturnType>();
  127. }
  128. ~ResourceWrapper() {
  129. Release();
  130. }
  131. };
  132. template<typename __ResourceTraits, typename __DeleterType>
  133. class ResourceWrapperEx {
  134. public:
  135. using HandleType = typename __ResourceTraits::HandleType;
  136. using DeleterType = __DeleterType;
  137. static_assert(
  138. std::is_trivial_v<HandleType> && std::is_standard_layout_v<HandleType>,
  139. "HandleType must be a POD type."
  140. );
  141. private:
  142. HandleType m_Handle;
  143. DeleterType m_Deleter;
  144. public:
  145. template<typename __DeleterArgType>
  146. ResourceWrapperEx(__ResourceTraits, __DeleterArgType&& Deleter) noexcept :
  147. m_Handle(__ResourceTraits::InvalidValue),
  148. m_Deleter(std::forward<__DeleterArgType>(Deleter)) {}
  149. template<typename __DeleterArgType>
  150. ResourceWrapperEx(__ResourceTraits, const HandleType& Handle, __DeleterArgType&& Deleter) noexcept :
  151. m_Handle(Handle),
  152. m_Deleter(std::forward<__DeleterArgType>(Deleter)) {}
  153. ResourceWrapperEx(const ResourceWrapperEx& Other) = delete;
  154. ResourceWrapperEx(ResourceWrapperEx&& Other) noexcept :
  155. m_Handle(std::move(Other.m_Handle)),
  156. m_Deleter(std::move(Other.m_Deleter))
  157. {
  158. Other.m_Handle = __ResourceTraits::InvalidValue;
  159. }
  160. ResourceWrapperEx& operator=(const ResourceWrapperEx& Other) = delete;
  161. ResourceWrapperEx& operator=(ResourceWrapperEx&& Other) noexcept {
  162. if (this != std::addressof(Other)) {
  163. if (IsValid()) {
  164. m_Deleter(m_Handle);
  165. }
  166. m_Handle = std::move(Other.m_Handle);
  167. m_Deleter = std::move(Other.m_Deleter);
  168. Other.m_Handle = __ResourceTraits::InvalidValue;
  169. }
  170. return *this;
  171. }
  172. [[nodiscard]]
  173. operator HandleType() const noexcept { // NOLINT: Allow implicit conversion.
  174. return m_Handle;
  175. }
  176. template<typename __AsType>
  177. [[nodiscard]]
  178. __AsType As() const noexcept {
  179. return reinterpret_cast<__AsType>(m_Handle);
  180. }
  181. template<bool __Enable = std::is_pointer_v<HandleType>>
  182. [[nodiscard]]
  183. std::enable_if_t<__Enable, HandleType> operator->() const noexcept {
  184. return m_Handle;
  185. }
  186. [[nodiscard]]
  187. bool IsValid() const noexcept {
  188. return __ResourceTraits::IsValid(m_Handle);
  189. }
  190. [[nodiscard]]
  191. HandleType Get() const noexcept {
  192. return m_Handle;
  193. }
  194. [[nodiscard]]
  195. HandleType* GetAddressOf() noexcept {
  196. return &m_Handle;
  197. }
  198. template<typename __ReturnType>
  199. [[nodiscard]]
  200. __ReturnType GetAddressOfAs() noexcept {
  201. return reinterpret_cast<__ReturnType>(&m_Handle);
  202. }
  203. void TakeOver(const HandleType& Handle) {
  204. if (IsValid() == false) {
  205. m_Handle = Handle;
  206. } else {
  207. throw std::runtime_error("ResourceWrapperEx is already in use.");
  208. }
  209. }
  210. void Discard() noexcept {
  211. m_Handle = __ResourceTraits::InvalidValue;
  212. }
  213. [[nodiscard]]
  214. HandleType Transfer() noexcept {
  215. auto t = m_Handle;
  216. m_Handle = __ResourceTraits::InvalidValue;
  217. return t;
  218. }
  219. template<typename __ReturnType>
  220. [[nodiscard]]
  221. __ReturnType TransferAs() noexcept {
  222. static_assert(std::is_trivial_v<__ReturnType> && std::is_standard_layout_v<__ReturnType>);
  223. auto t = reinterpret_cast<__ReturnType>(m_Handle);
  224. m_Handle = __ResourceTraits::InvalidValue;
  225. return t;
  226. }
  227. void Release() {
  228. if (IsValid()) {
  229. m_Deleter(m_Handle);
  230. m_Handle = __ResourceTraits::InvalidValue;
  231. }
  232. }
  233. void ReleaseAndTakeOver(const HandleType& Handle) {
  234. if (IsValid()) {
  235. __ResourceTraits::Release(m_Handle);
  236. }
  237. m_Handle = Handle;
  238. }
  239. [[nodiscard]]
  240. HandleType* ReleaseAndGetAddressOf() {
  241. if (IsValid()) {
  242. m_Deleter(m_Handle);
  243. m_Handle = __ResourceTraits::InvalidValue;
  244. }
  245. return GetAddressOf();
  246. }
  247. template<typename __ReturnType>
  248. __ReturnType ReleaseAndGetAddressOfAs() {
  249. if (IsValid()) {
  250. m_Deleter(m_Handle);
  251. m_Handle = __ResourceTraits::InvalidValue;
  252. }
  253. return GetAddressOfAs<__ReturnType>();
  254. }
  255. ~ResourceWrapperEx() {
  256. Release();
  257. }
  258. };
  259. template<typename __ResourceTraits, typename __DeleterArgType>
  260. ResourceWrapperEx(__ResourceTraits, __DeleterArgType&& Deleter) ->
  261. ResourceWrapperEx<__ResourceTraits, std::remove_reference_t<__DeleterArgType>>;
  262. template<typename __ResourceTraits, typename __DeleterArgType>
  263. ResourceWrapperEx(__ResourceTraits, const typename __ResourceTraits::HandleType& Handle, __DeleterArgType&& Deleter) ->
  264. ResourceWrapperEx<__ResourceTraits, std::remove_reference_t<__DeleterArgType>>;
  265. namespace ResourceTraits {
  266. template<typename __ClassType>
  267. struct CppObject {
  268. using HandleType = __ClassType*;
  269. static inline const HandleType InvalidValue = nullptr;
  270. [[nodiscard]]
  271. static bool IsValid(const HandleType& Handle) noexcept {
  272. return Handle != InvalidValue;
  273. }
  274. static void Release(const HandleType& Handle) {
  275. delete Handle;
  276. }
  277. };
  278. template<typename __ElementType>
  279. struct CppArray {
  280. using HandleType = __ElementType*;
  281. static inline const HandleType InvalidValue = nullptr;
  282. [[nodiscard]]
  283. static bool IsValid(const HandleType& Handle) noexcept {
  284. return Handle != InvalidValue;
  285. }
  286. static void Release(const HandleType& Handle) {
  287. delete[] Handle;
  288. }
  289. };
  290. }
  291. }