assetPtr.h

Engine/source/assets/assetPtr.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _ASSET_PTR_H_
 25#define _ASSET_PTR_H_
 26
 27#ifndef _ASSET_MANAGER_H_
 28#include "assetManager.h"
 29#endif
 30
 31//-----------------------------------------------------------------------------
 32
 33class AssetPtrCallback
 34{
 35    friend class AssetManager;
 36
 37protected:
 38    virtual void onAssetRefreshed( AssetPtrBase* pAssetPtrBase ) = 0;    
 39};
 40
 41//-----------------------------------------------------------------------------
 42
 43class AssetPtrBase
 44{
 45public:
 46    AssetPtrBase() {};
 47    virtual ~AssetPtrBase()
 48    {
 49        // Un-register any notifications.
 50        unregisterRefreshNotify();
 51    };
 52
 53    /// Referencing.
 54    virtual void clear( void ) = 0;
 55    virtual void setAssetId( const char* pAssetId ) = 0;
 56    virtual StringTableEntry getAssetId( void ) const = 0;
 57    virtual StringTableEntry getAssetType( void ) const = 0;
 58    virtual bool isAssetId( const char* pAssetId ) const = 0;
 59
 60    /// Validity.
 61    virtual bool isNull( void ) const = 0;
 62    virtual bool notNull( void ) const = 0;
 63
 64    /// Notification.
 65    inline void registerRefreshNotify( AssetPtrCallback* pCallback )
 66    {
 67        // Sanity!
 68        AssertFatal( AssetDatabase.isProperlyAdded(), "AssetPtrBase::registerRefreshNotify() - Cannot register an asset pointer with the asset system." );
 69
 70        // register refresh notify.
 71        AssetDatabase.registerAssetPtrRefreshNotify( this, pCallback );
 72    }
 73
 74    void unregisterRefreshNotify( void )
 75    {
 76        // Un-register the refresh notify if the asset system is available.
 77        if ( AssetDatabase.isProperlyAdded() )
 78            AssetDatabase.unregisterAssetPtrRefreshNotify( this );
 79    }
 80};
 81
 82//-----------------------------------------------------------------------------
 83
 84template<typename T> class AssetPtr : public AssetPtrBase
 85{
 86private:
 87    SimObjectPtr<T> mpAsset;
 88
 89public:
 90    AssetPtr() {}
 91    AssetPtr( const char* pAssetId )
 92    {
 93        // Finish if this is an invalid asset Id.
 94        if ( pAssetId == NULL || *pAssetId == 0 )
 95            return;
 96
 97        // Acquire asset.
 98        mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
 99    }
100    AssetPtr( const AssetPtr<T>& assetPtr )
101    {
102        // Does the asset pointer have an asset?
103        if ( assetPtr.notNull() )
104        {
105            // Yes, so acquire the asset.
106            mpAsset = AssetDatabase.acquireAsset<T>( assetPtr->getAssetId() );
107        }
108    }
109    virtual ~AssetPtr()
110    {
111        // Do we have an asset?
112        if ( notNull() )
113        {
114            // Yes, so release it.
115            AssetDatabase.releaseAsset( mpAsset->getAssetId() );
116        }
117    }
118
119    /// Assignment.
120    AssetPtr<T>& operator=( const char* pAssetId )
121    {
122        // Do we have an asset?
123        if ( notNull() )
124        {
125            // Yes, so finish if the asset Id is already assigned.
126            if ( isAssetId( pAssetId ) )
127                return *this;
128
129            // No, so release it.
130            AssetDatabase.releaseAsset( mpAsset->getAssetId() );
131        }
132
133        // Is the asset Id at least okay to attempt to acquire the asset?
134        if ( pAssetId != NULL && *pAssetId != 0 )
135        {
136            // Yes, so acquire the asset.
137            mpAsset = AssetDatabase.acquireAsset<T>( pAssetId );
138        }
139        else
140        {
141            // No, so remove reference.
142            mpAsset = NULL;
143        }
144
145        // Return Reference.
146        return *this;
147    }
148
149    AssetPtr<T>& operator=( const AssetPtr<T>& assetPtr )
150    {
151        // Set asset pointer.
152        *this = assetPtr->getAssetId();
153
154        // Return Reference.
155        return *this;
156    }
157
158    /// Referencing.
159    virtual void clear( void )
160    {
161        // Do we have an asset?
162        if ( notNull() )
163        {
164            // Yes, so release it.
165            AssetDatabase.releaseAsset( mpAsset->getAssetId() );
166        }
167
168        // Reset the asset reference.
169        mpAsset = NULL;
170    }
171
172    T* operator->( void ) const { return mpAsset; }
173    T& operator*( void ) const { return *mpAsset; }
174    operator T*( void ) const { return mpAsset; }
175    virtual void setAssetId( const char* pAssetId ) { *this = pAssetId; }
176    virtual StringTableEntry getAssetId( void ) const { return isNull() ? StringTable->EmptyString() : mpAsset->getAssetId(); }
177    virtual StringTableEntry getAssetType(void) const { return isNull() ? StringTable->EmptyString() : mpAsset->getClassName(); }
178    virtual bool isAssetId( const char* pAssetId ) const { return pAssetId == NULL ? isNull() : getAssetId() == StringTable->insert(pAssetId); }
179
180    /// Validity.
181    virtual bool isNull( void ) const { return mpAsset.isNull(); }
182    virtual bool notNull( void ) const { return !mpAsset.isNull(); }
183};
184
185#endif // _ASSET_PTR_H_
186