Kea  1.9.9-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 ()
 
bool empty () const
 Checks if the encapsulated value is empty. More...
 
template<>
bool empty () const
 
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...
 

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

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

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.

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.

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

It calls default string object constructor.

Definition at line 152 of file optional.h.

Member Function Documentation

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 138 of file optional.h.

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

template<>
bool isc::util::Optional< std::string >::empty ( ) const
inline
Returns
true if the value is empty, false otherwise.

Definition at line 160 of file optional.h.

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.

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.

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.

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.

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 128 of file optional.h.

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

Member Data Documentation

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

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