Skip to content

Commit

Permalink
some work on optional
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Aug 27, 2024
1 parent de6e282 commit 97ed848
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions include/flatmemory/details/types/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@
namespace flatmemory
{
/**
* ID class for non-trivial Optional type.
*
* The idea is to encode in the prefix size whether the optional holds a value of type T as follows:
* If the size prefix = sizeof(BufferSizeType), then the optional does not hold a value.
* This works because hashing or equality will be correct
* ID class
*/

/// @brief Optional encodes in the prefix size whether it holds a value of type T as follows:
/// If the size prefix = sizeof(BufferSizeType), then the optional does not hold a value.
/// @tparam T
template<IsTriviallyCopyableOrNonTrivialType T>
struct Optional : public NonTrivialType
{
Expand All @@ -70,48 +69,10 @@ class Layout<Optional<T>>
constexpr void print() const;
};

/**
* Free function operators
*/

template<IsOptional O>
bool operator==(const O& lhs, const O& rhs);

template<IsOptional O>
bool operator==(const O& lhs, std::nullopt_t);

template<IsOptional O>
bool operator==(std::nullopt_t, const O& rhs);

template<IsOptional O1, IsOptional O2>
requires HaveSameValueType<O1, O2>
bool operator==(const O1& lhs, const O2& rhs);

template<IsOptional O>
bool operator==(std::nullopt_t, const O& rhs);

template<IsOptional O>
bool operator==(const O& lhs, std::nullopt_t);

template<IsOptional O>
bool operator!=(const O& lhs, const O& rhs);

template<IsOptional O>
bool operator!=(const O& lhs, std::nullopt_t);

template<IsOptional O>
bool operator!=(std::nullopt_t, const O& rhs);

template<IsOptional O>
bool operator!=(const O& lhs, std::nullopt_t);

template<IsOptional O>
bool operator!=(std::nullopt_t, const O& rhs);

/**
* Builder
*/
// Specialization for trivially copyable types

template<IsTriviallyCopyable T>
class Builder<Optional<T>> : public IBuilder<Builder<Optional<T>>>
{
Expand Down Expand Up @@ -404,6 +365,7 @@ class View<Optional<T>>
/**
* ConstView
*/

template<IsTriviallyCopyable T>
class ConstView<Optional<T>>
{
Expand Down Expand Up @@ -476,6 +438,48 @@ class ConstView<Optional<T>>
friend class Builder;
};

/**
* Free function operators
*/

template<IsOptional O>
bool operator==(const O& lhs, const O& rhs);

template<IsOptional O>
bool operator==(const O& lhs, std::nullopt_t);

template<IsOptional O>
bool operator==(std::nullopt_t, const O& rhs);

template<IsOptional O1, IsOptional O2>
requires HaveSameValueType<O1, O2>
bool operator==(const O1& lhs, const O2& rhs);

template<IsOptional O>
bool operator!=(const O& lhs, const O& rhs)
{
return !(lhs == rhs);
}

template<IsOptional O>
bool operator!=(const O& lhs, std::nullopt_t)
{
return !(lhs == std::nullopt);
}

template<IsOptional O>
bool operator!=(std::nullopt_t, const O& rhs)
{
return !(std::nullopt == rhs);
}

template<IsOptional O1, IsOptional O2>
requires HaveSameValueType<O1, O2>
bool operator!=(const O1& lhs, const O2& rhs)
{
return !(lhs == rhs);
}

}

#endif

0 comments on commit 97ed848

Please sign in to comment.