resource.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef __RESOURCE_H__
  23. #define __RESOURCE_H__
  24. #ifndef _DATACHUNKER_H_
  25. #include "core/dataChunker.h"
  26. #endif
  27. #ifndef _PATH_H_
  28. #include "core/util/path.h"
  29. #endif
  30. #ifndef _REFBASE_H_
  31. #include "core/util/refBase.h"
  32. #endif
  33. #ifndef _TIMECLASS_H_
  34. #include "core/util/timeClass.h"
  35. #endif
  36. #ifndef _TSIGNAL_H_
  37. #include "core/util/tSignal.h"
  38. #endif
  39. #ifndef _PLATFORMASSERT_H_
  40. #include "platform/platformAssert.h"
  41. #endif
  42. #include <memory>
  43. class ResourceManager;
  44. // This is a utility class used by the resource manager.
  45. // The prime responsibility of this class is to delete
  46. // a resource. The base type is never used, but a template
  47. // version is always derived that knows how to delete the
  48. // particular type of resource. Normally, one needs not
  49. // worry about this class. However, if one wants to delete
  50. // a particular resource in a special manner, one may
  51. // override the ResourceHolder<T>::~ResourceHolder method.
  52. class ResourceHolderBase
  53. {
  54. public:
  55. static FreeListChunker<ResourceHolderBase> smHolderFactory;
  56. ResourceHolderBase() = default; // Default constructor
  57. virtual ~ResourceHolderBase() {}
  58. // Return void pointer to resource data.
  59. void* getResource() const { return mRes.get(); }
  60. protected:
  61. // Construct a resource holder pointing at 'p'.
  62. template<typename T>
  63. ResourceHolderBase(T* p) : mRes(p, [](void*) {}) {}
  64. std::unique_ptr<void, void(*)(void*)> mRes{ nullptr, [](void*) {} };
  65. };
  66. // All resources are derived from this type. The base type
  67. // is only instantiated by the resource manager
  68. // (the resource manager will return a base resource which a
  69. // derived resource, Resource<T>, will construct itself
  70. // with). Base class handles locking and unlocking and
  71. // provides several virtual functions to be defined by
  72. // derived resource.
  73. class ResourceBase
  74. {
  75. friend class ResourceManager;
  76. protected:
  77. class Header;
  78. public:
  79. typedef U32 Signature;
  80. public:
  81. ResourceBase(Header *header) { mResourceHeader = (header ? header : &smBlank); }
  82. virtual ~ResourceBase() {}
  83. const Torque::Path &getPath() const
  84. {
  85. AssertFatal(mResourceHeader != NULL,"ResourceBase::getPath called on invalid resource");
  86. return mResourceHeader->getPath();
  87. }
  88. U32 getChecksum() const
  89. {
  90. AssertFatal(mResourceHeader != NULL,"ResourceBase::getChecksum called on invalid resource");
  91. return mResourceHeader->getChecksum();
  92. }
  93. protected:
  94. typedef void ( *NotifyUnloadFn )( const Torque::Path& path, void* resource );
  95. class Header : public StrongRefBase
  96. {
  97. public:
  98. Header()
  99. : mSignature(0),
  100. mResource(NULL),
  101. mNotifyUnload( NULL )
  102. {
  103. }
  104. const Torque::Path &getPath() const { return mPath; }
  105. Signature getSignature() const { return mSignature; }
  106. void *getResource() const { return (mResource ? mResource->getResource() : NULL); }
  107. U32 getChecksum() const;
  108. void destroySelf() override;
  109. private:
  110. friend class ResourceBase;
  111. friend class ResourceManager;
  112. Signature mSignature;
  113. ResourceHolderBase* mResource;
  114. Torque::Path mPath;
  115. NotifyUnloadFn mNotifyUnload;
  116. };
  117. protected:
  118. static Header smBlank;
  119. ResourceBase() : mResourceHeader(&smBlank) {}
  120. StrongRefPtr<Header> mResourceHeader;
  121. void assign(const ResourceBase &inResource, void* resource = NULL);
  122. // The following functions are virtual, but cannot be pure-virtual
  123. // because we need to be able to instantiate this class.
  124. // To be defined by derived class. Creates a resource
  125. // holder of the desired type. Resource template handles
  126. // this, so one should never need to override.
  127. virtual ResourceHolderBase *createHolder(void *)
  128. {
  129. AssertFatal(0,"ResourceBase::createHolder: should not be called");
  130. return NULL;
  131. }
  132. // Create a Resource of desired type using passed path. Derived
  133. // resource class must define this.
  134. virtual void *create(const Torque::Path &path)
  135. {
  136. AssertFatal(0,"ResourceBase::create: should not be called");
  137. return NULL;
  138. }
  139. // Return signature for desired type.
  140. virtual Signature getSignature() const
  141. {
  142. return mResourceHeader->getSignature();
  143. }
  144. virtual Signal<bool(const Torque::Path &, void**)> &getStaticLoadSignal()
  145. {
  146. AssertFatal(0,"ResourceBase::getStaticLoadSignal: should not be called");
  147. static Signal<bool(const Torque::Path &, void**)> sLoadSignal;
  148. return sLoadSignal;
  149. }
  150. virtual void _triggerPostLoadSignal() {}
  151. virtual NotifyUnloadFn _getNotifyUnloadFn() { return ( NotifyUnloadFn ) NULL; }
  152. };
  153. // This is a utility class used by resource manager. Classes derived
  154. // from this template pretty much just know how to delete the template's
  155. // type.
  156. template<class T>
  157. class ResourceHolder : public ResourceHolderBase
  158. {
  159. public:
  160. ResourceHolder(T* t) : ResourceHolderBase(t) {}
  161. virtual ~ResourceHolder() {
  162. if (mRes) {
  163. T* typedmRes = static_cast<T*>(mRes.get());
  164. typedmRes->~T(); // Call the destructor explicitly
  165. }
  166. }
  167. };
  168. // Resource template. When dealing with resources, this is the
  169. // class that will be used. One creates resources by opening or
  170. // creating them via the resource manager. The resource manager
  171. // returns ResourceBases, which can be used to construct any
  172. // type of resource (see the constructors for this template).
  173. // When instantiating classes using this template, it is always
  174. // necessary to define the create and getSignature methods.
  175. // The createMethod will be responsible for loading a resource
  176. // from disk using passed path.
  177. template<class T> class Resource : public ResourceBase
  178. {
  179. public:
  180. Resource() {}
  181. Resource(const ResourceBase &base) { assign(base); }
  182. void operator=(const ResourceBase & base) { assign(base); }
  183. T* operator->() { return getResource(); }
  184. T& operator*() { return *getResource(); }
  185. operator T*() { return getResource(); }
  186. const T* operator->() const { return getResource(); }
  187. const T& operator*() const { return *getResource(); }
  188. operator const T*() const { return getResource(); }
  189. void setResource(const ResourceBase & base, void* resource) { assign(base, resource); }
  190. static Signature signature();
  191. /// Registering with this signal will give an opportunity to handle resource
  192. /// creation before calling the create() function. This may be used to handle
  193. /// file extensions differently and allow a more plugin-like approach to
  194. /// adding resources. Using this mechanism, one could, for example, override
  195. /// the default methods for loading DTS without touching the main source.
  196. static Signal<bool(const Torque::Path &, void**)> &getLoadSignal()
  197. {
  198. static Signal<bool(const Torque::Path &, void**)> sLoadSignal;
  199. return sLoadSignal;
  200. }
  201. /// Register with this signal to get notified when resources of this type
  202. /// have been loaded.
  203. static Signal< void( Resource< T >& ) >& getPostLoadSignal()
  204. {
  205. static Signal< void( Resource< T >& ) > sPostLoadSignal;
  206. return sPostLoadSignal;
  207. }
  208. /// Register with this signal to get notified when resources of this type
  209. /// are about to get unloaded.
  210. static Signal< void( const Torque::Path&, T* ) >& getUnloadSignal()
  211. {
  212. static Signal< void( const Torque::Path&, T* ) > sUnloadSignal;
  213. return sUnloadSignal;
  214. }
  215. private:
  216. T *getResource() { return (T*)mResourceHeader->getResource(); }
  217. const T *getResource() const { return (T*)mResourceHeader->getResource(); }
  218. Signature getSignature() const override { return Resource<T>::signature(); }
  219. ResourceHolderBase *createHolder(void *) override;
  220. Signal<bool(const Torque::Path &, void**)> &getStaticLoadSignal() override { return getLoadSignal(); }
  221. static void _notifyUnload( const Torque::Path& path, void* resource ) { getUnloadSignal().trigger( path, ( T* ) resource ); }
  222. void _triggerPostLoadSignal() override { getPostLoadSignal().trigger( *this ); }
  223. NotifyUnloadFn _getNotifyUnloadFn() override { return ( NotifyUnloadFn ) &_notifyUnload; }
  224. // These are to be define by instantiated resources
  225. // No generic version is provided...however, since
  226. // base resources are instantiated by resource manager,
  227. // these are not pure virtuals if undefined (but will assert)...
  228. void *create(const Torque::Path &path) override;
  229. };
  230. template<class T> inline ResourceHolderBase *Resource<T>::createHolder(void *ptr)
  231. {
  232. ResourceHolder<T> *resHolder = (ResourceHolder<T>*)(ResourceHolderBase::smHolderFactory.alloc());
  233. resHolder = constructInPlace(resHolder,(T*)ptr);
  234. return resHolder;
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Load Signal Hooks.
  238. //-----------------------------------------------------------------------------
  239. /// This template may be used to register a load signal as follows:
  240. /// static ResourceRegisterLoadSignal<T> sgAuto( staticLoadFunction );
  241. template <class T>
  242. class ResourceRegisterLoadSignal
  243. {
  244. public:
  245. ResourceRegisterLoadSignal( Delegate<bool(const Torque::Path &, void **)> func )
  246. {
  247. Resource<T>::getLoadSignal().notify( func );
  248. }
  249. };
  250. template< class T >
  251. class ResourceRegisterPostLoadSignal
  252. {
  253. public:
  254. ResourceRegisterPostLoadSignal( Delegate< void( Resource< T >& ) > func )
  255. {
  256. Resource< T >::getPostLoadSignal().notify( func );
  257. }
  258. };
  259. template< class T >
  260. class ResourceRegisterUnloadSignal
  261. {
  262. public:
  263. ResourceRegisterUnloadSignal( Delegate< void( const Torque::Path&, T* ) > func )
  264. {
  265. Resource< T >::getUnloadSignal().notify( func );
  266. }
  267. };
  268. #endif // __RESOURCE_H__