CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
cugl::Loader< T > Class Template Reference

#include <CULoader.h>

Inheritance diagram for cugl::Loader< T >:
cugl::BaseLoader cugl::GenericLoader< T >

Public Member Functions

 Loader ()
 
std::shared_ptr< Tget (const std::string key) const
 
std::shared_ptr< Toperator[] (const std::string key) const
 
size_t loadCount () const override
 
size_t inFlight () const override
 
void enqueue (const std::string key)
 
void unloadAll () override
 
virtual std::vector< std::string > keys () override
 
bool purgeKey (const std::string key) override
 
bool verify (const std::string key) const override
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
virtual void dispose ()
 
virtual bool init ()
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads)
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
void setManager (AssetManager *manager)
 
const AssetManagergetManager () const
 
void setJsonKey (const std::string key)
 
const std::string getJsonKey () const
 
void setPriority (Uint32 priority)
 
const Uint32 getPriority () const
 
bool load (const std::string key, const std::string source)
 
bool load (const std::shared_ptr< JsonValue > &json)
 
void loadAsync (const std::string key, const std::string source, LoaderCallback callback)
 
void loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
 
bool unload (const std::string key)
 
bool unload (const std::shared_ptr< JsonValue > &json)
 
virtual void unloadAll ()
 
virtual std::vector< std::string > keys ()
 
bool contains (const std::string key) const
 
void reserve (size_t amount)
 
virtual size_t loadCount () const
 
size_t waitCount () const
 
virtual size_t inFlight () const
 
bool complete () const
 
float progress () const
 

Protected Attributes

std::unordered_map< std::string, std::shared_ptr< T > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::string _jsonKey
 
Uint32 _priority
 
size_t _reserved
 
std::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

Additional Inherited Members

- Protected Member Functions inherited from cugl::BaseLoader
virtual bool read (const std::string key, const std::string source, LoaderCallback callback, bool async)
 
virtual bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async)
 
virtual bool purgeKey (const std::string key)
 
virtual bool purgeJson (const std::shared_ptr< JsonValue > &json)
 
virtual bool verify (const std::string key) const
 

Detailed Description

template<class T>
class cugl::Loader< T >

This class is a specific template for each loader.

This template works like a generic abstract class in Java. It provides some type correctness. It also provides some base functionality that is common for all loaders. Methods marked as abstract must be overriden in specific loader implementations.

All assets are assigned a key and retrieved via that key. This provides a quick way to reference assets.

IMPORTANT: This class is not even remotely thread-safe. Do not call any of these methods outside of the main CUGL thread.

Constructor & Destructor Documentation

◆ Loader()

template<class T >
cugl::Loader< T >::Loader ( )
inline

Creates a degenerate asset loader with no resources

NEVER CALL THIS CONSTRUCTOR. As this is an abstract class, you should call one of the static constructors of the appropriate child class.

Member Function Documentation

◆ enqueue()

template<class T >
void cugl::Loader< T >::enqueue ( const std::string  key)
inline

Queues up an asset for loading.

This method adds the given key to the queue. It also deducts from the reserve count if that value is non-zero.

Parameters
keyThe asset key to queue.

◆ get()

template<class T >
std::shared_ptr< T > cugl::Loader< T >::get ( const std::string  key) const
inline

Returns the asset for the given key.

If the key is valid, the asset is guaranteed not to be null. Otherwise, this method returns nullptr

Parameters
keyThe key associated with the asset
Returns
the asset pointer for the given key

◆ inFlight()

template<class T >
size_t cugl::Loader< T >::inFlight ( ) const
inlineoverridevirtual

Returns the number of assets that are actively being loaded.

This method is different from waitCount in that it does not count reserved assets. It only counts those elements which have been queued, but which have not yet been fully loaded.

Returns
the number of assets that are actively being loaded.

Reimplemented from cugl::BaseLoader.

◆ keys()

template<class T >
virtual std::vector< std::string > cugl::Loader< T >::keys ( )
inlineoverridevirtual

Returns the set of active keys in this loader.

Returns
the set of active keys in this loader.

Reimplemented from cugl::BaseLoader.

◆ loadCount()

template<class T >
size_t cugl::Loader< T >::loadCount ( ) const
inlineoverridevirtual

Returns the number of assets currently loaded.

This is a rough way to determine how many assets have been loaded so far. This method counts each asset equally regardless of the memory requirements of each asset.

Returns
the number of assets currently loaded.

Reimplemented from cugl::BaseLoader.

◆ operator[]()

template<class T >
std::shared_ptr< T > cugl::Loader< T >::operator[] ( const std::string  key) const
inline

Returns the asset for the given key.

If the key is valid, the asset is guaranteed not to be null. Otherwise, this method returns nullptr

Parameters
keyThe key associated with the asset
Returns
the asset pointer for the given key

◆ purgeKey()

template<class T >
bool cugl::Loader< T >::purgeKey ( const std::string  key)
inlineoverridevirtual

Unloads the asset for the given key

An asset may still be available if it is referenced by a smart pointer. See the description of the specific implementation for how assets are released.

This method is abstract and should be overridden in child classes. You will notice that this method is essentially identical to unload. We separated the methods because overloading and virtual methods do not place nice.

Returns
true if the asset was successfully unloaded

Reimplemented from cugl::BaseLoader.

◆ unloadAll()

template<class T >
void cugl::Loader< T >::unloadAll ( )
inlineoverridevirtual

Unloads all assets present in this loader.

An asset may still be available if it is referenced by a smart pointer. See the description of the specific implementation for how assets are released.

Reimplemented from cugl::BaseLoader.

◆ verify()

template<class T >
bool cugl::Loader< T >::verify ( const std::string  key) const
inlineoverridevirtual

Returns true if the key maps to a loaded asset.

This method is useful in case the asset fails to load. You will notice that this method is essentially identical to contains. We separated the methods because overloading and virtual methods do not place nice.

Parameters
keyThe key associated with the asset
Returns
true if the key maps to a loaded asset.

Reimplemented from cugl::BaseLoader.

Member Data Documentation

◆ _assets

template<class T >
std::unordered_map<std::string, std::shared_ptr<T> > cugl::Loader< T >::_assets
protected

Hash map storing the loaded assets

◆ _queue

template<class T >
std::unordered_set<std::string> cugl::Loader< T >::_queue
protected

The assets we are expecting that are not yet loaded


The documentation for this class was generated from the following file: