4.6.0
Freundlich's C++ toolkit
|
A class that makes values optional.
The goal of an optional type is to make it explicit that a value might be missing. This includes operations that might return no result, for example looking for an element in a container, performing a conversion that can fail, a tree that might have no parent, and so on. Values that may be nothing are common in C++ but are not handled consistently. This includes special values like std::string::npos
, nullptr
or end iterators. Modelling optionals explicitly has several advantages. Consider the following example:
First of all, the example makes it clear that the function can deal with an unsigned int
or nothing at all. However, using optionals in this way makes them no safer than using pointers that may be null. In order to fix this, we must avoid calling get_unsafe
manually. The most basic operation that does this for us is called maybe
. The user provides two functions which act on the two different cases.
Notice that by using maybe
, the code checking if the optional has a value and the code dereferencing the optional are both gone.
The key differences to std::optional
include:
fcppt::optional::object
does not have operator->
, operator*
nor operator bool
, so that it does not mimic pointers. fcppt::optional::object<T>
does not have an implicit constructor, nor assignment operator from T
. Instead of implementing optional references directly, fcppt offers fcppt::optional::reference which is a typedef to fcppt::optional::object<fcppt::reference<T>>
. The reason behind this is that code that acts on optionals might make use of references itself. For example it is not uncommon to pass the object held by an optional to functions like fcppt::optional::maybe_void by (const) reference, which can be used to change the object or to pass its address along.
On the other hand, if an optional holds a reference it does not hold the actual object, so changing the object behind the reference or passing the reference along has different semantics.
Using fcppt::reference for optional references clearly distinguishes these two cases.
fcppt::optional::maybe is the most basic function that operates on optionals.
fcppt::optional::map lifts a function to a function on optionals:
The more general fcppt::optional::bind returns the function's result directly:
Another commonly used function is fcppt::optional::from:
Header file | Description |
---|---|
object_fwd.hpp | Contains fcppt::optional::object optional's declaration. |
object_decl.hpp | Contains fcppt::optional::object optional's definition. |
object_impl.hpp | Contains the definition of fcppt::optional::object optional's member functions. |
object.hpp | Includes object_impl.hpp and comparison.hpp . |
alternative.hpp | Contains fcppt::optional::alternative . |
apply.hpp | Contains fcppt::optional::apply . |
assign.hpp | Contains fcppt::optional::assign . |
bind.hpp | Contains fcppt::optional::bind . |
cat.hpp | Contains fcppt::optional::cat . |
combine.hpp | Contains fcppt::optional::combine . |
comparison.hpp | Contains operator== , operator!= and operator< . |
copy_value.hpp | Contains fcppt::optional::copy_value . |
deref.hpp | Contains fcppt::optional::deref . |
filter.hpp | Contains fcppt::optional::filter . |
from.hpp | Contains fcppt::optional::from . |
from_pointer.hpp | Contains fcppt::optional::from_pointer . |
join.hpp | Contains fcppt::optional::join . |
make.hpp | Contains fcppt::optional::make . |
map.hpp | Contains fcppt::optional::map . |
maybe.hpp | Contains fcppt::optional::maybe . |
maybe_multi.hpp | Contains fcppt::optional::maybe_multi . |
maybe_void.hpp | Contains fcppt::optional::maybe_void . |
maybe_void_multi.hpp | Contains fcppt::optional::maybe_void_multi . |
output.hpp | Contains operator<< . |
reference_fwd.hpp | Contains the fcppt::optional::reference typedef. |
reference.hpp | Includes reference_fwd.hpp and object_impl.hpp . |
sequence.hpp | Contains fcppt::optional::sequence . |
to_exception.hpp | Contains fcppt::optional::to_exception . |
Classes | |
struct | fcppt::optional::is_object< T > |
Checks if a given type is an fcppt::optional::object. More... | |
struct | fcppt::optional::nothing |
A polymorphic empty optional. More... | |
class | fcppt::optional::object< T > |
A class that makes values optional. More... | |
Typedefs | |
template<fcppt::optional::object_concept Optional> | |
using | fcppt::optional::move_type = fcppt::move_if_rvalue_type<Optional, fcppt::optional::reference_type<Optional>> |
The moved inner type of an optional. | |
template<typename T > | |
using | fcppt::optional::reference = fcppt::optional::object<fcppt::reference<T>> |
Optional of a reference. | |
template<fcppt::optional::object_concept Optional> | |
using | fcppt::optional::reference_type = decltype(std::declval<Optional>().get_unsafe()) |
The reference type of an optional. | |
template<fcppt::optional::object_concept Optional> | |
using | fcppt::optional::value_type = fcppt::type_traits::value_type<std::remove_cvref_t<Optional>> |
The value type of an optional. | |
Functions | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move<> Function> requires std::is_same_v<std::remove_cvref_t<Optional>, std::invoke_result_t<Function>> | |
std::remove_cvref_t< Optional > | fcppt::optional::alternative (Optional &&_optional1, Function const &_optional2) |
Returns the first optional if it is not nothing. | |
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optionals >... > Function> | |
fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optionals >... > > | fcppt::optional::apply (Function const &_function, Optionals &&..._optionals) |
Applies a function to multiple optionals if each of them contains a value. | |
template<typename Element , typename Arg > requires std::is_same_v<Element, std::remove_cv_t<Arg>> | |
Element & | fcppt::optional::assign (fcppt::optional::object< Element > &_optional, Arg &&_arg) |
Assigns an optional and returns a reference to the optional's contents. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional > > Function> requires fcppt::optional::is_object_v< std::invoke_result_t<Function, fcppt::optional::move_type<Optional>>> | |
std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > | fcppt::optional::bind (Optional &&_source, Function const &_function) |
Converts an optional of one type to another. | |
template<typename TargetContainer , typename Source > | |
TargetContainer | fcppt::optional::cat (Source &&_source) |
Removes empty optionals from a range. | |
template<fcppt::optional::object_concept Optional1, fcppt::optional::object_concept Optional2, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional1 >, fcppt::optional::move_type< Optional2 > > Function> requires std:: is_same_v<fcppt::optional::value_type<Optional1>, fcppt::optional::value_type<Optional2>> && std::is_same_v< fcppt::optional::value_type<Optional1>, std::invoke_result_t< Function, fcppt::optional::move_type<Optional1>, fcppt::optional::move_type<Optional2>>> | |
fcppt::optional::object< fcppt::optional::value_type< Optional1 > > | fcppt::optional::combine (Optional1 &&_optional1, Optional2 &&_optional2, Function const &_function) |
Combines two optionals. | |
template<typename T > | |
bool | fcppt::optional::operator== (fcppt::optional::object< T > const &_a, fcppt::optional::object< T > const &_b) |
Compares two optionals for equality. | |
template<typename T > | |
bool | fcppt::optional::operator!= (fcppt::optional::object< T > const &_a, fcppt::optional::object< T > const &_b) |
Compares two optionals for inequality. | |
template<typename Type > | |
fcppt::optional::object< std::remove_cv_t< Type > > | fcppt::optional::copy_value (fcppt::optional::reference< Type > const &_opt) |
Copies the value of an optional reference. | |
template<typename Element > | |
fcppt::optional::object< fcppt::reference< std::remove_reference_t< decltype(*std::declval< Element >())> > | fcppt::optional::deref (fcppt::optional::object< Element > const &_optional) |
Dereferences the contents of an optional. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::value_type< Optional > > Function> requires std::is_same_v<bool, std::invoke_result_t<Function, fcppt::optional::value_type<Optional>>> | |
std::remove_cvref_t< Optional > | fcppt::optional::filter (Optional &&_source, Function const &_function) |
Filters an optional. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move Default> requires std::is_same_v<std::invoke_result_t<Default>, fcppt::optional::value_type<Optional>> | |
std::invoke_result_t< Default > | fcppt::optional::from (Optional &&_optional, Default const &_default) |
Returns the value contained in an optional or a default value. | |
template<typename T > | |
fcppt::optional::reference< T > | fcppt::optional::from_pointer (T *const _pointer) |
Creates an optional reference from a pointer. | |
template<typename Element , fcppt::concepts::invocable_move<> Function> requires std::is_same_v<std::invoke_result_t<Function>, Element> | |
Element & | fcppt::optional::get_or_assign (fcppt::optional::object< Element > &_optional, Function const &_function) |
Assigns an optional if it is empty. Returns a reference to the optional's contents. | |
template<fcppt::optional::object_concept Optional> requires fcppt::optional::is_object_v<fcppt::optional::value_type<Optional>> | |
fcppt::optional::value_type< Optional > | fcppt::optional::join (Optional &&_source) |
Removes one layer of optionals. | |
template<typename Type > | |
fcppt::optional::object< std::remove_cvref_t< Type > > | fcppt::optional::make (Type &&_value) |
Wraps a value into an optional. | |
template<fcppt::concepts::invocable_move Function> | |
fcppt::optional::object< std::invoke_result_t< Function > > | fcppt::optional::make_if (bool const _is_set, Function const &_function) |
Creates an optional depending on a boolean. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional > > Function> | |
fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > > | fcppt::optional::map (Optional &&_source, Function const &_function) |
Maps over an optional using a function. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optional > > Transform> requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optional>>> | |
std::invoke_result_t< Default > | fcppt::optional::maybe (Optional &&_optional, Default const &_default, Transform const &_transform) |
Transforms an optional value or returns a default value. | |
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform> requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optionals>...>> | |
std::invoke_result_t< Default > | fcppt::optional::maybe_multi (Default const _default, Transform const _transform, Optionals &&..._optionals) |
Transforms multiple optional values or returns a default value. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable< fcppt::optional::move_type< Optional > > Transform> | |
void | fcppt::optional::maybe_void (Optional &&_optional, Transform const &_transform) |
Transforms an optional value or does nothing. | |
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform> | |
void | fcppt::optional::maybe_void_multi (Transform const _transform, Optionals &&..._optionals) |
Transforms optional values or does nothing. | |
template<typename Type , typename Ch , typename Traits > | |
std::basic_ostream< Ch, Traits > & | fcppt::optional::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::optional::object< Type > const &_opt_value) |
Outputs an optional to a basic_ostream. | |
template<typename ResultContainer , typename Source > | |
fcppt::optional::object< ResultContainer > | fcppt::optional::sequence (Source &&_source) |
Turns a container of optionals into an optional container. | |
template<typename Container , fcppt::optional::object_concept Optional> requires std::is_same_v<fcppt::type_traits::value_type<Container>, fcppt::optional::value_type<Optional>> | |
Container | fcppt::optional::to_container (Optional &&_source) |
Puts the value of an optional into a container or returns an empty container. | |
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move MakeException> | |
fcppt::optional::move_type< Optional > | fcppt::optional::to_exception (Optional &&_optional, MakeException const &_make_exception) |
Returns the value contained in an optional or throws an exception. | |
template<typename T > | |
T * | fcppt::optional::to_pointer (fcppt::optional::reference< T > const _optional) |
Converts an optional reference to a pointer. | |
Variables | |
template<typename T > | |
constexpr bool | fcppt::optional::is_object_v = fcppt::optional::is_object<T>::value |
Checks if a given type is an fcppt::optional::object. | |
using fcppt::optional::move_type = fcppt::move_if_rvalue_type<Optional, fcppt::optional::reference_type<Optional>> |
The moved inner type of an optional.
using fcppt::optional::reference = fcppt::optional::object<fcppt::reference<T>> |
Optional of a reference.
using fcppt::optional::reference_type = decltype(std::declval<Optional>().get_unsafe()) |
The reference type of an optional.
using fcppt::optional::value_type = fcppt::type_traits::value_type<std::remove_cvref_t<Optional>> |
The value type of an optional.
|
nodiscard |
Returns the first optional if it is not nothing.
If _optional1 is not nothing, the result is _optional1, otherwise the result of _optional2 is returned.
|
inlinenodiscard |
Applies a function to multiple optionals if each of them contains a value.
If _optionals, o_1,...,o_n
are set to x_1,...,x_n
, then optional::object{_function(x_1,...,x_n)}
is returned. Otherwise, the empty optional is returned.
|
inlinenodiscard |
Assigns an optional and returns a reference to the optional's contents.
Assigns _arg to _optional and returns a reference to _arg.
|
inlinenodiscard |
Converts an optional of one type to another.
If _source is set to x
, then _function(x)
is returned. Otherwise, the empty optional is returned.
|
nodiscard |
Removes empty optionals from a range.
For every element e in _source, if e is set to x
, then x
is inserted into the target container.
Source | Must be a range of optionals. |
TargetContainer | Must be a container whose value type matches that of the optionals from Source |
TODO(concepts)
|
nodiscard |
Combines two optionals.
If _optional1 is set to x1 and _optional2 is set to x2, then the result is Optional(_function(x1, x2))
. Otherwise, if at least one optional is set, that optional is returned.
|
nodiscard |
Copies the value of an optional reference.
|
inlinenodiscard |
Dereferences the contents of an optional.
If the optional is set to x
, make_(c)ref(*x)
is returned. For example, this is useful as a shortcut for optionals containing iterators or unique pointers.
TODO(concepts)
|
nodiscard |
Filters an optional.
If _source is set to x
and _function(x)
returns true, _source is returned. Otherwise, the empty optional is returned.
|
nodiscard |
Returns the value contained in an optional or a default value.
If _optional is set to x, then x is returned. Otherwise, the result of _default is returned.
|
nodiscard |
Creates an optional reference from a pointer.
Creates an optional reference from _pointer. If _pointer is the null pointer, the result will be empty. Otherwise, the result will contain a reference to *_pointer
.
|
inlinenodiscard |
Assigns an optional if it is empty. Returns a reference to the optional's contents.
Assigns the result of _function to _optional if _optional is empty and returns a reference to the contents of _optional.
|
inlinenodiscard |
Removes one layer of optionals.
If _source is set to optional{x}
, then optional{x}
is returned. Otherwise, the empty optional is returned.
|
inlinenodiscard |
Wraps a value into an optional.
|
inlinenodiscard |
Creates an optional depending on a boolean.
If _is_set is true, then _function()
is returned as an optional. Otherwise, the empty optional is returned.
|
inlinenodiscard |
Maps over an optional using a function.
If _source is set to x
, then fcppt::optional::make(_function(x))
is returned. Otherwise, the empty optional is returned.
|
nodiscard |
Transforms an optional value or returns a default value.
For the lack of a better name, this function is called maybe like in Haskell. If _optional is set to x, then _transform(x)
is returned. Otherwise, the result of _default is returned.
|
nodiscard |
Transforms multiple optional values or returns a default value.
A multi version of fcppt::optional::maybe. If _optionals are set to x_1, ..., x_n
, then _transform(x_1, ..., x_n)
is returned. Otherwise, the result of _default is returned.
|
inline |
Transforms an optional value or does nothing.
If _optional is set to x, then _transform(x)
is called.
|
inline |
Transforms optional values or does nothing.
A multi version of fcppt::optional::maybe_void. If _optionals are set to x_1, ..., x_n
, then _transform(x_1, ..., x_n)
is called.
|
nodiscard |
Compares two optionals for inequality.
Compares _a and _b for inequality. Equal to !(_a == _b)
. This function requires T to be equality comparable.
TODO(concepts)
std::basic_ostream< Ch, Traits > & fcppt::optional::operator<< | ( | std::basic_ostream< Ch, Traits > & | _stream, |
fcppt::optional::object< Type > const & | _opt_value ) |
Outputs an optional to a basic_ostream.
TODO(concepts)
|
nodiscard |
Compares two optionals for equality.
Compares _a and _b for equality. Two optionals are equal if they are either both empty or if they are both not empty and their elements compare equal. This function requires T to be equality comparable.
TODO(concepts)
fcppt::optional::object< ResultContainer > fcppt::optional::sequence | ( | Source && | _source | ) |
Turns a container of optionals into an optional container.
Let _source be a container [o_1,...o_n]
of type fcppt::optional::object<T>
. If there is an i
such that o_i
is nothing, then nothing is returned. Otherwise, all optionals have a value, [v_1,...,v_n]
, and fcppt::optional::object<ResultContainer>{v_1,...,v_n}
is returned.
ResultContainer | Must be a container of type T |
Source | Must be an optional type |
TODO(concepts)
|
nodiscard |
Puts the value of an optional into a container or returns an empty container.
If _source holds x
, then Container{x}
is returned. Otherwise the empty container is returned.
Container and Optional must have the same value types.
TODO(concepts)
|
inlinenodiscard |
Returns the value contained in an optional or throws an exception.
If _optional is set to x, then x is returned. Otherwise, the result of _make_exception is thrown as an exception.
|
inlinenodiscard |
Converts an optional reference to a pointer.
If _optional is empty, returns nullptr
. Otherwise, returns the address of the referenced object of _optional.
|
inlineconstexpr |
Checks if a given type is an fcppt::optional::object.