resource.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. class ResourceManager;
  43. // This is a utility class used by the resource manager.
  44. // The prime responsibility of this class is to delete
  45. // a resource. The base type is never used, but a template
  46. // version is always derived that knows how to delete the
  47. // particular type of resource. Normally, one needs not
  48. // worry about this class. However, if one wants to delete
  49. // a particular resource in a special manner, one may
  50. // override the ResourceHolder<T>::~ResourceHolder method.
  51. class ResourceHolderBase
  52. {
  53. public:
  54. static FreeListChunker<ResourceHolderBase> smHolderFactory;
  55. virtual ~ResourceHolderBase() {}
  56. // Return void pointer to resource data.
  57. void *getResource() const { return mRes; }
  58. protected:
  59. // Construct a resource holder pointing at 'p'.
  60. ResourceHolderBase(void *p) : mRes(p) {}
  61. void *mRes;
  62. };
  63. // All resources are derived from this type. The base type
  64. // is only instantiated by the resource manager
  65. // (the resource manager will return a base resource which a
  66. // derived resource, Resource<T>, will construct itself
  67. // with). Base class handles locking and unlocking and
  68. // provides several virtual functions to be defined by
  69. // derived resource.
  70. class ResourceBase
  71. {
  72. friend class ResourceManager;
  73. protected:
  74. class Header;
  75. public:
  76. typedef U32 Signature;
  77. public:
  78. ResourceBase(Header *header) { mResourceHeader = (header ? header : &smBlank); }
  79. virtual ~ResourceBase() {}
  80. const Torque::Path &getPath() const
  81. {
  82. AssertFatal(mResourceHeader != NULL,"ResourceBase::getPath called on invalid resource");
  83. return mResourceHeader->getPath();
  84. }
  85. U32 getChecksum() const
  86. {
  87. AssertFatal(mResourceHeader != NULL,"ResourceBase::getChecksum called on invalid resource");
  88. return mResourceHeader->getChecksum();
  89. }
  90. protected:
  91. typedef void ( *NotifyUnloadFn )( const Torque::Path& path, void* resource );
  92. class Header : public StrongRefBase
  93. {
  94. public:
  95. Header()
  96. : mSignature(0),
  97. mResource(NULL),
  98. mNotifyUnload( NULL )
  99. {
  100. }
  101. const Torque::Path &getPath() const { return mPath; }
  102. Signature getSignature() const { return mSignature; }
  103. void *getResource() const { return (mResource ? mResource->getResource() : NULL); }
  104. U32 getChecksum() const;
  105. virtual void destroySelf();
  106. private:
  107. friend class ResourceBase;
  108. friend class ResourceManager;
  109. Signature mSignature;
  110. ResourceHolderBase* mResource;
  111. Torque::Path mPath;
  112. NotifyUnloadFn mNotifyUnload;
  113. };
  114. protected:
  115. static Header smBlank;
  116. ResourceBase() : mResourceHeader(&smBlank) {}
  117. StrongRefPtr<Header> mResourceHeader;
  118. void assign(const ResourceBase &inResource, void* resource = NULL);
  119. // The following functions are virtual, but cannot be pure-virtual
  120. // because we need to be able to instantiate this class.
  121. // To be defined by derived class. Creates a resource
  122. // holder of the desired type. Resource template handles
  123. // this, so one should never need to override.
  124. virtual ResourceHolderBase *createHolder(void *)
  125. {
  126. AssertFatal(0,"ResourceBase::createHolder: should not be called");
  127. return NULL;
  128. }
  129. // Create a Resource of desired type using passed path. Derived
  130. // resource class must define this.
  131. virtual void *create(const Torque::Path &path)
  132. {
  133. AssertFatal(0,"ResourceBase::create: should not be called");
  134. return NULL;
  135. }
  136. // Return signature for desired type.
  137. virtual Signature getSignature() const
  138. {
  139. return mResourceHeader->getSignature();
  140. }
  141. virtual Signal<bool(const Torque::Path &, void**)> &getStaticLoadSignal()
  142. {
  143. AssertFatal(0,"ResourceBase::getStaticLoadSignal: should not be called");
  144. static Signal<bool(const Torque::Path &, void**)> sLoadSignal;
  145. return sLoadSignal;
  146. }
  147. virtual void _triggerPostLoadSignal() {}
  148. virtual NotifyUnloadFn _getNotifyUnloadFn() { return ( NotifyUnloadFn ) NULL; }
  149. };
  150. // This is a utility class used by resource manager. Classes derived
  151. // from this template pretty much just know how to delete the template's
  152. // type.
  153. template<class T> class ResourceHolder : public ResourceHolderBase
  154. {
  155. public:
  156. ResourceHolder(T *t) : ResourceHolderBase(t) {}
  157. virtual ~ResourceHolder() { delete ((T*)mRes); }
  158. };
  159. // Resource template. When dealing with resources, this is the
  160. // class that will be used. One creates resources by opening or
  161. // creating them via the resource manager. The resource manager
  162. // returns ResourceBases, which can be used to construct any
  163. // type of resource (see the constructors for this template).
  164. // When instantiating classes using this template, it is always
  165. // necessary to define the create and getSignature methods.
  166. // The createMethod will be responsible for loading a resource
  167. // from disk using passed path.
  168. template<class T> class Resource : public ResourceBase
  169. {
  170. public:
  171. Resource() {}
  172. Resource(const ResourceBase &base) { assign(base); }
  173. void operator=(const ResourceBase & base) { assign(base); }
  174. T* operator->() { return getResource(); }
  175. T& operator*() { return *getResource(); }
  176. operator T*() { return getResource(); }
  177. const T* operator->() const { return getResource(); }
  178. const T& operator*() const { return *getResource(); }
  179. operator const T*() const { return getResource(); }
  180. void setResource(const ResourceBase & base, void* resource) { assign(base, resource); }
  181. static Signature signature();
  182. /// Registering with this signal will give an opportunity to handle resource
  183. /// creation before calling the create() function. This may be used to handle
  184. /// file extensions differently and allow a more plugin-like approach to
  185. /// adding resources. Using this mechanism, one could, for example, override
  186. /// the default methods for loading DTS without touching the main source.
  187. static Signal<bool(const Torque::Path &, void**)> &getLoadSignal()
  188. {
  189. static Signal<bool(const Torque::Path &, void**)> sLoadSignal;
  190. return sLoadSignal;
  191. }
  192. /// Register with this signal to get notified when resources of this type
  193. /// have been loaded.
  194. static Signal< void( Resource< T >& ) >& getPostLoadSignal()
  195. {
  196. static Signal< void( Resource< T >& ) > sPostLoadSignal;
  197. return sPostLoadSignal;
  198. }
  199. /// Register with this signal to get notified when resources of this type
  200. /// are about to get unloaded.
  201. static Signal< void( const Torque::Path&, T* ) >& getUnloadSignal()
  202. {
  203. static Signal< void( const Torque::Path&, T* ) > sUnloadSignal;
  204. return sUnloadSignal;
  205. }
  206. private:
  207. T *getResource() { return (T*)mResourceHeader->getResource(); }
  208. const T *getResource() const { return (T*)mResourceHeader->getResource(); }
  209. Signature getSignature() const { return Resource<T>::signature(); }
  210. ResourceHolderBase *createHolder(void *);
  211. Signal<bool(const Torque::Path &, void**)> &getStaticLoadSignal() { return getLoadSignal(); }
  212. static void _notifyUnload( const Torque::Path& path, void* resource ) { getUnloadSignal().trigger( path, ( T* ) resource ); }
  213. virtual void _triggerPostLoadSignal() { getPostLoadSignal().trigger( *this ); }
  214. virtual NotifyUnloadFn _getNotifyUnloadFn() { return ( NotifyUnloadFn ) &_notifyUnload; }
  215. // These are to be define by instantiated resources
  216. // No generic version is provided...however, since
  217. // base resources are instantiated by resource manager,
  218. // these are not pure virtuals if undefined (but will assert)...
  219. void *create(const Torque::Path &path);
  220. };
  221. template<class T> inline ResourceHolderBase *Resource<T>::createHolder(void *ptr)
  222. {
  223. ResourceHolder<T> *resHolder = (ResourceHolder<T>*)(ResourceHolderBase::smHolderFactory.alloc());
  224. resHolder = constructInPlace(resHolder,(T*)ptr);
  225. return resHolder;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Load Signal Hooks.
  229. //-----------------------------------------------------------------------------
  230. /// This template may be used to register a load signal as follows:
  231. /// static ResourceRegisterLoadSignal<T> sgAuto( staticLoadFunction );
  232. template <class T>
  233. class ResourceRegisterLoadSignal
  234. {
  235. public:
  236. ResourceRegisterLoadSignal( Delegate<bool(const Torque::Path &, void **)> func )
  237. {
  238. Resource<T>::getLoadSignal().notify( func );
  239. }
  240. };
  241. template< class T >
  242. class ResourceRegisterPostLoadSignal
  243. {
  244. public:
  245. ResourceRegisterPostLoadSignal( Delegate< void( Resource< T >& ) > func )
  246. {
  247. Resource< T >::getPostLoadSignal().notify( func );
  248. }
  249. };
  250. template< class T >
  251. class ResourceRegisterUnloadSignal
  252. {
  253. public:
  254. ResourceRegisterUnloadSignal( Delegate< void( const Torque::Path&, T* ) > func )
  255. {
  256. Resource< T >::getUnloadSignal().notify( func );
  257. }
  258. };
  259. #endif // __RESOURCE_H__