#include "Stroika/Foundation/StroikaPreComp.h"
#include <mutex>
#include <optional>
#include <shared_mutex>
#include "Stroika/Foundation/Common/Common.h"
#include "Stroika/Foundation/Common/Compare.h"
#include "Stroika/Foundation/Common/Concepts.h"
#include "Stroika/Foundation/Common/Empty.h"
#include "Stroika/Foundation/Containers/Adapters/Adder.h"
#include "Stroika/Foundation/Debug/AssertExternallySynchronizedMutex.h"
#include "Stroika/Foundation/Execution/NullMutex.h"
#include "Stroika/Foundation/Memory/BlockAllocated.h"
#include "Stroika/Foundation/Memory/Common.h"
#include "Optional.inl"
Go to the source code of this file.
Namespaces | |
namespace | Stroika::Foundation |
Functions | |
template<typename T , convertible_to< T > CONVERTIBLE_TO_T, convertible_to< function< T(T, T)> > OP = plus<T>> | |
void | Stroika::Foundation::Memory::AccumulateIf (optional< T > *lhsOptionalValue, const optional< CONVERTIBLE_TO_T > &rhsOptionalValue, const OP &op=OP{}) |
AccumulateIf () add in the rhs argument value to lhs optional, but if both were missing leave 'lhs' as still missing, and if only RHS available, assign it to the left. | |
template<typename T , typename CONVERTABLE_TO_TYPE > | |
void | Stroika::Foundation::Memory::CopyToIf (CONVERTABLE_TO_TYPE *to, const optional< T > ©FromIfHasValue) |
template<Private_::INullCoalescable OT> | |
const OT & | Stroika::Foundation::Memory::NullCoalesce (const OT &l, const OT &r) |
return one of l, or r, with first preference for which is engaged, and second preference for left-to-right. | |
template<typename T > | |
constexpr const T & | Stroika::Foundation::Memory::ValueOf (const optional< T > &t) |
Same as *t, but Requires that 't' is engaged. | |
template<typename OUT_T , Common::explicitly_convertible_to< OUT_T > IN_T> | |
optional< OUT_T > | Stroika::Foundation::Memory::OptionallyCopy (const optional< IN_T > &in) |
if you can copy an IN_T to an OUT_T, you should be able to copy an optional<IN_T> to an optional<OUT_T> | |
template<typename T , class F > | |
constexpr auto | Stroika::Foundation::Memory::And_Then (const optional< T > &o, F &&f) |
template<typename T , class F > | |
constexpr auto | Stroika::Foundation::Memory::Or_Else (const optional< T > &o, F &&f) |
template<typename T , class F > | |
constexpr auto | Stroika::Foundation::Memory::Transform (const optional< T > &o, F &&f) |
template<typename RHS_CONVERTIBLE_TO_OPTIONAL_OF_T , constructible_from< RHS_CONVERTIBLE_TO_OPTIONAL_OF_T > T = RHS_CONVERTIBLE_TO_OPTIONAL_OF_T> | |
constexpr optional< T > | Stroika::Foundation::Memory::OptionalFromNullable (const RHS_CONVERTIBLE_TO_OPTIONAL_OF_T *from) |
template<typename T > | |
optional< T > | Stroika::Foundation::Memory::operator+ (const optional< T > &lhs, const optional< T > &rhs) |
template<typename T > | |
optional< T > | Stroika::Foundation::Memory::operator- (const optional< T > &lhs, const optional< T > &rhs) |
template<typename T > | |
optional< T > | Stroika::Foundation::Memory::operator* (const optional< T > &lhs, const optional< T > &rhs) |
template<typename T > | |
optional< T > | Stroika::Foundation::Memory::operator/ (const optional< T > &lhs, const optional< T > &rhs) |
TODO:
NOTE TO SUGGEST TO C++ standards - Things I miss most about my Optional versus std::optional > Value () - what they call value_or - should take T{} as default argument. About 25% of teh time that's what I want, and its much more clear/terse. > Accumulate method, and operator +=, operator-= etc overloads calling Accumulate(). Much simpler and more elegant code with those methods.
Definition in file Optional.h.
void Stroika::Foundation::Memory::AccumulateIf | ( | optional< T > * | lhsOptionalValue, |
const optional< CONVERTIBLE_TO_T > & | rhsOptionalValue, | ||
const OP & | op = OP{} |
||
) |
AccumulateIf () add in the rhs argument value to lhs optional, but if both were missing leave 'lhs' as still missing, and if only RHS available, assign it to the left.
Notes:
Definition at line 28 of file Optional.inl.
void Stroika::Foundation::Memory::CopyToIf | ( | CONVERTABLE_TO_TYPE * | to, |
const optional< T > & | copyFromIfHasValue | ||
) |
Assign (overwriting) the value held by this optional (first argument) if one is present with destination (second) argument if engaged. Assigns from right to left.
The point of this to to facilitate a common idiom, where you want to maintain an existing value unless you get an update. This function is ANALAGOUS to if (o.has_value()) { destArgVal = *o; }
but can be done in a single line.
Definition at line 110 of file Optional.inl.
const OT & Stroika::Foundation::Memory::NullCoalesce | ( | const OT & | l, |
const OT & | r | ||
) |
return one of l, or r, with first preference for which is engaged, and second preference for left-to-right.
So Equivalent to (depending on overload) static_cast<bool> (l)? l : r; or static_cast<bool> (l)? *l : r;
This is similar to/inspired by C# ?? operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator)
Definition at line 134 of file Optional.inl.
|
constexpr |
Same as *t, but Requires that 't' is engaged.
Definition at line 156 of file Optional.inl.
optional< OUT_T > Stroika::Foundation::Memory::OptionallyCopy | ( | const optional< IN_T > & | in | ) |
if you can copy an IN_T to an OUT_T, you should be able to copy an optional<IN_T> to an optional<OUT_T>
Actually, sometimes you can. But due to C++ one-step conversion operator rule, some cases where you would want this to work it doesn't. More likely though, you need to specify some other lambda todo the conversion of T like below:
Definition at line 180 of file Optional.inl.
|
constexpr |
wrappers on std c++23 monadic optional support, til we can assume c++23
Definition at line 207 of file Optional.h.
|
constexpr |
wrappers on std c++23 monadic optional support, til we can assume c++23
Definition at line 224 of file Optional.h.
|
constexpr |
wrappers on std c++23 monadic optional support, til we can assume c++23
Definition at line 241 of file Optional.h.
|
constexpr |
'Constructor' taking const RHS_CONVERTIBLE_TO_OPTIONAL_OF_T* is to allow easier interoperability with code that uses null-pointers to mean 'is-missing': nullptr means missing, and if non null, dereference and copy.
Definition at line 17 of file Optional.inl.
optional< T > Stroika::Foundation::Memory::operator+ | ( | const optional< T > & | lhs, |
const optional< T > & | rhs | ||
) |
if lhs and rhs engaged, this returns *lhs + *rhs, and otherwise nullopt
Definition at line 203 of file Optional.inl.
optional< T > Stroika::Foundation::Memory::operator- | ( | const optional< T > & | lhs, |
const optional< T > & | rhs | ||
) |
if lhs and rhs engaged, this returns *lhs - *rhs, and otherwise nullopt
Definition at line 233 of file Optional.inl.
optional< T > Stroika::Foundation::Memory::operator* | ( | const optional< T > & | lhs, |
const optional< T > & | rhs | ||
) |
if lhs and rhs engaged, this returns *lhs * *rhs, and otherwise nullopt
Definition at line 263 of file Optional.inl.
optional< T > Stroika::Foundation::Memory::operator/ | ( | const optional< T > & | lhs, |
const optional< T > & | rhs | ||
) |
if lhs and rhs engaged, this returns *lhs / *rhs, and otherwise nullopt
Definition at line 293 of file Optional.inl.