Drake
Drake C++ Documentation
Loading...
Searching...
No Matches
Value< T > Class Template Reference

Detailed Description

template<typename T>
class drake::Value< T >

A container class for an arbitrary type T (with some restrictions).

This class inherits from AbstractValue and therefore at runtime can be passed between functions without mentioning T.

Example:

void print_string(const AbstractValue& arg) {
const std::string& message = arg.get_value<std::string>();
std::cerr << message;
}
void meow() {
const Value<std::string> value("meow");
print_string(value);
}
AbstractValue(const AbstractValue &)=delete
const T & get_value() const
Returns the value wrapped in this AbstractValue as a const reference.
Definition value.h:90
A container class for an arbitrary type T (with some restrictions).
Definition value.h:199

(Advanced.) User-defined classes with additional features may subclass Value, but should take care to override Clone().

Template Parameters
TMust be copy-constructible or cloneable. Must not be a pointer, array, nor have const, volatile, or reference qualifiers.

#include <drake/common/value.h>

Public Member Functions

 Value ()
 Constructs a Value<T> using T's default constructor, if available.
 Value (const T &v)
 Constructs a Value<T> by copying or cloning the given value v.
template<typename... Args>
 Value (Args &&... args)
 Constructs a Value<T> by forwarding the given args to T's constructor, if available.
 Value (std::unique_ptr< T > v)
 Constructs a Value<T> by copying or moving the given value v.
 ~Value () override=default
const T & get_value () const
 Returns a const reference to the stored value.
T & get_mutable_value ()
 Returns a mutable reference to the stored value.
void set_value (const T &v)
 Replaces the stored value with a new one.
std::unique_ptr< AbstractValueClone () const override
 Returns a copy of this AbstractValue.
void SetFrom (const AbstractValue &other) override
 Copies the value in other to this value.
const std::type_info & type_info () const final
 Returns typeid of the contained object of type T.
const std::type_info & static_type_info () const final
 Returns typeid(T) for this Value<T> object.
std::string GetNiceTypeName () const
 Returns a human-readable name for the underlying type T.
Does not allow copy, move, or assignment
 Value (const Value &)=delete
Valueoperator= (const Value &)=delete
 Value (Value &&)=delete
Valueoperator= (Value &&)=delete
Public Member Functions inherited from AbstractValue
virtual ~AbstractValue ()
template<typename T>
const T & get_value () const
 Returns the value wrapped in this AbstractValue as a const reference.
template<typename T>
T & get_mutable_value ()
 Returns the value wrapped in this AbstractValue as mutable reference.
template<typename T>
void set_value (const T &v)
 Sets the value wrapped in this AbstractValue.
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.
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.
std::string GetNiceTypeName () const
 Returns a human-readable name for the underlying type T.
 AbstractValue (const AbstractValue &)=delete
AbstractValueoperator= (const AbstractValue &)=delete
 AbstractValue (AbstractValue &&)=delete
AbstractValueoperator= (AbstractValue &&)=delete

Additional Inherited Members

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

Constructor & Destructor Documentation

◆ Value() [1/6]

template<typename T>
Value ( const Value< T > & )
delete

◆ Value() [2/6]

template<typename T>
Value ( Value< T > && )
delete

◆ Value() [3/6]

template<typename T>
Value ( )

Constructs a Value<T> using T's default constructor, if available.

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

◆ Value() [4/6]

template<typename T>
Value ( const T & v)
explicit

Constructs a Value<T> by copying or cloning the given value v.

◆ Value() [5/6]

template<typename T>
template<typename... Args>
Value ( Args &&... args)
explicit

Constructs a Value<T> by forwarding the given args to T's constructor, if available.

This is only available for non-primitive T's that are constructible from args.

◆ Value() [6/6]

template<typename T>
Value ( std::unique_ptr< T > v)
explicit

Constructs a Value<T> by copying or moving the given value v.

Precondition
v is non-null.

◆ ~Value()

template<typename T>
~Value ( )
overridedefault

Member Function Documentation

◆ Clone()

template<typename T>
std::unique_ptr< AbstractValue > Clone ( ) const
overridevirtual

Returns a copy of this AbstractValue.

Implements AbstractValue.

◆ get_mutable_value()

template<typename T>
T & get_mutable_value ( )

Returns a mutable reference to the stored value.

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

◆ get_value()

template<typename T>
const T & get_value ( ) const

Returns a const reference to the stored value.

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

◆ GetNiceTypeName()

template<typename T>
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.

◆ operator=() [1/2]

template<typename T>
Value & operator= ( const Value< T > & )
delete

◆ operator=() [2/2]

template<typename T>
Value & operator= ( Value< T > && )
delete

◆ set_value()

template<typename T>
void set_value ( const T & v)

Replaces the stored value with a new one.

◆ SetFrom()

template<typename T>
void SetFrom ( const AbstractValue< T > & other)
overridevirtual

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.

Implements AbstractValue.

◆ static_type_info()

template<typename T>
const std::type_info & static_type_info ( ) const
finalvirtual

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.

Implements AbstractValue.

◆ type_info()

template<typename T>
const std::type_info & type_info ( ) const
finalvirtual

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.

Implements AbstractValue.


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