resource.h 11 KB

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