Drake
AbstractValue Class Referenceabstract

A fully type-erased container class. More...

#include <drake/common/value.h>

Public Member Functions

virtual ~AbstractValue ()
 
template<typename T >
const T & get_value () const
 Returns the value wrapped in this AbstractValue as a const reference. More...
 
template<typename T >
T & get_mutable_value ()
 Returns the value wrapped in this AbstractValue as mutable reference. More...
 
template<typename T >
void set_value (const T &v)
 Sets the value wrapped in this AbstractValue. More...
 
template<typename T >
const T * maybe_get_value () const
 Returns the value wrapped in this AbstractValue, if T matches the originally declared type of this AbstractValue. More...
 
template<typename T >
const T & GetValue () const
 Returns the value wrapped in this AbstractValue, which must be of exactly type T. More...
 
template<typename T >
const T & GetValueOrThrow () const
 Like GetValue, but throws std::logic_error on mismatched types even in Release builds. More...
 
template<typename T >
const T * MaybeGetValue () const
 Like GetValue, but quietly returns nullptr on mismatched types, even in Release builds. More...
 
template<typename T >
T & GetMutableValue ()
 Returns the value wrapped in this AbstractValue, which must be of exactly type T. More...
 
template<typename T >
T & GetMutableValueOrThrow ()
 Like GetMutableValue, but throws std::logic_error on mismatched types even in Release builds. More...
 
template<typename T >
void SetValue (const T &value_to_set)
 Sets the value wrapped in this AbstractValue, which must be of exactly type T. More...
 
template<typename T >
void SetValueOrThrow (const T &value_to_set)
 Like SetValue, but throws on mismatched types even in Release builds. More...
 
virtual std::unique_ptr< AbstractValueClone () const =0
 Returns a copy of this AbstractValue. More...
 
virtual void SetFrom (const AbstractValue &other)=0
 Copies the value in other to this value. More...
 
virtual void SetFromOrThrow (const AbstractValue &other)=0
 Like SetFrom, but throws std::logic_error on mismatched types even in Release builds. More...
 
virtual const std::type_info & type_info () const =0
 Returns typeid of the contained object of type T. More...
 
virtual const std::type_info & static_type_info () const =0
 Returns typeid(T) for this Value<T> object. More...
 
std::string GetNiceTypeName () const
 Returns a human-readable name for the underlying type T. More...
 
Does not allow copy, move, or assignment
 AbstractValue (const AbstractValue &)=delete
 
AbstractValueoperator= (const AbstractValue &)=delete
 
 AbstractValue (AbstractValue &&)=delete
 
AbstractValueoperator= (AbstractValue &&)=delete
 

Static Public Member Functions

template<typename T >
static std::unique_ptr< AbstractValueMake (const T &value)
 Returns an AbstractValue containing the given value. More...
 

Detailed Description

A fully type-erased container class.

An AbstractValue stores an object of some type T (where T is declared during at construction time) that at runtime can be passed between functions without mentioning T. Only when the stored T must be accessed does the user need to mention T again.

(Advanced.) Note that AbstractValue's getters and setters method declare that "If T does not match, a std::logic_error will be thrown with a helpful error message". The code that implements this check uses hashing, so in the extraordinarily unlikely case of a 64-bit hash collision, the error may go undetected in Release builds. (Debug builds have extra checks that will trigger.)

(Advanced.) Only Value should inherit directly from AbstractValue. User-defined classes with additional features may inherit from Value.

Constructor & Destructor Documentation

◆ AbstractValue() [1/2]

AbstractValue ( const AbstractValue )
delete

◆ AbstractValue() [2/2]

AbstractValue ( AbstractValue &&  )
delete

◆ ~AbstractValue()

~AbstractValue ( )
virtualdefault

Member Function Documentation

◆ Clone()

virtual std::unique_ptr<AbstractValue> Clone ( ) const
pure virtual

Returns a copy of this AbstractValue.

Implemented in Value< T >.

◆ get_mutable_value()

T& get_mutable_value ( )

Returns the value wrapped in this AbstractValue as mutable reference.

The reference remains valid only until this object is set or destroyed.

Template Parameters
TThe originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, a std::logic_error will be thrown with a helpful error message.

◆ get_value()

const T& get_value ( ) const

Returns the value wrapped in this AbstractValue as a const reference.

The reference remains valid only until this object is set or destroyed.

Template Parameters
TThe originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, a std::logic_error will be thrown with a helpful error message.

◆ GetMutableValue()

T& GetMutableValue ( )

Returns the value wrapped in this AbstractValue, which must be of exactly type T.

T cannot be a superclass, abstract or otherwise. In Debug builds, if the types don't match, an std::logic_error will be thrown with a helpful error message. In Release builds, this is not guaranteed. Intentionally not const: holders of const references to the AbstractValue should not be able to mutate the value it contains. (Deprecated.)

Deprecated:
"Use get_mutable_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ GetMutableValueOrThrow()

T& GetMutableValueOrThrow ( )

Like GetMutableValue, but throws std::logic_error on mismatched types even in Release builds.

(Deprecated.)

Deprecated:
"Use get_mutable_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ GetNiceTypeName()

std::string GetNiceTypeName ( ) const

Returns a human-readable name for the underlying type T.

This may be slow but is useful for error messages. If T is polymorphic, this returns the typeid of the most-derived type of the contained object.

◆ GetValue()

const T& GetValue ( ) const

Returns the value wrapped in this AbstractValue, which must be of exactly type T.

T cannot be a superclass, abstract or otherwise. In Debug builds, if the types don't match, an std::logic_error will be thrown with a helpful error message. In Release builds, this is not guaranteed. (Deprecated.)

Deprecated:
"Use get_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ GetValueOrThrow()

const T& GetValueOrThrow ( ) const

Like GetValue, but throws std::logic_error on mismatched types even in Release builds.

(Deprecated.)

Deprecated:
"Use get_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ Make()

static std::unique_ptr<AbstractValue> Make ( const T &  value)
static

Returns an AbstractValue containing the given value.

◆ maybe_get_value()

const T* maybe_get_value ( ) const

Returns the value wrapped in this AbstractValue, if T matches the originally declared type of this AbstractValue.

Template Parameters
TThe originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, returns nullptr.

◆ MaybeGetValue()

const T* MaybeGetValue ( ) const

Like GetValue, but quietly returns nullptr on mismatched types, even in Release builds.

Returns
A pointer to the stored value if T is the right type, otherwise nullptr. (Deprecated.)
Deprecated:
"Use maybe_get_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ operator=() [1/2]

AbstractValue& operator= ( const AbstractValue )
delete

◆ operator=() [2/2]

AbstractValue& operator= ( AbstractValue &&  )
delete

◆ set_value()

void set_value ( const T &  v)

Sets the value wrapped in this AbstractValue.

Template Parameters
TThe originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, a std::logic_error will be thrown with a helpful error message.

◆ SetFrom()

virtual void SetFrom ( const AbstractValue other)
pure virtual

Copies the value in other to this value.

If other is not compatible with this object, a std::logic_error will be thrown with a helpful error message.

Implemented in Value< T >.

◆ SetFromOrThrow()

virtual void SetFromOrThrow ( const AbstractValue other)
pure virtual

Like SetFrom, but throws std::logic_error on mismatched types even in Release builds.

(Deprecated.)

Deprecated:
"Use SetFrom() instead."
This will be removed from Drake on or after "2019-07-01" .

Implemented in Value< T >.

◆ SetValue()

void SetValue ( const T &  value_to_set)

Sets the value wrapped in this AbstractValue, which must be of exactly type T.

T cannot be a superclass, abstract or otherwise.

Parameters
value_to_setThe value to be copied or cloned into this AbstractValue. In Debug builds, if the types don't match, an std::logic_error will be thrown with a helpful error message. In Release builds, this is not guaranteed. (Deprecated.)
Deprecated:
"Use set_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ SetValueOrThrow()

void SetValueOrThrow ( const T &  value_to_set)

Like SetValue, but throws on mismatched types even in Release builds.

(Deprecated.)

Deprecated:
"Use set_value<T>() instead."
This will be removed from Drake on or after "2019-07-01" .

◆ static_type_info()

virtual const std::type_info& static_type_info ( ) const
pure virtual

Returns typeid(T) for this Value<T> object.

If T is polymorphic, this does NOT reflect the typeid of the most-derived type of the contained object; the result is always the base type T.

Implemented in Value< T >.

◆ type_info()

virtual const std::type_info& type_info ( ) const
pure virtual

Returns typeid of the contained object of type T.

If T is polymorphic, this returns the typeid of the most-derived type of the contained object.

Implemented in Value< T >.


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