template class Poco::Nullable

Overview

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

#include <Nullable.h>

template <typename C>
class Nullable
{
public:
    // construction

    Nullable();
    Nullable(const NullType&);
    Nullable(const C& value);
    Nullable(const Nullable& other);

    // methods

    Nullable&
    assign(const C& value);

    Nullable&
    assign(const Nullable& other);

    Nullable&
    assign(NullType);

    Nullable&
    operator=(const C& value);

    Nullable&
    operator=(const Nullable& other);

    Nullable&
    operator=(NullType);

    void
    swap(Nullable& other);

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

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

    bool
    operator==(const NullType&) const;

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

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

    bool
    operator!=(const NullType&) const;

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

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

    C&
    value();

    const C&
    value() const;

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

    operator C &();
    operator const C &() const;
    operator NullType &();

    bool
    isNull() const;

    void
    clear();
};

Detailed Documentation

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.

Construction

Nullable()

Creates an empty Nullable.

Nullable(const NullType&)

Creates an empty Nullable.

Nullable(const C& value)

Creates a Nullable with the given value.

Nullable(const Nullable& other)

Creates a Nullable by copying another one.

Methods

Nullable&
assign(const C& value)

Assigns a value to the Nullable.

Nullable&
assign(const Nullable& other)

Assigns another Nullable.

Nullable&
assign(NullType)

Sets value to null.

Nullable&
operator=(const C& value)

Assigns a value to the Nullable.

Nullable&
operator=(const Nullable& other)

Assigns another Nullable.

Nullable&
operator=(NullType)

Assigns another Nullable.

void
swap(Nullable& other)

Swaps this Nullable with other.

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

Compares two Nullables for equality.

bool
operator==(const C& value) const

Compares Nullable with value for equality.

bool
operator==(const NullType&) const

Compares Nullable with NullData for equality.

bool
operator!=(const C& value) const

Compares Nullable with value for non equality.

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

Compares two Nullables for non equality.

bool
operator!=(const NullType&) const

Compares with NullData for non equality.

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.

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.

C&
value()

Returns the Nullable ‘s value.

Throws a NullValueException if the Nullable is empty.

const C&
value() const

Returns the Nullable ‘s value.

Throws a NullValueException if the Nullable is empty.

const C&
value(const C& deflt) const

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

operator C &()

Get reference to the value.

operator const C &() const

Get const reference to the value.

operator NullType &()

Get reference to the value.

bool
isNull() const

Returns true if the Nullable is empty.

void
clear()

Clears the Nullable.