ara::core::Optional
It’s a wrapper for other types and a flag that indicates if the value is initialized or not.
struct ConfigurationParameters {
std::string mandatoryParameter_1;
int32_t mandatoryParameter_2;
ara::core::Optional<std::string> optionalParameter_1;
ara::core::Optional<int32_t> optionalParameter_2;
};
...
void processConfig(ConfigurationParameters& cfg) {
// Process mandatory parameters as usual
auto param1Value = cfg.mandatoryParameter_1;
// use param1Value
// for optional parameters first check their existence
if(cfg.optionalParameter_1.has_value()) {
// use optional parameter
auto optionalParam = cfg.optionalParameter_1.value();
}
}
- Parameters
Uthe type of element in the container
class ara::core::Optional
A container with at most one element The class template ara::core::Optional manages optional values, i.e. values that may or may not be present. The existence can be evaluated during both compile-time and runtime.
Members
public inline constexpr Optional() noexcept
Default constructor.
public inline constexpr Optional(nullopt_t) noexcept
Construct empty optional from nullopt.
public constexpr Optional(Optional const & v) = default
Copy construct from another Optional.
public constexpr Optional(Optional && v) = default
Move value from another Optional.
public template<>
inline constexpr explicit Optional(in_place_t, Args &&... args) noexcept
Internal In place constructor.
- Parameters
Argsarguments forwarded for inplace construction
public template<>
inline constexpr explicit Optional(in_place_t, std::initializer_list< T > il, Args &&... args) noexcept
Internal In place constructor.
- Parameters
TIn place constructor typeArgsarguments forwarded for inplace construction
public inline Optional(U const & v) noexcept
Construct a new Optional object using provided value.
- Parameters
vinitial value
public inline Optional(U && v) noexcept
Construct a new Optional object by moving the value.
- Parameters
vinitial value
public ~Optional() = default
Destroy the Optional object.
public Optional&operator=(const Optional & rhs) = default
Assignment operator. Please refer to C++17 documentation for more details.
-
Parameters
rhssource Optional object from where value is assigned
-
Returns Optional& reference to this object
public Optional&operator=(Optional && rhs) = default
Assignment operator. Please refer to C++17 documentation for more details.
-
Parameters
rhssource Optional object from where value is assigned
-
Returns Optional& reference to this object
public inline Optional&operator=(nullopt_t) noexcept
Remove any value contained in this Optional object. If no value is contained, then don't have any effect.
- Returns Optional& reference to this object
public template<>
inline U & emplace(Args &&... args) noexcept
Create the value in place and set it as the value for this Optional object. Please refer to C++17 documentation for more details.
-
Parameters
argsarguments which are used to construct the value in place
-
Returns U& reference to the new value
public inline constexpr const U * operator->() const
Return a pointer to the contained value Please refer to C++17 documentation for more details.
- Returns constexpr const U* Pointer to the contained value
public inline constexpr U * operator->()
Return a pointer to the contained value Please refer to C++17 documentation for more details.
- Returns constexpr const U* Pointer to the contained value
public inline constexpr U & operator*()
Return a reference to the contained value Please refer to C++17 documentation for more details.
- Returns constexpr const U* Reference to the contained value
public inline constexpr const U & operator*() const
Return a reference to the contained value Please refer to C++17 documentation for more details.
- Returns constexpr const U* Reference to the contained value
public inline constexpr bool has_value() const noexcept
Check whether the object contain any value.
-
Returns true if the object contain value
-
Returns false if the object doesn't contain value.
public inline constexpr explicit operator bool() const noexcept
Check whether the object contain any value.
-
Returns true if the object contain value
-
Returns false if the object doesn't contain value.
public inline U & value()
Return a reference to the value, if this Optional contain a value. Otherwise throw bad_optional_access exception.
- Returns U&
public inline const U & value() const
Return a reference to the value, if this Optional contain a value. Otherwise throw bad_optional_access exception.
- Returns U&
public template<>
inline constexpr U value_or(T && t) const
Return the value if this Optional contain a value, otherwise return the provided value of 't'.
-
Parameters
Ttype of t
-
Parameters
tdefault value to return if the Optional does not contain any value
-
Returns constexpr U
public template<>
inline constexpr U value_or(T && t)
Return the value if this Optional contain a value, otherwise return the provided value of 't'.
-
Parameters
Ttype of t
-
Parameters
tdefault value to return if the Optional does not contain any value
-
Returns constexpr U
public inline void reset() noexcept
Discard any value contained in this Optional object. Please refer to C++17 documentation for more details.
typedef value_type
the type of element in the container