RTI Connext Modern C++ API  Version 6.0.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 OMG_NOEXCEPT
 Checks if this optional instance contains a valid object.
 
bool has_value () const OMG_NOEXCEPT
 Checks if this optional instance contains a valid object.
 
 operator bool () const OMG_NOEXCEPT
 Returns has_value()
 
void reset ()
 Destroys the underlying object and leaves this optional with an invalid (unset) value.
 
const T & value () const
 Retrieves the underlying object if it exists.
 
T & value ()
 Retrieves the underlying object if it exists.
 
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.
 
const T & operator* () const
 
T & operator* ()
 
const T * operator-> () const
 
T * operator-> ()
 
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 operator<< to *optional or to the string "NULL" if !optional.has_value().
 

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 has_value() returns true and operator* returns a reference to the actual object of type T. Otherwise has_value() returns false and operator* throws dds::core::PreconditionNotMetError. To assing a value you can use the assignment operator.

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

This type's API is similar to that of std::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
!has_value()
template<typename T>
dds::core::optional< T >::optional ( const T &  value)
inline

Create an optional object with a copy of a value.

Postcondition
has_value() && *(*this) == value
template<typename T>
dds::core::optional< T >::optional ( T &&  value)
inline

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

Postcondition
has_value()
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
has_value() == 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.

[DEPRECATED] Use operator= instead.

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

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

[DEPRECATED] Use operator= instead.

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

Checks if this optional instance contains a valid object.

[DEPRECATED] Use has_value()

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

Checks if this optional instance contains a valid object.

If my_optional->has_value(), then *my_optional returns a valid object

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

Returns has_value()

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

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

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

Retrieves the underlying object if it exists.

This operation, unlike operator* throws an exception if the underlying object doesn't exist.

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

Retrieves the underlying object if it exists.

This operation, unlike operator* throws an exception if the underlying object doesn't exist.

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

Retrieves the underlying object if it exists.

[DEPRECATED] Use value() or operator*

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

Retrieves the underlying object if it exists.

[DEPRECATED] Use value() or operator*

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

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

[DEPRECATED]

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

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

[DEPRECATED]

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

Get the value, without checking if it exists

Precondition
has_value(), otherwise this operation has undefined behavior. See value().
template<typename T>
T& dds::core::optional< T >::operator* ( )
inline

Get the value, without checking if it exists

Precondition
has_value(), otherwise this operation has undefined behavior. See value().
template<typename T>
const T* dds::core::optional< T >::operator-> ( ) const
inline

Get the value.

Exceptions
dds::core::PreconditionNotMetErrorif !has_value()
template<typename T>
T* dds::core::optional< T >::operator-> ( )
inline

Get the value.

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

Assignment operator.

Copies *other if it exists.

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

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

Moves *other 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
*(*this) == 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
has_value()

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 == *b.
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 == *b.
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 == 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 == 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 == 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 == value
template<typename T >
std::ostream & operator<< ( std::ostream &  out,
const optional< T > &  optional 
)
related

Applies operator<< to *optional or to the string "NULL" if !optional.has_value().


RTI Connext Modern C++ API Version 6.0.1 Copyright © Sat Nov 23 2019 Real-Time Innovations, Inc