flyweight.hpp
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | List of all members
flyweight::flyweight< T, Args > Class Template Reference

#include <flyweight.hpp>

Public Types

using value = T
 
using autorelease_value = autorelease_value< T, flyweight, std::tuple< Args... > >
 

Public Member Functions

 flyweight ()
 
template<typename Creator >
 flyweight (Creator &&creator)
 
template<typename Creator , typename Deleter >
 flyweight (Creator &&creator, Deleter &&deleter)
 
T & get (const std::tuple< Args... > &arg_tuple)
 
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto get (Args &&... args) -> typename std::enable_if< uses_multiple_args, T & >::type
 Alternative to flyweight::get with the arguments unpacked.
 
autorelease_value get_autorelease (const std::tuple< Args... > &arg_tuple)
 
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto get_autorelease (Args &&... args) -> typename std::enable_if< uses_multiple_args, autorelease_value >::type
 Alternative to flyweight::get_autorelease with the arguments unpacked.
 
bool is_loaded (const std::tuple< Args... > &arg_tuple) const
 Check whether the value mapped to the passed arguments is loaded.
 
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto is_loaded (Args &&... args) const -> typename std::enable_if< uses_multiple_args, bool >::type
 Alternative to flyweight::is_loaded with the arguments unpacked.
 
bool release (const std::tuple< Args... > &arg_tuple)
 
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto release (Args &&... args) -> typename std::enable_if< uses_multiple_args, bool >::type
 Alternative to flyweight::release with the arguments unpacked.
 

Protected Attributes

std::unordered_map< std::tuple< Args... >, T, detail::tuple_hasher< Args... > > map
 
std::function< T(Args &&...)> creator
 
std::function< void(T &)> deleter
 

Detailed Description

template<typename T, typename... Args>
class flyweight::flyweight< T, Args >

Factory for flyweight objects of type T, created with arguments of type Args....

When getting a value, the flyweight first checks for an existing value associated with the passed arguments, returning a reference to it if found. Otherwise, a new value is created and cached for future requests. When no longer needed, an existing value may be released by calling flyweight::release.

Flyweights are useful as a way to load heavy objects only once and sharing them whenever necessary, for example images used as icons in an interactive app. Flyweights can also be used to implement function result memoization.

Template Parameters
TValue type.
ArgsArguments mapped to loaded values.

Constructor & Destructor Documentation

◆ flyweight() [1/3]

template<typename T , typename... Args>
flyweight::flyweight< T, Args >::flyweight ( )
inline

Default constructor. Uses default_creator as the value creator and default_deleter as the value deleter.

◆ flyweight() [2/3]

template<typename T , typename... Args>
template<typename Creator >
flyweight::flyweight< T, Args >::flyweight ( Creator &&  creator)
inline

Constructor with custom value creator functor.

Parameters
creatorCreator functor that will be called when creating a mapped value for the first time. It will be called with the arguments passed to flyweight::get or flyweight::get_tuple.

◆ flyweight() [3/3]

template<typename T , typename... Args>
template<typename Creator , typename Deleter >
flyweight::flyweight< T, Args >::flyweight ( Creator &&  creator,
Deleter &&  deleter 
)
inline

Constructor with custom value creator functor and deleter functor.

Parameters
creatorCreator functor that will be called when creating a mapped value for the first time. It will be called with the arguments passed to flyweight::get or flyweight::get_tuple.
deleterDeleter functor that will be called when releasing a mapped value. It will be called by flyweight::release or flyweight::release_tuple with a reference to the value.

Member Function Documentation

◆ get() [1/2]

template<typename T , typename... Args>
T & flyweight::flyweight< T, Args >::get ( const std::tuple< Args... > &  arg_tuple)
inline

Gets the value associated to the passed arguments. If the value was already created, a reference to the existing value is returned. Otherwise, the value is created using the creator functor passed on the flyweight's constructor.

Parameters
argsArguments that represent a value. These will be forwarded to the creator functor if the value is not loaded yet.
Returns
Reference to the value mapped to the passed arguments.

◆ get() [2/2]

template<typename T , typename... Args>
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto flyweight::flyweight< T, Args >::get ( Args &&...  args) -> typename std::enable_if<uses_multiple_args, T&>::type
inline

Alternative to flyweight::get with the arguments unpacked.

◆ get_autorelease() [1/2]

template<typename T , typename... Args>
autorelease_value flyweight::flyweight< T, Args >::get_autorelease ( const std::tuple< Args... > &  arg_tuple)
inline

Alternative to flyweight::get that returns an autorelease_value. This enables the RAII idiom for automatically releasing values.

See also
get

◆ get_autorelease() [2/2]

template<typename T , typename... Args>
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto flyweight::flyweight< T, Args >::get_autorelease ( Args &&...  args) -> typename std::enable_if<uses_multiple_args, autorelease_value>::type
inline

Alternative to flyweight::get_autorelease with the arguments unpacked.

◆ is_loaded() [1/2]

template<typename T , typename... Args>
bool flyweight::flyweight< T, Args >::is_loaded ( const std::tuple< Args... > &  arg_tuple) const
inline

Check whether the value mapped to the passed arguments is loaded.

◆ is_loaded() [2/2]

template<typename T , typename... Args>
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto flyweight::flyweight< T, Args >::is_loaded ( Args &&...  args) const -> typename std::enable_if<uses_multiple_args, bool>::type
inline

Alternative to flyweight::is_loaded with the arguments unpacked.

◆ release() [1/2]

template<typename T , typename... Args>
bool flyweight::flyweight< T, Args >::release ( const std::tuple< Args... > &  arg_tuple)
inline

Release the value mapped to the passed arguments. Trying to release a value that is not loaded is a no-op.

Returns
true if a loaded value was released, false otherwise.

◆ release() [2/2]

template<typename T , typename... Args>
template<bool uses_multiple_args = (sizeof...(Args) > 1)>
auto flyweight::flyweight< T, Args >::release ( Args &&...  args) -> typename std::enable_if<uses_multiple_args, bool>::type
inline

Alternative to flyweight::release with the arguments unpacked.

Member Data Documentation

◆ map

template<typename T , typename... Args>
std::unordered_map<std::tuple<Args...>, T, detail::tuple_hasher<Args...> > flyweight::flyweight< T, Args >::map
protected

Value map. Maps the tuple of arguments to an already loaded value of type T.

◆ creator

template<typename T , typename... Args>
std::function<T(Args&&...)> flyweight::flyweight< T, Args >::creator
protected

Creator function. Wraps the creator functor passed when constructing the flyweight, if any.

◆ deleter

template<typename T , typename... Args>
std::function<void(T&)> flyweight::flyweight< T, Args >::deleter
protected

Deleter function. Wraps the deleter functor passed when constructing the flyweight, if any.


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