Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ build:clang --copt -Wno-used-but-marked-unused
build:clang --copt -Wno-switch-default
#
build:clang --copt -Wno-poison-system-directories
build:clang --copt -Wno-unknown-warning-option
build:clang --copt -Wno-nrvo
122 changes: 122 additions & 0 deletions include/fixed_containers/optional_reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "fixed_containers/source_location.hpp"

#include <compare>
#include <functional>
#include <memory>
#include <optional>
#include <type_traits>
Expand All @@ -27,6 +28,9 @@ class OptionalReference
using pointer = T*;
using const_pointer = T*;
using BackingType = pointer;
template <class F, class Ref>
using TransformResultType =
OptionalReference<std::remove_reference_t<std::invoke_result_t<F, Ref>>>;

public:
// Needed for structural type
Expand Down Expand Up @@ -144,6 +148,124 @@ class OptionalReference
return val;
}

template <class F>
constexpr std::invoke_result_t<F, reference> and_then(F&& func) &
{
if (has_value())
{
return std::invoke(std::forward<F>(func), *val());
}
return std::invoke_result_t<F, reference>{};
}

template <class F>
constexpr std::invoke_result_t<F, const_reference> and_then(F&& func) const&
{
if (has_value())
{
return std::invoke(std::forward<F>(func), *val());
}
return std::invoke_result_t<F, const_reference>{};
}

template <class F>
constexpr std::invoke_result_t<F, reference> and_then(F&& func) &&
{
if (has_value())
{
return std::invoke(std::forward<F>(func), *val());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this std::move val? Or will *val() already be an rvalue when we're inside this special kind of method

}
return std::invoke_result_t<F, reference>{};
}

template <class F>
constexpr std::invoke_result_t<F, const_reference> and_then(F&& func) const&&
{
if (has_value())
{
return std::invoke(std::forward<F>(func), *val());
}
return std::invoke_result_t<F, const_reference>{};
}

template <class F>
constexpr TransformResultType<F, reference> transform(F&& func) &
{
using InvokeResult = std::invoke_result_t<F, reference>;
static_assert(std::is_reference_v<InvokeResult>,
"transform() function must return a reference for OptionalReference");
Comment on lines +194 to +196

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having this requirement makes transform() pretty useless (how often does a real function return a reference?). I think returning a std::optional is much more reasonable here, at least as a valid choice. It could even intelligently choose between the two return types based on the function passed.

using ResultValueType = std::remove_reference_t<InvokeResult>;
if (has_value())
{
return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val()));
}
return OptionalReference<ResultValueType>{};
}

template <class F>
constexpr TransformResultType<F, const_reference> transform(F&& func) const&
{
using InvokeResult = std::invoke_result_t<F, const_reference>;
static_assert(std::is_reference_v<InvokeResult>,
"transform() function must return a reference for OptionalReference");
using ResultValueType = std::remove_reference_t<InvokeResult>;
if (has_value())
{
return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val()));
}
return OptionalReference<ResultValueType>{};
}

template <class F>
constexpr TransformResultType<F, reference> transform(F&& func) &&
{
using InvokeResult = std::invoke_result_t<F, reference>;
static_assert(std::is_reference_v<InvokeResult>,
"transform() function must return a reference for OptionalReference");
using ResultValueType = std::remove_reference_t<InvokeResult>;
if (has_value())
{
return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val()));
}
return OptionalReference<ResultValueType>{};
}

template <class F>
constexpr TransformResultType<F, const_reference> transform(F&& func) const&&
{
using InvokeResult = std::invoke_result_t<F, const_reference>;
static_assert(std::is_reference_v<InvokeResult>,
"transform() function must return a reference for OptionalReference");
using ResultValueType = std::remove_reference_t<InvokeResult>;
if (has_value())
{
return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val()));
}
return OptionalReference<ResultValueType>{};
}

template <class F>
constexpr Self or_else(F&& func) const&
requires std::is_same_v<std::invoke_result_t<F>, Self>
{
if (has_value())
{
return *this;
}
return std::invoke(std::forward<F>(func));
}

template <class F>
constexpr Self or_else(F&& func) &&
requires std::is_same_v<std::invoke_result_t<F>, Self>
{
if (has_value())
{
return std::move(*this);
}
return std::invoke(std::forward<F>(func));
}

private:
[[nodiscard]] constexpr const BackingType& val() const
{
Expand Down
1 change: 1 addition & 0 deletions test/fixed_deque_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <algorithm>
#include <array>
#include <concepts>
#include <cstddef>
#include <deque>
#include <initializer_list>
Expand Down
1 change: 1 addition & 0 deletions test/fixed_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <concepts>
#include <cstddef>
#include <functional>
#include <iterator>
Expand Down
3 changes: 2 additions & 1 deletion test/fixed_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <algorithm>
#include <array>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <span>
Expand Down Expand Up @@ -1910,7 +1911,7 @@ TEST(FixedString, AppendTruncating)
constexpr auto VAL1 = []()
{
FixedString<4> out{"1"};
std::string_view view{"2345678"};
const std::string_view view{"2345678"};
append_truncating(out, view);
return out;
}();
Expand Down
1 change: 1 addition & 0 deletions test/fixed_unordered_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <memory>
Expand Down
1 change: 1 addition & 0 deletions test/fixed_vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <algorithm>
#include <array>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <memory>
Expand Down
Loading
Loading