RTI Connext Modern C++ API  Version 5.3.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dds::core::optional< T > Class Template Reference

<<value-type>> Represents an object that may not contain a valid value More...

#include <dds/core/Optional.hpp>

Public Member Functions

 optional () OMG_NOEXCEPT
 Create an unset optional object.
 
 optional (const T &value)
 Create an optional object with a copy of a value.
 
 optional (T &&value)
 <<C++11>> Create an optional object moving a value
 
 optional (bool condition, const T &value)
 Create an optional member conditionally set or unset.
 
 optional (bool condition, T &&value)
 <<C++11>> Creates an optional member conditionally set or unset by moving a value.
 
 ~optional ()
 Destroys the underlying object if it exists.
 
void set (const T &value)
 Assigns a copy of an object.
 
void set (T &&value)
 <<C++11>> Assigns an object by moving it
 
bool is_set () const
 Checks if this optional instance contains a valid object.
 
 operator bool () const
 Returns is_set()
 
void reset ()
 Destroys the underlying object and leaves this optional with an invalid (unset) value.
 
const T & get () const
 Retrieves the underlying object if it exists.
 
T & get ()
 Retrieves the underlying object if it exists.
 
const T * get_ptr () const
 Returns &get() if the object is initialized or NULL otherwise.
 
T * get_ptr ()
 Returns &get() if the object is initialized or NULL otherwise.
 
optional< T > & operator= (const optional< T > &other)
 Assignment operator.
 
optional< T > & operator= (optional< T > &&other) OMG_NOEXCEPT
 <<C++11>> Move-assignment operator
 
optional< T > & operator= (const T &value)
 Assign a (valid) value.
 
optional< T > & operator= (T &&value)
 Assign a (valid) value by moving an object.
 

Friends

void swap (optional< T > &left, optional< T > &right) OMG_NOEXCEPT
 Swaps the underlying objects.
 

Related Functions

(Note that these are not member functions.)

template<typename T >
bool operator== (const optional< T > &a, const optional< T > &b)
 Compares two optional values.
 
template<typename T >
bool operator!= (const optional< T > &a, const optional< T > &b)
 Compares two optional values.
 
template<typename T >
bool operator== (const optional< T > &optional_value, const T &value)
 Compares an optional member and a value of the underlying type.
 
template<typename T >
bool operator== (const T &value, const optional< T > &optional_value)
 Compares an optional member and a value of the underlying type.
 
template<typename T >
bool operator!= (const optional< T > &optional_value, const T &value)
 Compares an optional member and a value of the underlying type.
 
template<typename T >
bool operator!= (const T &value, const optional< T > &optional_value)
 Compares an optional member and a value of the underlying type.
 
template<typename T >
std::ostream & operator<< (std::ostream &out, const optional< T > &optional)
 Applies the operator<< to optional.get() or the string "NULL" if !optional.is_set()
 

Detailed Description

template<typename T>
class dds::core::optional< T >

<<value-type>> Represents an object that may not contain a valid value

Template Parameters
TThe type of the actual object this optional<T> wraps

Members of an IDL type marked with the @Optional tag map to this C++ type.

When an optional value has a valid value is_set() returns true and get() returns a reference to the actual object of type T. Otherwise is_set() returns false and get() throws dds::core::PreconditionNotMetError. To assing a value you can use the assignment operator or set().

An optional object has full value-type semantics; copying an optional value copies the underlying object if it exists.

This class is similar to boost::optional.

See Also
Working with IDL types

Constructor & Destructor Documentation

template<typename T>
dds::core::optional< T >::optional ( )
inline

Create an unset optional object.

Postcondition
!is_set()
template<typename T>
dds::core::optional< T >::optional ( const T &  value)
inline

Create an optional object with a copy of a value.

Postcondition
is_set() && get() == value
template<typename T>
dds::core::optional< T >::optional ( T &&  value)
inline

<<C++11>> Create an optional object moving a value

Postcondition
is_set()
template<typename T>
dds::core::optional< T >::optional ( bool  condition,
const T &  value 
)
inline

Create an optional member conditionally set or unset.

Parameters
conditionIf true creates an optional member with value otherwise it creates an unset optional member
valueThe value to set if condition is true
Postcondition
is_set() == condition
template<typename T>
dds::core::optional< T >::optional ( bool  condition,
T &&  value 
)
inline

<<C++11>> Creates an optional member conditionally set or unset by moving a value.

Moves the value rather than copying it.

See Also
optional(bool, const T&)
template<typename T>
dds::core::optional< T >::~optional ( )
inline

Destroys the underlying object if it exists.

Member Function Documentation

template<typename T>
void dds::core::optional< T >::set ( const T &  value)
inline

Assigns a copy of an object.

template<typename T>
void dds::core::optional< T >::set ( T &&  value)
inline

<<C++11>> Assigns an object by moving it

template<typename T>
bool dds::core::optional< T >::is_set ( ) const
inline

Checks if this optional instance contains a valid object.

If this->is_set(), then this->get() is a valid object

template<typename T>
dds::core::optional< T >::operator bool ( ) const
inline

Returns is_set()

template<typename T>
void dds::core::optional< T >::reset ( )
inline

Destroys the underlying object and leaves this optional with an invalid (unset) value.

Postcondition
!is_set()
template<typename T>
const T& dds::core::optional< T >::get ( ) const
inline

Retrieves the underlying object if it exists.

Exceptions
dds::core::PreconditionNotMetErrorif !is_set().
template<typename T>
T& dds::core::optional< T >::get ( )
inline

Retrieves the underlying object if it exists.

Exceptions
dds::core::PreconditionNotMetErrorif !is_set().
template<typename T>
const T* dds::core::optional< T >::get_ptr ( ) const
inline

Returns &get() if the object is initialized or NULL otherwise.

template<typename T>
T* dds::core::optional< T >::get_ptr ( )
inline

Returns &get() if the object is initialized or NULL otherwise.

template<typename T>
optional<T>& dds::core::optional< T >::operator= ( const optional< T > &  other)
inline

Assignment operator.

Copies other.get() if it exists.

template<typename T>
optional<T>& dds::core::optional< T >::operator= ( optional< T > &&  other)
inline

<<C++11>> Move-assignment operator

Moves other.get() if it exists.

template<typename T>
optional<T>& dds::core::optional< T >::operator= ( const T &  value)
inline

Assign a (valid) value.

Parameters
valueThe value to assign to this optional member
Postcondition
get() == value
template<typename T>
optional<T>& dds::core::optional< T >::operator= ( T &&  value)
inline

Assign a (valid) value by moving an object.

Parameters
valueThe value to move into this optional member
Postcondition
is_set()

Friends And Related Function Documentation

template<typename T>
void swap ( optional< T > &  left,
optional< T > &  right 
)
friend

Swaps the underlying objects.

This operation is always O(1).

template<typename T >
bool operator== ( const optional< T > &  a,
const optional< T > &  b 
)
related

Compares two optional values.

Returns
true if both are unset or both are set and a.get() == b.get().
template<typename T >
bool operator!= ( const optional< T > &  a,
const optional< T > &  b 
)
related

Compares two optional values.

Returns
false if both are unset or both are set and a.get() == b.get().
template<typename T >
bool operator== ( const optional< T > &  optional_value,
const T &  value 
)
related

Compares an optional member and a value of the underlying type.

Returns
Return true if optional_value is set and optional_value.get() == value
template<typename T >
bool operator== ( const T &  value,
const optional< T > &  optional_value 
)
related

Compares an optional member and a value of the underlying type.

Returns
Return true if optional_value is set and optional_value.get() == value
template<typename T >
bool operator!= ( const optional< T > &  optional_value,
const T &  value 
)
related

Compares an optional member and a value of the underlying type.

Returns
Return false if optional_value is set and optional_value.get() == value
template<typename T >
bool operator!= ( const T &  value,
const optional< T > &  optional_value 
)
related

Compares an optional member and a value of the underlying type.

Returns
Return false if optional_value is set and optional_value.get() == value
template<typename T >
std::ostream & operator<< ( std::ostream &  out,
const optional< T > &  optional 
)
related

Applies the operator<< to optional.get() or the string "NULL" if !optional.is_set()


RTI Connext Modern C++ API Version 5.3.1 Copyright © Mon Feb 19 2018 Real-Time Innovations, Inc