CUGL 2.1
Cornell University Game Library
Public Member Functions | Protected 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 waitCount () const override
 
void unloadAll () 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
 
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)
 
bool contains (const std::string key) const
 
bool complete () const
 
float progress () const
 

Protected Member Functions

virtual std::vector< std::string > keys () override
 
bool purge (const std::string key) override
 
bool verify (const std::string key) const override
 
- 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 purge (const std::shared_ptr< JsonValue > &json)
 

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::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

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

◆ 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

◆ keys()

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

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

◆ purge()

template<class T>
bool cugl::Loader< T >::purge ( const std::string  key)
inlineoverrideprotectedvirtual

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
inlineoverrideprotectedvirtual

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.

◆ waitCount()

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

Returns the number of textures waiting to load.

This is a rough way to determine how many texture are still pending. A texture is pending if it has been loaded asychronously, and the loading process has not yet finished. This method counts each texture equally regardless of the memory requirements of the format.

Returns
the number of textures waiting to load.

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: