Kea  2.1.7-git
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. More...
 

Public Member Functions

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

Protected Attributes

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

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

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

Referenced by isc::dhcp::Subnet6ConfigParser::initSubnet(), isc::dhcp::SharedNetwork6Parser::parse(), isc::dhcp::CfgSubnets4::selectSubnet4o6(), and isc::dhcp::Network::setIface().

◆ empty() [2/2]

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

◆ operator=()

template<typename T>
template<typename A >
Optional<T>& isc::util::Optional< T >::operator= ( 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.

◆ 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.

◆ unspecified() [1/2]

◆ unspecified() [2/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.

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

◆ 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.

Referenced by isc::dhcp::CSVLeaseFile6::next().

Member Data Documentation

◆ default_

◆ unspecified_


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