Skip to content

Commit

Permalink
Rename forward_if to forward_like and make it more like the standard
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Aug 13, 2024
1 parent e309594 commit 439d61c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
25 changes: 0 additions & 25 deletions include/mettle/detail/forward_if.hpp

This file was deleted.

22 changes: 22 additions & 0 deletions include/mettle/detail/forward_like.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef INC_METTLE_DETAIL_FORWARD_LIKE_HPP
#define INC_METTLE_DETAIL_FORWARD_LIKE_HPP

#include <type_traits>
#include <utility>

namespace mettle::detail {
// TODO: Remove this once we require C++23.
template<typename T, typename U>
inline auto && forward_like(U &&value) {
using Value = std::remove_reference_t<U>;
using Ref = std::conditional_t<
std::is_lvalue_reference_v<T>, Value &, Value &&
>;
using CRef = std::conditional_t<
std::is_const_v<std::remove_reference_t<T>>, const Ref, Ref
>;
return static_cast<CRef>(value);
}
} // namespace mettle::detail

#endif
17 changes: 9 additions & 8 deletions include/mettle/suite/compiled_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "attributes.hpp"
#include "../test_uid.hpp"
#include "../detail/forward_if.hpp"
#include "../detail/forward_like.hpp"

namespace mettle {

Expand Down Expand Up @@ -44,17 +44,18 @@ namespace mettle {
String &&name, Tests &&tests, Subsuites &&subsuites,
const attributes &attrs, Compile &&compile
) : name_(std::forward<String>(name)) {
using detail::forward_if;

for(auto &&test : tests) {
tests_.emplace_back(
forward_if<Tests>(test.name),
compile(forward_if<Tests>(test.function)),
unite(forward_if<Tests>(test.attrs), attrs)
detail::forward_like<Tests>(test.name),
compile(detail::forward_like<Tests>(test.function)),
unite(detail::forward_like<Tests>(test.attrs), attrs)
);
}
for(auto &&ss : subsuites) {
subsuites_.emplace_back(
detail::forward_like<Subsuites>(ss), attrs, compile
);
}
for(auto &&ss : subsuites)
subsuites_.emplace_back(forward_if<Subsuites>(ss), attrs, compile);
}

template<typename Function2, typename Compile>
Expand Down
2 changes: 1 addition & 1 deletion include/mettle/suite/make_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ namespace mettle {
template<typename Subsuites>
void subsuite(Subsuites &&subsuites) {
for(auto &&i : subsuites)
subsuites_.push_back(detail::forward_if<Subsuites>(i));
subsuites_.push_back(detail::forward_like<Subsuites>(i));
}

template<typename ...Fixture, typename ...Args>
Expand Down
12 changes: 6 additions & 6 deletions test/test_forward_if.cpp → test/test_forward_like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ using namespace mettle;

#include "copy_counter.hpp"

suite<> test_forward_if("forward_if()", [](auto &_) {
subsuite<int, int &, const int &>(_, "forward_if", type_only, [](auto &_) {
suite<> test_forward_like("forward_like()", [](auto &_) {
subsuite<int, int &, const int &>(_, "forward_like", type_only, [](auto &_) {
using Fixture = fixture_type_t<decltype(_)>;

_.test("forward_if<T>(moveable)", []() {
_.test("forward_like<T>(moveable)", []() {
bool should_move = !std::is_lvalue_reference<Fixture>::value;
moveable_type from;
auto to = detail::forward_if<Fixture>(from);
auto to = detail::forward_like<Fixture>(from);

expect("number of copies", to.copies, equal_to(should_move ? 0 : 1));
expect("number of moves", to.moves, equal_to(should_move ? 1 : 0));
});

_.test("forward_if<T>(copyable)", []() {
_.test("forward_like<T>(copyable)", []() {
copyable_type from;
auto to = detail::forward_if<Fixture>(from);
auto to = detail::forward_like<Fixture>(from);

expect("number of copies", to.copies, equal_to(1));
});
Expand Down

0 comments on commit 439d61c

Please sign in to comment.