Drake
Drake C++ Documentation
AbstractValue Class Referenceabstract

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.

#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 >
T * maybe_get_mutable_value ()
 Returns the mutable value wrapped in this AbstractValue, if T matches the originally declared type of this AbstractValue. 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 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

static std::unique_ptr< AbstractValueMake ()
 Constructs an AbstractValue using T's default constructor, if available. More...
 
template<typename T >
static std::unique_ptr< AbstractValueMake (const T &value)
 Returns an AbstractValue containing the given value. More...
 

Constructor & Destructor Documentation

◆ AbstractValue() [1/2]

AbstractValue ( const AbstractValue )
delete

◆ AbstractValue() [2/2]

AbstractValue ( AbstractValue &&  )
delete

◆ ~AbstractValue()

virtual ~AbstractValue ( )
virtual

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.

◆ 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.

◆ Make() [1/2]

static std::unique_ptr<AbstractValue> Make ( )
static

Constructs an AbstractValue using T's default constructor, if available.

This is only available for T's that support default construction.

◆ Make() [2/2]

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

Returns an AbstractValue containing the given value.

◆ maybe_get_mutable_value()

T* maybe_get_mutable_value ( )

Returns the mutable 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.

◆ 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.

◆ operator=() [1/2]

AbstractValue& operator= ( AbstractValue &&  )
delete

◆ operator=() [2/2]

AbstractValue& operator= ( const 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 >.

◆ 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 file: