Skip to content

Commit

Permalink
Simplify struct_names_clang.h. (#34)
Browse files Browse the repository at this point in the history
* Simplify `struct_names_clang.h`.
* Fix/Verify that `ToTuple` works for `move-eligble` values as expected: ToTuple(Extend&&) -> std::tuple<T...>.
* Fixed issue with `Extend` handling move only types when used in decompose assignment.
* Made template search functionality (`BinarySearch`, `LinearSearch`, `MaxSearch`, `ReverseSearch`) public.
  • Loading branch information
helly25 authored May 4, 2024
1 parent 68dd9b0 commit e0921c8
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 107 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

* Added concept `mbo::types::IsVariant`.
* Added concept `mbo::types::HasVariantMember`.
* Fixed issue with `Extend` ability to handle struct names when compiled with Clang.
* Fixed issue with `Extend` handling move only types when used in decompose assignment.
* Added template struct `BinarySearch` implements templated binary search algorithm.
* Added template struct `LinearSearch` implements templated linear search algorithm.
* Added template struct `ReverseSearch` implements templated reverse linear search algorithm.
* Added template struct `MaxSearch` implements templated linear search for last match algorithm.

# 0.2.24

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ The C++ library is organized in functional groups each residing in their own dir
* Mainly, this prevents calling the destructor and thus prevents termination issues (initialization order fiasco).
* mbo/types:ref_wrap_cc, mbo/types/ref_wrap.h
* template-type `RefWrap<T>`: similar to `std::reference_wrapper` but supports operators `->` and `*`.
* mbo/types:template_search_cc, mbo/types/template_search.h:
* template struct `BinarySearch` implements templated binary search algorithm.
* template struct `LinearSearch` implements templated linear search algorithm.
* template struct `ReverseSearch` implements templated reverse linear search algorithm.
* template struct `MaxSearch` implements templated linear search for last match algorithm.
* mbo/types:traits_cc, mbo/types/traits.h
* type alias `ContainerConstIteratorValueType` returned the value-type of the const_iterator of a container.
* concept `ContainerIsForwardIteratable` determines whether a types can be used in forward iteration.
Expand Down
36 changes: 26 additions & 10 deletions mbo/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ cc_library(

cc_library(
name = "extend_cc",
hdrs = ["extend.h"],
srcs = ["extender.h"],
hdrs = ["extend.h"],
visibility = ["//visibility:public"],
deps = [
":tstring_cc",
Expand Down Expand Up @@ -85,18 +85,34 @@ cc_test(
)

cc_library(
name = "ref_wrap_cc",
hdrs = ["ref_wrap.h"],
visibility = ["//visibility:public"],
name = "ref_wrap_cc",
hdrs = ["ref_wrap.h"],
visibility = ["//visibility:public"],
)

cc_test(
name = "ref_wrap_test",
srcs = ["ref_wrap_test.cc"],
deps = [
":ref_wrap_cc",
"@com_google_googletest//:gtest_main",
]
name = "ref_wrap_test",
srcs = ["ref_wrap_test.cc"],
deps = [
":ref_wrap_cc",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "template_search_cc",
hdrs = ["template_search.h"],
visibility = ["//visibility:public"],
)

cc_test(
name = "template_search_test",
srcs = ["template_search_test.cc"],
deps = [
":template_search_cc",
":traits_cc",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
Expand Down
39 changes: 39 additions & 0 deletions mbo/types/extend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -716,5 +716,44 @@ TEST_F(ExtendTest, VariantMember) {
EXPECT_THAT(data.ToString(), Conditional(kStructNameSupport, R"({.value: 69})", R"({69})"));
}

struct MoveOnly {
constexpr explicit MoveOnly(int val) noexcept : value(val) {}

constexpr MoveOnly() = delete;
constexpr ~MoveOnly() noexcept = default;

MoveOnly(const MoveOnly&) = delete;
MoveOnly& operator=(const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) noexcept = default;
MoveOnly& operator=(MoveOnly&&) noexcept = default;

int value{0};
};

struct UseMoveOnly : Extend<UseMoveOnly> {
MoveOnly move1{0};
MoveOnly move2{0};
};

TEST_F(ExtendTest, MoveOnlyTuple) {
// Verify that `ToTuple` works for `move-eligble` values as expected: ToTuple(Extend&&) -> std::tuple<T...>.
static constexpr int kValue1{25};
static constexpr int kValue2{33};
static constexpr int kValue3{42};
UseMoveOnly data{
.move1{MoveOnly{kValue1}},
.move2{MoveOnly{kValue2}},
};
auto [move1, move2] = std::move(data);
EXPECT_THAT(move1.value, kValue1);
EXPECT_THAT(move2.value, kValue2);

move1.value = kValue3;
EXPECT_THAT(move1.value, kValue3);
EXPECT_THAT(move2.value, kValue2);
EXPECT_THAT(data.move1.value, Ne(kValue3))
<< "It does not matter what the value is after the move, but it must not be `kValue3`.";
}

} // namespace
} // namespace mbo::types
15 changes: 4 additions & 11 deletions mbo/types/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ cc_test(
cc_library(
name = "traits_cc",
hdrs = [
"binary_search.h",
"decompose_count.h",
"is_braces_constructible.h",
],
deps = [":cases_cc"],
deps = [
":cases_cc",
"//mbo/types:template_search_cc",
],
)

cc_library(
Expand All @@ -77,12 +79,3 @@ mope_test(
outs = ["decompose_count.h"],
clang_format = True,
)

cc_test(
name = "binary_search_test",
srcs = ["binary_search_test.cc"],
deps = [
":traits_cc",
"@com_google_googletest//:gtest_main",
]
)
Loading

0 comments on commit e0921c8

Please sign in to comment.