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< AbstractValue > | Clone () 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 | |
AbstractValue & | operator= (const AbstractValue &)=delete |
AbstractValue (AbstractValue &&)=delete | |
AbstractValue & | operator= (AbstractValue &&)=delete |
Static Public Member Functions | |
static std::unique_ptr< AbstractValue > | Make () |
Constructs an AbstractValue using T's default constructor, if available. More... | |
template<typename T > | |
static std::unique_ptr< AbstractValue > | Make (const T &value) |
Returns an AbstractValue containing the given value . More... | |
|
delete |
|
delete |
|
virtual |
|
pure virtual |
Returns a copy of this AbstractValue.
Implemented in Value< T >.
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.
T | The 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. |
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.
T | The 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. |
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.
|
static |
Constructs an AbstractValue using T's default constructor, if available.
This is only available for T's that support default construction.
|
static |
Returns an AbstractValue containing the given value
.
T* maybe_get_mutable_value | ( | ) |
Returns the mutable value wrapped in this AbstractValue, if T matches the originally declared type of this AbstractValue.
T | The originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, returns nullptr. |
const T* maybe_get_value | ( | ) | const |
Returns the value wrapped in this AbstractValue, if T matches the originally declared type of this AbstractValue.
T | The originally declared type of this AbstractValue, e.g., from AbstractValue::Make<T>() or Value<T>::Value(). If T does not match, returns nullptr. |
|
delete |
|
delete |
void set_value | ( | const T & | v | ) |
Sets the value wrapped in this AbstractValue.
T | The 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. |
|
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 >.
|
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 >.
|
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 >.