TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_resource_handle.hpp
1 
10 #ifndef TurtleBrains_ResourceHandle_hpp
11 #define TurtleBrains_ResourceHandle_hpp
12 
13 #include <turtle_brains/core/tb_types.hpp>
14 
15 #include <functional> //for std::hash()
16 
17 namespace TurtleBrains::Core
18 {
19 
24  template<typename Type> class ResourceHandle
25  {
26  public:
31 
36 
44  bool IsValid(void) const;
45 
49  operator bool() const;
50 
57  bool operator==(const ResourceHandle& other) const;
58 
65  bool operator!=(const ResourceHandle& other) const;
66 
67  bool operator<(const ResourceHandle& other) const
68  {
69  return mValue < other.mValue;
70  }
71 
72  //inline size_t GetHash(void) const { return std::hash<tbCore::uint32>(mValue); }
73  inline size_t GetHash(void) const { return mValue; }
74 
75  //Was unable to make this private because the template friend class was not working. I believe it requires
76  //The friend class to have been declared already, which would create circular dependencies.
77  //private:
78  //template<typename ResourceType, typename HandleType> friend class TurtleBrains::Core::ResourceCache;
79 
87 
95  //operator size_t() const;
96 
104  };
105 
106 }; /* namespace TurtleBrains::Core */
107 
108 namespace tbCore = TurtleBrains::Core;
109 
110 //--------------------------------------------------------------------------------------------------------------------//
111 
112 // 2025-11-09: This fixes an issue where std::unordered_map<ResourceHandle<Type>> would complain about the std::hash not
113 // being defined/usable. It does have to be in global space to work.
114 template<typename Type> struct std::hash<TurtleBrains::Core::ResourceHandle<Type>>
115 {
116  std::size_t operator()(const TurtleBrains::Core::ResourceHandle<Type>& handle) const
117  {
118  return handle.GetHash();
119  }
120 };
121 
122 //--------------------------------------------------------------------------------------------------------------------//
123 //--------------------------------------------------------------------------------------------------------------------//
124 //--------------------------------------------------------------------------------------------------------------------//
125 
127  mValue(static_cast<tbCore::uint32>(~0))
128 {
129 }
130 
131 //--------------------------------------------------------------------------------------------------------------------//
132 
134 {
135 }
136 
137 //--------------------------------------------------------------------------------------------------------------------//
138 
139 template<typename Type> bool TurtleBrains::Core::ResourceHandle<Type>::IsValid(void) const
140 {
141  return mValue != static_cast<tbCore::uint32>(~0);
142 }
143 
144 //--------------------------------------------------------------------------------------------------------------------//
145 
146 template<typename Type> TurtleBrains::Core::ResourceHandle<Type>::operator bool() const
147 {
148  return IsValid();
149 }
150 
151 //--------------------------------------------------------------------------------------------------------------------//
152 
153 template<typename Type> bool TurtleBrains::Core::ResourceHandle<Type>::operator==(const ResourceHandle& other) const
154 {
155  return mValue == other.mValue;
156 }
157 
158 //--------------------------------------------------------------------------------------------------------------------//
159 
160 template<typename Type> bool TurtleBrains::Core::ResourceHandle<Type>::operator!=(const ResourceHandle& other) const
161 {
162  return mValue != other.mValue;
163 }
164 
165 //--------------------------------------------------------------------------------------------------------------------//
166 
168  mValue(value)
169 {
170 }
171 
172 //--------------------------------------------------------------------------------------------------------------------//
173 
174 //template<typename Type> TurtleBrains::Core::ResourceHandle<Type>::operator size_t() const
175 //{
176 // return static_cast<size_t>(mValue);
177 //}
178 
179 //--------------------------------------------------------------------------------------------------------------------//
180 
181 #endif /* TurtleBrains_ResourceHandle_hpp */
Definition: tb_resource_handle.hpp:25
ResourceHandle(void)
Definition: tb_resource_handle.hpp:126
tbCore::uint32 mValue
Definition: tb_resource_handle.hpp:103
bool IsValid(void) const
Definition: tb_resource_handle.hpp:139
bool operator==(const ResourceHandle &other) const
Definition: tb_resource_handle.hpp:153
bool operator!=(const ResourceHandle &other) const
Definition: tb_resource_handle.hpp:160
~ResourceHandle(void)
Definition: tb_resource_handle.hpp:133
ResourceHandle(const tbCore::uint32 value)
Definition: tb_resource_handle.hpp:167
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
std::uint32_t uint32
Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1).
Definition: tb_types.hpp:27
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22