Skip to content

Commit

Permalink
Use std::identity
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Aug 12, 2024
1 parent d8f9e15 commit 19f9287
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
23 changes: 7 additions & 16 deletions include/mettle/detail/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@

namespace mettle::detail {

// XXX: When we start requiring C++20, we can remove this.
struct identity {
template<typename T>
constexpr decltype(auto) operator ()(T &&t) const {
return std::forward<T>(t);
}
};

template<typename T>
std::string stringify(const T &t) {
std::ostringstream ss;
ss << t;
return ss.str();
}

template<typename ...Args>
const std::string & stringify(const std::string &s) {
inline const std::string & stringify(const std::string &s) {
return s;
}

Expand Down Expand Up @@ -68,22 +59,22 @@ namespace mettle::detail {
const std::string &delim_;
};

template<typename Iter, typename Func = identity>
template<typename Iter, typename Func = std::identity>
inline const string_joiner<Iter, Func>
iter_joined(Iter begin, Iter end, const Func &func = identity{},
iter_joined(Iter begin, Iter end, const Func &func = Func{},
const std::string &delim = ", ") {
return {begin, end, func, delim};
}

template<typename T, typename Func = identity>
inline auto joined(const T &t, const Func &func = identity{},
template<typename T, typename Func = std::identity>
inline auto joined(const T &t, const Func &func = Func{},
const std::string &delim = ", ") ->
const string_joiner<decltype(std::begin(t)), Func> {
return {std::begin(t), std::end(t), func, delim};
}

template<typename T, typename Func = identity>
inline auto joined(std::initializer_list<T> t, const Func &func = identity{},
template<typename T, typename Func = std::identity>
inline auto joined(std::initializer_list<T> t, const Func &func = Func{},
const std::string &delim = ", ") ->
const string_joiner<decltype(std::begin(t)), Func> {
return {std::begin(t), std::end(t), func, delim};
Expand Down
8 changes: 4 additions & 4 deletions include/mettle/suite/make_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ namespace mettle {
make_subsuite(const std::string &name, const attributes &attrs,
Args &&...args) {
return detail::do_build<ParentFixture, Fixture...>(
name, attrs, std::forward<Args>(args)..., detail::identity{}
name, attrs, std::forward<Args>(args)..., std::identity{}
);
}

template<typename ParentFixture, typename ...Fixture, typename ...Args>
inline auto
make_subsuite(const std::string &name, Args &&...args) {
return detail::do_build<ParentFixture, Fixture...>(
name, {}, std::forward<Args>(args)..., detail::identity{}
name, {}, std::forward<Args>(args)..., std::identity{}
);
}

Expand All @@ -179,15 +179,15 @@ namespace mettle {
make_subsuites(const std::string &name, const attributes &attrs,
Args &&...args) {
return detail::do_builds<ParentFixture, Fixture...>(
name, attrs, std::forward<Args>(args)..., detail::identity{}
name, attrs, std::forward<Args>(args)..., std::identity{}
);
}

template<typename ParentFixture, typename ...Fixture, typename ...Args>
inline auto
make_subsuites(const std::string &name, Args &&...args) {
return detail::do_builds<ParentFixture, Fixture...>(
name, {}, std::forward<Args>(args)..., detail::identity{}
name, {}, std::forward<Args>(args)..., std::identity{}
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libmettle/log/xunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace mettle::log {

void xunit::started_suite(const std::vector<std::string> &suites) {
using namespace mettle::detail;
auto name = stringify(joined(suites, identity{}, " > "));
auto name = stringify(joined(suites, std::identity{}, " > "));
suite_stack_.emplace(std::move(name));
}

Expand Down

0 comments on commit 19f9287

Please sign in to comment.