slang::Bag class

Bag - A general container of arbitrary objects.

The Bag container is a collection of various type-erased objects that can be looked up by their original type. This is useful for things like passing around a collection of various options to different subsystems without needing to have cross dependencies between them.

Public functions

auto empty() const -> bool
Returns true if there are no items in the bag.
template<typename T>
void set(const T& item)
Adds or overwrites an existing element of type T in the bag (making a copy in the process).
template<typename T>
void set(T&& item)
Adds or overwrites an existing element of type T in the bag (moving in the new item in the process).
template<typename T>
auto get() const -> const T*
Gets an element of type T from the bag, if it exists.
template<typename T>
auto insertOrGet() -> T&
Gets an element of type T from the bag, if it exists.
template<typename T>
auto getOrDefault() const -> T
Gets an element of type T from the bag, if it exists.

Function documentation

template<typename T>
const T* slang::Bag::get() const

Gets an element of type T from the bag, if it exists.

Otherwise returns nullptr.

template<typename T>
T& slang::Bag::insertOrGet()

Gets an element of type T from the bag, if it exists.

Otherwise adds a default constructed element to the bag and returns a reference to it.

template<typename T>
T slang::Bag::getOrDefault() const

Gets an element of type T from the bag, if it exists.

Otherwise returns a default constructed T.