Drake
Drake C++ Documentation
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);
}

(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. More...
 
 Value (const T &v)
 Constructs a Value<T> by copying or cloning the given value v. More...
 
template<typename... Args>
 Value (Args &&... args)
 Constructs a Value<T> by forwarding the given args to T's constructor, if available. More...
 
 Value (std::unique_ptr< T > v)
 Constructs a Value<T> by copying or moving the given value v. More...
 
 ~Value () override
 
const T & get_value () const
 Returns a const reference to the stored value. More...
 
T & get_mutable_value ()
 Returns a mutable reference to the stored value. More...
 
void set_value (const T &v)
 Replaces the stored value with a new one. More...
 
std::unique_ptr< AbstractValueClone () const override
 Returns a copy of this AbstractValue. More...
 
void SetFrom (const AbstractValue &other) override
 Copies the value in other to this value. More...
 
const std::type_info & type_info () const final
 Returns typeid of the contained object of type T. More...
 
const std::type_info & static_type_info () const final
 Returns typeid(T) for this Value<T> object. More...
 
std::string GetNiceTypeName () const
 Returns a human-readable name for the underlying type T. More...
 
virtual const std::type_info & static_type_info () const=0
 Returns typeid(T) for this Value<T> object. More...
 
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. 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...
 
std::string GetNiceTypeName () const
 Returns a human-readable name for the underlying type T. More...
 
 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. More...
 
template<typename T >
static std::unique_ptr< AbstractValueMake (const T &value)
 Returns an AbstractValue containing the given value. More...
 

Constructor & Destructor Documentation

◆ Value() [1/6]

Value ( const Value< T > &  )
delete

◆ Value() [2/6]

Value ( Value< T > &&  )
delete

◆ Value() [3/6]

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]

Value ( const T &  v)
explicit

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

◆ Value() [5/6]

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]

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()

~Value ( )
override

Member Function Documentation

◆ Clone()

std::unique_ptr<AbstractValue> Clone ( ) const
overridevirtual

Returns a copy of this AbstractValue.

Implements AbstractValue.

◆ get_mutable_value()

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()

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()

std::string GetNiceTypeName

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]

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

◆ operator=() [2/2]

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

◆ set_value()

void set_value ( const T &  v)

Replaces the stored value with a new one.

◆ SetFrom()

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() [1/2]

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.

◆ static_type_info() [2/2]

virtual const std::type_info& static_type_info

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.

◆ type_info()

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: