Gromacs  2018.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions
gmx::Variant Class Reference

#include <gromacs/utility/variant.h>

Description

Represents a dynamically typed value of an arbitrary type.

To create a variant, 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 variant, 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

 Variant ()
 Creates an empty variant value.
 
template<typename T >
 Variant (const T &value)
 Creates a variant that holds the given value. More...
 
template<typename T >
 Variant (T &&value)
 Creates a variant that holds the given value. More...
 
 Variant (const Variant &other)
 Creates a deep copy of a variant. More...
 
 Variant (Variant &&other) noexcept
 Move-constructs a variant.
 
Variantoperator= (const Variant &other)
 Assigns the variant. More...
 
Variantoperator= (Variant &&other) noexcept
 Move-assigns the variant.
 
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 Variant create (const T &value)
 Creates a variant that holds the given value. More...
 
template<typename T >
static Variant create (T &&value)
 Creates a variant that holds the given value. More...
 

Constructor & Destructor Documentation

template<typename T >
gmx::Variant::Variant ( const T &  value)
inlineexplicit

Creates a variant that holds the given value.

Exceptions
std::bad_allocif out of memory.
template<typename T >
gmx::Variant::Variant ( T &&  value)
inlineexplicit

Creates a variant that holds the given value.

Exceptions
std::bad_allocif out of memory.
gmx::Variant::Variant ( const Variant other)
inline

Creates a deep copy of a variant.

Exceptions
std::bad_allocif out of memory.

Member Function Documentation

template<typename T >
const T& gmx::Variant::cast ( ) const
inline

Gets the value when the type is known.

Template Parameters
TType to get (which must match what the variant stores).

Asserts if the variant is empty or does not contain the requested type.

template<typename T >
T& gmx::Variant::castRef ( )
inline

Gets the value when the type is known as a modifiable reference.

Template Parameters
TType to get (which must match what the variant stores).

Asserts if the variant is empty or does not contain the requested type.

template<typename T >
static Variant gmx::Variant::create ( const T &  value)
inlinestatic

Creates a variant that holds the given value.

Exceptions
std::bad_allocif out of memory.

This method allows explicitly specifying the template argument, contrary to the templated constructor.

template<typename T >
static Variant gmx::Variant::create ( T &&  value)
inlinestatic

Creates a variant that holds the given value.

Exceptions
std::bad_allocif out of memory.

In addition to allowing specifying the template argument, this method avoids copying when move-construction is possible.

Variant& gmx::Variant::operator= ( const Variant other)
inline

Assigns the variant.

Exceptions
std::bad_allocif out of memory.
template<typename T >
const T* gmx::Variant::tryCast ( ) const
inline

Tries to get the value as the given type.

Template Parameters
TType to get.
Returns
Pointer to the value, or nullptr if the type does not match the stored value.
template<typename T >
T* gmx::Variant::tryCastRef ( )
inline

Tries to get the value as the given type as a non-const pointer.

Template Parameters
TType to get.
Returns
Pointer to the value, or nullptr if the type does not match the stored value.

This method allows modifying the value in-place, which is useful with more complicated data structures.


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