flyweight.hpp
|
#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 |
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.
T | Value type. |
Args | Arguments mapped to loaded values. |
|
inline |
Default constructor. Uses default_creator
as the value creator and default_deleter
as the value deleter.
|
inline |
Constructor with custom value creator functor.
creator | Creator 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 . |
|
inline |
Constructor with custom value creator functor and deleter functor.
creator | Creator 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 . |
deleter | Deleter 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. |
|
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.
args | Arguments that represent a value. These will be forwarded to the creator functor if the value is not loaded yet. |
|
inline |
Alternative to flyweight::get
with the arguments unpacked.
|
inline |
Alternative to flyweight::get
that returns an autorelease_value
. This enables the RAII idiom for automatically releasing values.
|
inline |
Alternative to flyweight::get_autorelease
with the arguments unpacked.
|
inline |
Check whether the value mapped to the passed arguments is loaded.
|
inline |
Alternative to flyweight::is_loaded
with the arguments unpacked.
|
inline |
Release the value mapped to the passed arguments. Trying to release a value that is not loaded is a no-op.
true
if a loaded value was released, false
otherwise.
|
inline |
Alternative to flyweight::release
with the arguments unpacked.
|
protected |
Value map. Maps the tuple of arguments to an already loaded value of type T
.
|
protected |
Creator function. Wraps the creator functor passed when constructing the flyweight, if any.
|
protected |
Deleter function. Wraps the deleter functor passed when constructing the flyweight, if any.