ResourceObject.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include <type_traits>
  3. template<typename __ResourceTraits>
  4. class ResourceObject {
  5. public:
  6. using HandleType = typename __ResourceTraits::HandleType;
  7. private:
  8. HandleType _Handle;
  9. public:
  10. ResourceObject() noexcept :
  11. _Handle(__ResourceTraits::InvalidValue) {}
  12. explicit ResourceObject(const HandleType& Handle) noexcept :
  13. _Handle(Handle) {}
  14. //
  15. // Copy constructor is not allowed
  16. //
  17. ResourceObject(const ResourceObject<__ResourceTraits>& Other) = delete;
  18. ResourceObject(ResourceObject<__ResourceTraits>&& Other) noexcept :
  19. _Handle(Other._Handle)
  20. {
  21. Other._Handle = __ResourceTraits::InvalidValue;
  22. }
  23. //
  24. // Copy assignment is not allowed
  25. //
  26. ResourceObject<__ResourceTraits>&
  27. operator=(const ResourceObject<__ResourceTraits>& Other) = delete;
  28. ResourceObject<__ResourceTraits>&
  29. operator=(ResourceObject<__ResourceTraits>&& Other) noexcept {
  30. _Handle = Other._Handle;
  31. Other._Handle = __ResourceTraits::InvalidValue;
  32. return *this;
  33. }
  34. template<typename __DummyType = int,
  35. typename = typename std::enable_if<std::is_pointer<HandleType>::value, __DummyType>::type>
  36. HandleType operator->() const noexcept {
  37. return _Handle;
  38. }
  39. operator HandleType() const noexcept {
  40. return _Handle;
  41. }
  42. // Check if handle is a valid handle
  43. bool IsValid() const noexcept {
  44. return _Handle != __ResourceTraits::InvalidValue;
  45. }
  46. HandleType Get() const noexcept {
  47. return _Handle;
  48. }
  49. void TakeOver(const HandleType& Handle) noexcept {
  50. if (_Handle != __ResourceTraits::InvalidValue) {
  51. __ResourceTraits::Releasor(_Handle);
  52. _Handle = __ResourceTraits::InvalidValue;
  53. } else {
  54. _Handle = Handle;
  55. }
  56. }
  57. void Abandon() noexcept {
  58. _Handle = __ResourceTraits::InvalidValue;
  59. }
  60. // Force release
  61. void Release() {
  62. if (_Handle != __ResourceTraits::InvalidValue) {
  63. __ResourceTraits::Releasor(_Handle);
  64. _Handle = __ResourceTraits::InvalidValue;
  65. }
  66. }
  67. ~ResourceObject() {
  68. if (_Handle != __ResourceTraits::InvalidValue) {
  69. __ResourceTraits::Releasor(_Handle);
  70. _Handle = __ResourceTraits::InvalidValue;
  71. }
  72. }
  73. };
  74. template<typename __ClassType>
  75. struct CppObjectTraits {
  76. using HandleType = __ClassType*;
  77. static inline const HandleType InvalidValue = nullptr;
  78. static inline void Releasor(HandleType pObject) {
  79. delete pObject;
  80. }
  81. };
  82. template<typename __ClassType>
  83. struct CppDynamicArrayTraits {
  84. using HandleType = __ClassType*;
  85. static inline const HandleType InvalidValue = nullptr;
  86. static inline void Releasor(HandleType pArray) {
  87. delete[] pArray;
  88. }
  89. };