Poco

template < typename C >

class Nullable

Library: Foundation
Package: Core
Header: Poco/Nullable.h

Description

Nullable is a simple wrapper class for value types that allows objects or native type variables to have "null" value.

The class is useful for passing parameters to functions when parameters are optional and no default values should be used or when a non-assigned state is needed, such as in e.g. fetching null values from database.

A Nullable can be default constructed. In this case, the Nullable will have a Null value and isNull() will return true. Calling value() (without default value) on a Null object will throw a NullValueException.

A Nullable can also be constructed from a value. It is possible to assign a value to a Nullable, and to reset a Nullable to contain a Null value by calling clear().

For use with Nullable, the value type should support default construction.

Member Summary

Member Functions: assign, clear, isNull, operator !=, operator <, operator =, operator ==, operator >, operator C &, operator const C &, operator const NullType &, swap, value

Types Aliases

Type

using Type = C;

Constructors

Nullable inline

Nullable();

Creates an empty Nullable.

Nullable inline

Nullable(
    const NullType & param301
);

Creates an empty Nullable.

Nullable inline

Nullable(
    const C & value
);

Creates a Nullable with the given value.

Nullable inline

Nullable(
    C && value
);

Creates a Nullable by moving the given value.

Nullable inline

Nullable(
    const Nullable & other
);

Creates a Nullable by copying another one.

Nullable inline

Nullable(
    Nullable && other
) noexcept;

Creates a Nullable by moving another one.

Destructor

~Nullable

~Nullable() = default;

Destroys the Nullable.

Member Functions

assign inline

Nullable & assign(
    const C & value
);

Assigns a value to the Nullable.

assign inline

Nullable & assign(
    C && value
);

Assigns a value to the Nullable.

assign inline

Nullable & assign(
    const Nullable & other
);

Assigns another Nullable.

assign inline

Nullable & assign(
    NullType
);

Sets value to null.

clear inline

void clear();

Clears the Nullable.

isNull inline

bool isNull() const;

Returns true if the Nullable is empty.

operator != inline

bool operator != (
    const C & value
) const;

Compares Nullable with value for non equality

operator != inline

bool operator != (
    const Nullable < C > & other
) const;

Compares two Nullables for non equality

operator != inline

bool operator != (
    const NullType & param303
) const;

Compares with NullData for non equality

operator < inline

bool operator < (
    const Nullable < C > & other
) const;

Compares two Nullable objects. Return true if this object's value is smaler than the other object's value. Null value is smaller than a non-null value.

operator = inline

Nullable & operator = (
    const C & value
);

Assigns a value to the Nullable.

operator = inline

Nullable & operator = (
    C && value
);

Move-assigns a value to the Nullable.

operator = inline

Nullable & operator = (
    const Nullable & other
);

Assigns another Nullable.

operator = inline

Nullable & operator = (
    Nullable && other
) noexcept;

Moves another Nullable.

operator = inline

Nullable & operator = (
    NullType
);

Assigns NullType.

operator == inline

bool operator == (
    const Nullable < C > & other
) const;

Compares two Nullables for equality

operator == inline

bool operator == (
    const C & value
) const;

Compares Nullable with value for equality

operator == inline

bool operator == (
    const NullType & param302
) const;

Compares Nullable with NullData for equality

operator > inline

bool operator > (
    const Nullable < C > & other
) const;

Compares two Nullable objects. Return true if this object's value is greater than the other object's value. A non-null value is greater than a null value.

operator C & inline

explicit operator C & ();

Get reference to the value

operator const C & inline

explicit operator const C & () const;

Get const reference to the value

operator const NullType & inline

operator const NullType & () const;

Get reference to the value

swap inline

void swap(
    Nullable & other
) noexcept;

Swaps this Nullable with other.

value inline

C & value();

Returns the Nullable's value.

Throws a NullValueException if the Nullable is empty.

value inline

const C & value() const;

Returns the Nullable's value.

Throws a NullValueException if the Nullable is empty.

value inline

const C & value(
    const C & deflt
) const;

Returns the Nullable's value, or the given default value if the Nullable is empty.