Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FixedMap/EnumMap] Fix emplace to work with non-copyable/non-moveable types and piecewise construction #85

Merged
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
51 changes: 51 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ cc_library(
copts = ["-std=c++20"],
)

cc_library(
name = "assert_or_abort",
hdrs = ["include/fixed_containers/assert_or_abort.hpp"],
includes = ["include"],
copts = ["-std=c++20"],
)

cc_library(
name = "bidirectional_iterator",
hdrs = ["include/fixed_containers/bidirectional_iterator.hpp"],
Expand All @@ -34,6 +41,7 @@ cc_library(
hdrs = ["include/fixed_containers/circular_indexing.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":int_math",
":integer_range",
],
Expand All @@ -45,6 +53,7 @@ cc_library(
hdrs = ["include/fixed_containers/circular_integer_range_iterator.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":circular_indexing",
":int_math",
":integer_range",
Expand Down Expand Up @@ -75,11 +84,22 @@ cc_library(
copts = ["-std=c++20"],
)

cc_library(
name = "emplace",
hdrs = ["include/fixed_containers/emplace.hpp"],
includes = ["include"],
deps = [
":concepts",
],
copts = ["-std=c++20"],
)

cc_library(
name = "enum_array",
hdrs = ["include/fixed_containers/enum_array.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":concepts",
":enum_utils",
":ranges",
Expand All @@ -92,8 +112,10 @@ cc_library(
hdrs = ["include/fixed_containers/enum_map.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":bidirectional_iterator",
":concepts",
":emplace",
":enum_utils",
":erase_if",
":filtered_integer_range_iterator",
Expand All @@ -112,6 +134,7 @@ cc_library(
hdrs = ["include/fixed_containers/enum_set.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":bidirectional_iterator",
":concepts",
":enum_utils",
Expand All @@ -126,6 +149,7 @@ cc_library(
hdrs = ["include/fixed_containers/enum_utils.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
"@com_github_neargye_magic_enum//:magic_enum",
],
copts = ["-std=c++20"],
Expand All @@ -143,6 +167,7 @@ cc_library(
hdrs = ["include/fixed_containers/filtered_integer_range_iterator.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":bidirectional_iterator",
":integer_range",
":iterator_utils",
Expand Down Expand Up @@ -181,6 +206,7 @@ cc_library(
includes = ["include"],
deps = [
":algorithm",
":assert_or_abort",
":circular_indexing",
":consteval_compare",
":integer_range",
Expand All @@ -199,6 +225,7 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_index_based_storage.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":consteval_compare",
":fixed_vector",
":index_or_value_storage",
Expand All @@ -211,7 +238,9 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_map.hpp",],
includes = ["include"],
deps = [
":assert_or_abort",
":bidirectional_iterator",
":emplace",
":erase_if",
":fixed_red_black_tree",
":map_checking",
Expand Down Expand Up @@ -244,6 +273,7 @@ cc_library(
],
includes = ["include"],
deps = [
":assert_or_abort",
":concepts",
":fixed_index_based_storage",
":value_or_reference_storage",
Expand All @@ -255,6 +285,7 @@ cc_library(
name = "fixed_red_black_tree_view",
hdrs = ["include/fixed_containers/fixed_red_black_tree_view.hpp"],
deps = [
":assert_or_abort",
":fixed_red_black_tree",
],
includes = ["include"],
Expand All @@ -266,6 +297,7 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_set.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":bidirectional_iterator",
":erase_if",
":fixed_red_black_tree",
Expand Down Expand Up @@ -293,6 +325,7 @@ cc_library(
hdrs = ["include/fixed_containers/fixed_string.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":concepts",
":fixed_vector",
":preconditions",
Expand Down Expand Up @@ -341,13 +374,19 @@ cc_library(
name = "int_math",
hdrs = ["include/fixed_containers/int_math.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
],
copts = ["-std=c++20"],
)

cc_library(
name = "integer_range",
hdrs = ["include/fixed_containers/integer_range.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
],
copts = ["-std=c++20"],
)

Expand All @@ -356,6 +395,7 @@ cc_library(
hdrs = ["include/fixed_containers/integer_range_iterator.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":int_math",
":integer_range",
":iterator_utils",
Expand Down Expand Up @@ -474,6 +514,7 @@ cc_library(
hdrs = ["include/fixed_containers/reflection.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
":concepts",
":fixed_stack",
":fixed_vector",
Expand Down Expand Up @@ -527,6 +568,9 @@ cc_library(
name = "string_literal",
hdrs = ["include/fixed_containers/string_literal.hpp"],
includes = ["include"],
deps = [
":assert_or_abort",
],
copts = ["-std=c++20"],
)

Expand Down Expand Up @@ -654,6 +698,7 @@ cc_test(
name = "enum_map_test",
srcs = ["test/enum_map_test.cpp"],
deps = [
":assert_or_abort",
":consteval_compare",
":enum_map",
":enums_test_common",
Expand All @@ -671,6 +716,7 @@ cc_test(
name = "enum_set_test",
srcs = ["test/enum_set_test.cpp"],
deps = [
":assert_or_abort",
":consteval_compare",
":enum_set",
":enums_test_common",
Expand Down Expand Up @@ -711,6 +757,7 @@ cc_test(
name = "fixed_circular_deque_test",
srcs = ["test/fixed_circular_deque_test.cpp"],
deps = [
":assert_or_abort",
":fixed_circular_deque",
":instance_counter",
":mock_testing_types",
Expand All @@ -737,6 +784,7 @@ cc_test(
name = "fixed_deque_test",
srcs = ["test/fixed_deque_test.cpp"],
deps = [
":assert_or_abort",
":fixed_deque",
":instance_counter",
":mock_testing_types",
Expand All @@ -762,6 +810,7 @@ cc_test(
name = "fixed_map_test",
srcs = ["test/fixed_map_test.cpp"],
deps = [
":assert_or_abort",
":concepts",
":consteval_compare",
":fixed_map",
Expand Down Expand Up @@ -810,6 +859,7 @@ cc_test(
name = "fixed_set_test",
srcs = ["test/fixed_set_test.cpp"],
deps = [
":assert_or_abort",
":concepts",
":consteval_compare",
":fixed_set",
Expand Down Expand Up @@ -867,6 +917,7 @@ cc_test(
name = "fixed_vector_test",
srcs = ["test/fixed_vector_test.cpp"],
deps = [
":assert_or_abort",
":fixed_vector",
":instance_counter",
":mock_testing_types",
Expand Down
15 changes: 15 additions & 0 deletions include/fixed_containers/assert_or_abort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <cstdlib>

namespace fixed_containers
{
// In contrast to assert(), works in Release mode
constexpr void assert_or_abort(bool b)
{
if (!b)
{
std::abort();
}
}
} // namespace fixed_containers
6 changes: 3 additions & 3 deletions include/fixed_containers/circular_indexing.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/int_math.hpp"
#include "fixed_containers/integer_range.hpp"

#include <cassert>
#include <compare>
#include <cstdint>
#include <tuple>
Expand All @@ -30,7 +30,7 @@ constexpr CyclesAndInteger increment_index_with_wraparound(const IntegerRangeTyp
if (range.distance() == 0)
{
// Default constructed
assert(i == 0);
assert_or_abort(i == 0);
return {};
}

Expand All @@ -53,7 +53,7 @@ constexpr CyclesAndInteger decrement_index_with_wraparound(const IntegerRangeTyp
if (range.distance() == 0)
{
// Default constructed
assert(i == 0);
assert_or_abort(i == 0);
return {};
}

Expand Down
8 changes: 4 additions & 4 deletions include/fixed_containers/circular_integer_range_iterator.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/circular_indexing.hpp"
#include "fixed_containers/int_math.hpp"
#include "fixed_containers/integer_range.hpp"
#include "fixed_containers/iterator_utils.hpp"
#include "fixed_containers/random_access_iterator.hpp"

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
Expand Down Expand Up @@ -54,18 +54,18 @@ class CircularIntegerRangeEntryProvider

constexpr bool operator==(const CircularIntegerRangeEntryProvider& other) const noexcept
{
assert(integer_range_ == other.integer_range_);
assert_or_abort(integer_range_ == other.integer_range_);
return current_index_ == other.current_index_;
}
constexpr auto operator<=>(const CircularIntegerRangeEntryProvider& other) const noexcept
{
assert(integer_range_ == other.integer_range_);
assert_or_abort(integer_range_ == other.integer_range_);
return current_index_ <=> other.current_index_;
}

constexpr std::ptrdiff_t operator-(const CircularIntegerRangeEntryProvider& other) const
{
assert(integer_range_ == other.integer_range_);
assert_or_abort(integer_range_ == other.integer_range_);
const auto cycle_offset =
static_cast<std::ptrdiff_t>(current_index_.cycles - other.current_index_.cycles) *
static_cast<std::ptrdiff_t>(integer_range_.distance());
Expand Down
3 changes: 3 additions & 0 deletions include/fixed_containers/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ concept InputIterator =
std::is_convertible_v<typename std::iterator_traits<Iterator>::iterator_category,
std::input_iterator_tag>;

template <typename T>
concept IsStdPair = std::same_as<T, std::pair<typename T::first_type, typename T::second_type>>;

// The member type `is_transparent` is a convention that indicates to the user that this function
// object is a transparent function object: it accepts arguments of arbitrary types and uses perfect
// forwarding, which avoids unnecessary copying and conversion when the function object is used in
Expand Down
42 changes: 42 additions & 0 deletions include/fixed_containers/emplace.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "fixed_containers/concepts.hpp"

#include <utility>

namespace fixed_containers::emplace_detail
{
template <typename Container, typename... Args>
requires(sizeof...(Args) >= 1 and sizeof...(Args) <= 3)
constexpr std::pair<typename Container::iterator, bool> emplace_in_terms_of_try_emplace_impl(
Container& c, Args&&... args)
{
return [&]<typename First, typename... Rest>(First&& first, Rest&&... rest)
{
if constexpr (sizeof...(Rest) == 0 && IsStdPair<First>)
{
// Lambda to avoid compilation errors with .first/.second when passing a non-pair
return [&c]<typename Pair>(Pair&& pair) {
return c.try_emplace(std::forward<Pair>(pair).first,
std::forward<Pair>(pair).second);
}(std::forward<First>(first));
}
else if constexpr (sizeof...(Rest) == 2 &&
std::same_as<std::piecewise_construct_t, std::decay_t<First>>)
{
return [&c]<typename P1, typename P2>(P1&& p1, P2&& p2)
{
return [&c, &p1, &p2]<std::size_t... INDEX_1, std::size_t... INDEX_2>(
std::index_sequence<INDEX_1...>, std::index_sequence<INDEX_2...>) {
return c.try_emplace(std::get<INDEX_1>(p1)..., std::get<INDEX_2>(p2)...);
}(std::make_index_sequence<std::tuple_size_v<P1>>{},
std::make_index_sequence<std::tuple_size_v<P2>>{});
}(std::forward<Rest>(rest)...);
}
else
{
return c.try_emplace(std::forward<First>(first), std::forward<Rest>(rest)...);
}
}(std::forward<Args>(args)...);
}
} // namespace fixed_containers::emplace_detail
3 changes: 2 additions & 1 deletion include/fixed_containers/enum_array.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/concepts.hpp"
#include "fixed_containers/enum_utils.hpp"
#include "fixed_containers/ranges.hpp"
Expand Down Expand Up @@ -97,7 +98,7 @@ class EnumArray
requires NotDefaultConstructible<T>
: values_(initializer_pair_list_to_value_array(list))
{
assert(fixed_containers::rich_enums_detail::is_zero_based_contiguous_and_sorted(
assert_or_abort(fixed_containers::rich_enums_detail::is_zero_based_contiguous_and_sorted(
ENUM_COUNT, PairOrdinalComparator<ENUM_COUNT>{list}));
}

Expand Down
Loading
Loading