Kea 2.7.5
isc::util::Optional< T > Class Template Reference

A template representing an optional value. More...

#include <optional.h>

+ Inheritance diagram for isc::util::Optional< T >:

Public Types

typedef T ValueType
 Type of the encapsulated value.
 

Public Member Functions

 Optional ()
 Default constructor.
 
 Optional ()
 Specialization of the default Optional constructor for strings.
 
template<typename A >
 Optional (A value, const bool unspecified=false)
 Constructor.
 
bool empty () const
 Checks if the encapsulated value is empty.
 
bool empty () const
 Specialization of the Optional::empty method for strings.
 
get () const
 Retrieves the encapsulated value.
 
 operator T () const
 Type cast operator.
 
bool operator!= (const T &other) const
 Inequality operator.
 
template<typename A >
Optional< T > & operator= (A other)
 Assigns a new value value and marks it "specified".
 
bool operator== (const T &other) const
 Equality operator.
 
bool unspecified () const
 Checks if the value has been specified or unspecified.
 
void unspecified (bool unspecified)
 Modifies the flag that indicates whether the value is specified or unspecified.
 
valueOr (T const &or_value) const
 Retrieves the encapsulated value if specified, or the given value otherwise.
 

Protected Attributes

default_
 Encapsulated value.
 
bool unspecified_
 Flag which indicates if the value is specified.
 

Detailed Description

template<typename T>
class isc::util::Optional< T >

A template representing an optional value.

This template class encapsulates an optional value. The default implementation encapsulates numeric values, but additional specializations are defined as necessary to support other types od data.

This class includes a boolean flag which indicates if the encapsulated value is specified or unspecified. For example, a configuration parser for the DHCP server may use this class to represent a value of the configuration parameter which may appear in the configuration file, but is not mandatory. The value of the Optional may be initialized to "unspecified" initially. When the configuration parser finds that the particular parameter exists in the configuration file, the default value can be overridden and the value may be marked as "specified". If the parameter is not found, the value remains "unspecified" and the appropriate actions may be taken, e.g. the default value may be used.

Template Parameters
Typeof the encapsulated value.

Definition at line 36 of file optional.h.

Member Typedef Documentation

◆ ValueType

template<typename T >
typedef T isc::util::Optional< T >::ValueType

Type of the encapsulated value.

Definition at line 40 of file optional.h.

Constructor & Destructor Documentation

◆ Optional() [1/3]

template<typename T >
isc::util::Optional< T >::Optional ( )
inline

Default constructor.

Sets the encapsulated value to 0 and marks it as "unspecified".

The caller must ensure that the constructor of the class T creates a valid object when invoked with 0 as an argument. For example, a std::string(0) compiles but will crash at runtime as 0 is not a valid pointer for the std::string(const char*) constructor. Therefore, the specialization of the Optional template for std::string is provided below. It uses std::string default constructor.

For any other type used with this template which doesn't come with an appropriate constructor, the caller must create a template specialization similar to the one provided for std::string below.

Definition at line 94 of file optional.h.

◆ Optional() [2/3]

template<typename T >
template<typename A >
isc::util::Optional< T >::Optional ( A value,
const bool unspecified = false )
inline

Constructor.

Sets an explicit value and marks it as "specified".

Template Parameters
AType of the value to be assigned. Typically this is T, but may also be a type that can be cast to T.
Parameters
valuevalue to be assigned.
unspecifiedinitial state. Default is "unspecified".

Definition at line 107 of file optional.h.

◆ Optional() [3/3]

isc::util::Optional< std::string >::Optional ( )
inline

Specialization of the default Optional constructor for strings.

It calls default string object constructor.

Definition at line 167 of file optional.h.

Member Function Documentation

◆ empty() [1/2]

template<typename T >
bool isc::util::Optional< T >::empty ( ) const
inline

Checks if the encapsulated value is empty.

This method can be overloaded in the template specializations that are dedicated to strings, vectors etc.

Exceptions
isc::InvalidOperation.

Definition at line 153 of file optional.h.

References isc_throw.

Referenced by isc::dhcp::Network::clientSupported().

◆ empty() [2/2]

bool isc::util::Optional< std::string >::empty ( ) const
inline

Specialization of the Optional::empty method for strings.

Returns
true if the value is empty, false otherwise.

Definition at line 175 of file optional.h.

◆ get()

◆ operator T()

template<typename T >
isc::util::Optional< T >::operator T ( ) const
inline

Type cast operator.

This operator converts the optional value to the actual value being encapsulated.

Returns
Encapsulated value.

Definition at line 60 of file optional.h.

◆ operator!=()

template<typename T >
bool isc::util::Optional< T >::operator!= ( const T & other) const
inline

Inequality operator.

Parameters
othervalue to be compared.

Definition at line 74 of file optional.h.

References isc::util::Optional< T >::default_.

◆ operator=()

template<typename T >
template<typename A >
Optional< T > & isc::util::Optional< T >::operator= ( A other)
inline

Assigns a new value value and marks it "specified".

Template Parameters
AType of the value to be assigned. Typically this is T, but may also be a type that can be cast to T.
Parameters
othernew actual value.

Definition at line 48 of file optional.h.

References isc::util::Optional< T >::default_, and isc::util::Optional< T >::unspecified_.

◆ operator==()

template<typename T >
bool isc::util::Optional< T >::operator== ( const T & other) const
inline

Equality operator.

Parameters
othervalue to be compared.

Definition at line 67 of file optional.h.

References isc::util::Optional< T >::default_.

◆ unspecified() [1/2]

template<typename T >
bool isc::util::Optional< T >::unspecified ( ) const
inline

Checks if the value has been specified or unspecified.

Returns
true if the value hasn't been specified, false otherwise.

Definition at line 143 of file optional.h.

References isc::util::Optional< T >::unspecified_.

Referenced by isc::util::Optional< T >::unspecified().

◆ unspecified() [2/2]

template<typename T >
void isc::util::Optional< T >::unspecified ( bool unspecified)
inline

Modifies the flag that indicates whether the value is specified or unspecified.

Parameters
unspecifiednew value of the flag. If it is true, the value is marked as unspecified, otherwise it is marked as specified.

Definition at line 136 of file optional.h.

References isc::util::Optional< T >::unspecified(), and isc::util::Optional< T >::unspecified_.

Referenced by isc::db::PsqlBindArray::addOptional(), isc::db::PsqlBindArray::addOptional(), isc::db::PsqlBindArray::addOptionalInet4(), isc::db::PsqlBindArray::addOptionalInet6(), isc::db::MySqlBinding::condCreateBool(), isc::db::MySqlBinding::condCreateFloat(), isc::db::MySqlBinding::condCreateInteger(), isc::dhcp::ClientClassDef::toElement(), isc::dhcp::Network::toElement(), isc::process::ConfigBase::toElement(), and isc::process::ConfigControlInfo::toElement().

+ Here is the call graph for this function:

◆ valueOr()

template<typename T >
T isc::util::Optional< T >::valueOr ( T const & or_value) const
inline

Retrieves the encapsulated value if specified, or the given value otherwise.

Parameters
or_valuethe value it defaults to, if unspecified
Returns
the encapsulated value or the default value

Definition at line 124 of file optional.h.

References isc::util::Optional< T >::default_, and isc::util::Optional< T >::unspecified_.

Member Data Documentation

◆ default_

◆ unspecified_

template<typename T >
bool isc::util::Optional< T >::unspecified_
protected

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