Gromacs
2020-beta2
|
#include <gromacs/utility/any.h>
Represents a dynamically typed value of an arbitrary type.
To create a any, either initialize it as empty, or with the create() method (or the equivalent constructor, if the type parameter can be deduced and is clear to the reader from the context).
To query the type of the contents in the any, use isEmpty(), type(), and isType().
To access the value, you need to know the type as a compile-time constant (e.g., through branching based on isType()), and then use cast() or tryCast().
Methods in this class do not throw unless otherwise indicated.
This provides essentially the same functionality as boost::any.
Public Member Functions | |
Any () | |
Creates an empty any value. | |
template<typename T , typename = std::enable_if_t<!std::is_same<T, Any>::value>> | |
Any (T &&value) | |
Creates a any that holds the given value. More... | |
Any (const Any &other) | |
Creates a deep copy of a any. More... | |
Any (Any &&other) noexcept | |
Move-constructs a any. | |
Any & | operator= (const Any &other) |
Assigns the any. More... | |
Any & | operator= (Any &&other) noexcept |
Move-assigns the any. | |
bool | isEmpty () const |
Whether any value is stored. | |
std::type_index | type () const |
Returns the dynamic type of the value that is currently stored. | |
template<typename T > | |
bool | isType () const |
Returns whether the type stored matches the template parameter. | |
template<typename T > | |
const T * | tryCast () const |
Tries to get the value as the given type. More... | |
template<typename T > | |
const T & | cast () const |
Gets the value when the type is known. More... | |
template<typename T > | |
T * | tryCastRef () |
Tries to get the value as the given type as a non-const pointer. More... | |
template<typename T > | |
T & | castRef () |
Gets the value when the type is known as a modifiable reference. More... | |
Static Public Member Functions | |
template<typename T > | |
static Any | create (const T &value) |
Creates a any that holds the given value. More... | |
template<typename T > | |
static Any | create (T &&value) |
Creates a any that holds the given value. More... | |
|
inlineexplicit |
Creates a any that holds the given value.
std::bad_alloc | if out of memory. |
|
inline |
Creates a deep copy of a any.
std::bad_alloc | if out of memory. |
|
inline |
Gets the value when the type is known.
T | Type to get (which must match what the any stores). |
Asserts if the any is empty or does not contain the requested type.
|
inline |
Gets the value when the type is known as a modifiable reference.
T | Type to get (which must match what the any stores). |
Asserts if the any is empty or does not contain the requested type.
|
inlinestatic |
Creates a any that holds the given value.
std::bad_alloc | if out of memory. |
This method allows explicitly specifying the template argument, contrary to the templated constructor.
|
inlinestatic |
Creates a any that holds the given value.
std::bad_alloc | if out of memory. |
In addition to allowing specifying the template argument, this method avoids copying when move-construction is possible.
Assigns the any.
std::bad_alloc | if out of memory. |
|
inline |
Tries to get the value as the given type.
T | Type to get. |
|
inline |
Tries to get the value as the given type as a non-const pointer.
T | Type to get. |
This method allows modifying the value in-place, which is useful with more complicated data structures.