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

Add ReplacePlaceholders #451

Merged
merged 1 commit into from
Dec 22, 2021
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
20 changes: 20 additions & 0 deletions include/llama/Meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,25 @@ namespace llama

template<typename FromList, template<auto...> class ToList>
using mp_unwrap_values_into = typename mp_unwrap_values_into_impl<FromList, ToList>::type;

template<typename E, typename... Args>
struct ReplacePlaceholdersImpl
{
using type = E;
};
template<std::size_t I, typename... Args>
struct ReplacePlaceholdersImpl<boost::mp11::mp_arg<I>, Args...>
{
using type = boost::mp11::mp_at_c<boost::mp11::mp_list<Args...>, I>;
};

template<template<typename...> typename E, typename... Ts, typename... Args>
struct ReplacePlaceholdersImpl<E<Ts...>, Args...>
{
using type = E<typename ReplacePlaceholdersImpl<Ts, Args...>::type...>;
};
} // namespace internal

template<typename Expression, typename... Args>
using ReplacePlaceholders = typename internal::ReplacePlaceholdersImpl<Expression, Args...>::type;
} // namespace llama
27 changes: 27 additions & 0 deletions tests/meta.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "common.hpp"

TEST_CASE("ReplacePlaceholders")
{
using namespace boost::mp11;
using namespace tag;

STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<int, A, B, C>, int>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<std::string, A, B, C>, std::string>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<Particle, A, B, C>, Particle>);

STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<_1, A, B, C>, A>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<_2, A, B, C>, B>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<_3, A, B, C>, C>);

STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<_1>, A, B, C>, mp_list<A>>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<_1, _2>, A, B, C>, mp_list<A, B>>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<_3, _1, _2>, A, B, C>, mp_list<C, A, B>>);

STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<_3, _1, _1>, A, B, C>, mp_list<C, A, A>>);
STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<_2, _2, _2>, A, B, C>, mp_list<B, B, B>>);

STATIC_REQUIRE(std::is_same_v<llama::ReplacePlaceholders<mp_list<mp_list<_2>>, A, B, C>, mp_list<mp_list<B>>>);
STATIC_REQUIRE(std::is_same_v<
llama::ReplacePlaceholders<mp_list<mp_list<_2, _1>, _2, mp_list<_2, mp_list<_3>>>, A, B, C>,
mp_list<mp_list<B, A>, B, mp_list<B, mp_list<C>>>>);
}