assetPtr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _ASSET_PTR_H_
  23. #define _ASSET_PTR_H_
  24. #ifndef _ASSET_MANAGER_H_
  25. #include "assetManager.h"
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. class AssetPtrCallback
  29. {
  30. friend class AssetManager;
  31. protected:
  32. virtual void onAssetRefreshed( AssetPtrBase* pAssetPtrBase ) = 0;
  33. };
  34. //-----------------------------------------------------------------------------
  35. class AssetPtrBase
  36. {
  37. public:
  38. AssetPtrBase() {};
  39. virtual ~AssetPtrBase()
  40. {
  41. // Un-register any notifications.
  42. unregisterRefreshNotify();
  43. };
  44. /// Referencing.
  45. virtual void clear( void ) = 0;
  46. virtual void setAssetId( const char* pAssetId ) = 0;
  47. virtual StringTableEntry getAssetId( void ) const = 0;
  48. virtual StringTableEntry getAssetType( void ) const = 0;
  49. virtual bool isAssetId( const char* pAssetId ) const = 0;
  50. /// Validity.
  51. virtual bool isNull( void ) const = 0;
  52. virtual bool notNull( void ) const = 0;
  53. /// Notification.
  54. inline void registerRefreshNotify( AssetPtrCallback* pCallback )
  55. {
  56. // Sanity!
  57. AssertFatal( AssetDatabase.isProperlyAdded(), "AssetPtrBase::registerRefreshNotify() - Cannot register an asset pointer with the asset system." );
  58. // register refresh notify.
  59. AssetDatabase.registerAssetPtrRefreshNotify( this, pCallback );
  60. }
  61. void unregisterRefreshNotify( void )
  62. {
  63. // Un-register the refresh notify if the asset system is available.
  64. if ( AssetDatabase.isProperlyAdded() )
  65. AssetDatabase.unregisterAssetPtrRefreshNotify( this );
  66. }
  67. };
  68. //-----------------------------------------------------------------------------
  69. template<typename T> class AssetPtr : public AssetPtrBase
  70. {
  71. private:
  72. SimObjectPtr<T> mpAsset;
  73. public:
  74. AssetPtr() {}
  75. AssetPtr( const char* pAssetId )
  76. {
  77. // Finish if this is an invalid asset Id.
  78. if ( pAssetId == NULL || *pAssetId == 0 )
  79. return;
  80. // Acquire asset.
  81. mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
  82. }
  83. AssetPtr( const AssetPtr<T>& assetPtr )
  84. {
  85. // Does the asset pointer have an asset?
  86. if ( assetPtr.notNull() )
  87. {
  88. // Yes, so acquire the asset.
  89. mpAsset = AssetDatabase.acquireAsset<T>( assetPtr->getAssetId() );
  90. }
  91. }
  92. virtual ~AssetPtr()
  93. {
  94. // Do we have an asset?
  95. if ( notNull() )
  96. {
  97. // Yes, so release it.
  98. AssetDatabase.releaseAsset( mpAsset->getAssetId() );
  99. }
  100. }
  101. /// Assignment.
  102. AssetPtr<T>& operator=( const char* pAssetId )
  103. {
  104. // Do we have an asset?
  105. if ( notNull() )
  106. {
  107. // Yes, so finish if the asset Id is already assigned.
  108. if ( isAssetId( pAssetId ) )
  109. return *this;
  110. // No, so release it.
  111. AssetDatabase.releaseAsset( mpAsset->getAssetId() );
  112. }
  113. // Is the asset Id at least okay to attempt to acquire the asset?
  114. if ( pAssetId != NULL && *pAssetId != 0 )
  115. {
  116. // Yes, so acquire the asset.
  117. mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
  118. }
  119. else
  120. {
  121. // No, so remove reference.
  122. mpAsset = NULL;
  123. }
  124. // Return Reference.
  125. return *this;
  126. }
  127. AssetPtr<T>& operator=( const AssetPtr<T>& assetPtr )
  128. {
  129. // Set asset pointer.
  130. *this = assetPtr->getAssetId();
  131. // Return Reference.
  132. return *this;
  133. }
  134. /// Referencing.
  135. virtual void clear( void )
  136. {
  137. // Do we have an asset?
  138. if ( notNull() )
  139. {
  140. // Yes, so release it.
  141. AssetDatabase.releaseAsset( mpAsset->getAssetId() );
  142. }
  143. // Reset the asset reference.
  144. mpAsset = NULL;
  145. }
  146. T* operator->( void ) const { return mpAsset; }
  147. T& operator*( void ) const { return *mpAsset; }
  148. operator T*( void ) const { return mpAsset; }
  149. virtual void setAssetId( const char* pAssetId ) { *this = pAssetId; }
  150. virtual StringTableEntry getAssetId( void ) const { return isNull() ? StringTable->EmptyString() : mpAsset->getAssetId(); }
  151. virtual StringTableEntry getAssetType(void) const { return isNull() ? StringTable->EmptyString() : mpAsset->getClassName(); }
  152. virtual bool isAssetId( const char* pAssetId ) const { return pAssetId == NULL ? isNull() : getAssetId() == StringTable->insert(pAssetId); }
  153. /// Validity.
  154. virtual bool isNull( void ) const { return mpAsset.isNull(); }
  155. virtual bool notNull( void ) const { return !mpAsset.isNull(); }
  156. };
  157. #endif // _ASSET_PTR_H_