Skip to content

Commit

Permalink
move policy to end of use_classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Feb 4, 2024
1 parent 3f01dc3 commit 9ae9f48
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 99 deletions.
19 changes: 9 additions & 10 deletions include/yorel/yomm2/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,17 @@ typename method<Key, R(A...), Policy>::next_type
// -----------------------------------------------------------------------------
// class_declaration

template<typename First, class... Rest>
struct class_declaration : detail::class_declaration_aux<
detail::get_policy<First, Rest...>,
detail::remove_policy<First, Rest...>> {};
template<class... Classes>
struct class_declaration
: detail::class_declaration_aux<
detail::get_policy<Classes...>, detail::remove_policy<Classes...>> {};


template<class... First>
struct class_declaration<detail::types<First...>>
: detail::class_declaration_aux<default_policy, detail::types<First...>> {};
template<class... Classes>
struct class_declaration<detail::types<Classes...>>
: detail::class_declaration_aux<
detail::get_policy<Classes...>, detail::remove_policy<Classes...>> {};

template<class Policy, class... First>
struct class_declaration<Policy, detail::types<First...>>
: detail::class_declaration_aux<Policy, detail::types<First...>> {};

template<typename First, class... Rest>
using use_classes = typename detail::use_classes_aux<
Expand Down
54 changes: 23 additions & 31 deletions include/yorel/yomm2/detail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,34 +259,18 @@ struct is_policy_aux<types<T...>> : std::false_type {};
template<typename T>
constexpr bool is_policy = is_policy_aux<T>::value;

template<bool, typename... Classes>
struct split_policy_aux;

template<typename Policy, typename... Classes>
struct split_policy_aux<true, Policy, Classes...> {
using policy = Policy;
using classes = types<Classes...>;
};

template<typename... Classes>
struct split_policy_aux<false, Classes...> {
using policy = default_policy;
using classes = types<Classes...>;
};

template<typename ClassOrPolicy, typename... Classes>
struct split_policy
: split_policy_aux<is_policy<ClassOrPolicy>, ClassOrPolicy, Classes...> {};
template<typename T>
constexpr bool is_not_policy = !is_policy<T>;

template<typename... Classes>
using get_policy = typename split_policy<Classes...>::policy;
using get_policy = std::conditional_t<
is_policy<boost::mp11::mp_back<types<Classes...>>>,
boost::mp11::mp_back<types<Classes...>>, default_policy>;

template<typename... Classes>
using remove_policy = typename split_policy<Classes...>::classes;

template<typename ClassOrPolicy, typename... Classes>
using remove_policy_first =
boost::mp11::mp_first<remove_policy<ClassOrPolicy, Classes...>>;
using remove_policy = std::conditional_t<
is_policy<boost::mp11::mp_back<types<Classes...>>>,
boost::mp11::mp_pop_back<types<Classes...>>, types<Classes...>>;

template<typename Signature>
struct next_ptr_t;
Expand Down Expand Up @@ -430,8 +414,8 @@ template<class Class, class Policy>
struct is_virtual_ptr_aux<virtual_ptr<Class, Policy>> : std::true_type {};

template<class Class, class Policy>
struct is_virtual_ptr_aux<const virtual_ptr<Class, Policy>&>
: std::true_type {};
struct is_virtual_ptr_aux<const virtual_ptr<Class, Policy>&> : std::true_type {
};

template<typename T>
constexpr bool is_virtual_ptr = is_virtual_ptr_aux<T>::value;
Expand Down Expand Up @@ -576,8 +560,7 @@ struct select_spec_polymorphic_type_aux<

template<class Policy, typename P, typename Q>
struct select_spec_polymorphic_type_aux<
Policy, const virtual_ptr<P, Policy>&,
const virtual_ptr<Q, Policy>&> {
Policy, const virtual_ptr<P, Policy>&, const virtual_ptr<Q, Policy>&> {
using type = typename virtual_traits<
Policy, const virtual_ptr<Q, Policy>&>::polymorphic_type;
};
Expand Down Expand Up @@ -671,10 +654,19 @@ struct use_classes_aux<Policy, types<types<Classes...>, MoreClassLists...>>

{};

template<class First, class Second, class... Rest>
template<typename... Ts>
using second_last = boost::mp11::mp_at_c<
types<Ts...>, boost::mp11::mp_size<types<Ts...>>::value - 2>;

template<class... Classes>
using use_classes_macro = typename std::conditional_t<
is_policy<Second>, use_classes_aux<Second, types<Rest...>>,
use_classes_aux<First, types<Second, Rest...>>>::type;
is_policy<second_last<Classes...>>,
use_classes_aux<
second_last<Classes...>,
boost::mp11::mp_pop_back<boost::mp11::mp_pop_back<types<Classes...>>>>,
use_classes_aux<
boost::mp11::mp_back<types<Classes...>>,
boost::mp11::mp_pop_back<types<Classes...>>>>::type;

std::ostream* log_on(std::ostream* os);
std::ostream* log_off();
Expand Down
77 changes: 38 additions & 39 deletions include/yorel/yomm2/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <yorel/yomm2/symbols.hpp>

#ifndef YOMM2_DEFAULT_POLICY
#define YOMM2_DEFAULT_POLICY ::yorel::yomm2::default_policy
#define YOMM2_DEFAULT_POLICY ::yorel::yomm2::default_policy
#endif

#define yOMM2_PLIST(N, I, A) \
Expand Down Expand Up @@ -50,22 +50,21 @@
::yorel::yomm2::method<YOMM2_SYMBOL(ID), R ARGS, POLICY>

#if !BOOST_PP_VARIADICS_MSVC
#define YOMM2_DECLARE(...) \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_, __VA_ARGS__) \
(__VA_ARGS__)
#define YOMM2_STATIC_DECLARE(...) \
BOOST_PP_OVERLOAD(YOMM2_STATIC_DECLARE_, __VA_ARGS__) \
(__VA_ARGS__)
#define YOMM2_DECLARE(...) \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_, __VA_ARGS__) \
(__VA_ARGS__)
#define YOMM2_STATIC_DECLARE(...) \
BOOST_PP_OVERLOAD(YOMM2_STATIC_DECLARE_, __VA_ARGS__) \
(__VA_ARGS__)
#else
#define YOMM2_DECLARE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_STATIC_DECLARE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_STATIC_DECLARE_, __VA_ARGS__)( \
__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_DECLARE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_STATIC_DECLARE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_STATIC_DECLARE_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#endif

#define YOMM2_DECLARE_3(R, ID, ARGS) \
Expand Down Expand Up @@ -98,19 +97,19 @@
}

#if defined(YOMM2_ENABLE_TRACE) && (YOMM2_ENABLE_TRACE & 1) || !defined(NDEBUG)
#define yOMM2_NAME(FROM_TYPEID, HUMAN_READABLE) HUMAN_READABLE
#define yOMM2_NAME(FROM_TYPEID, HUMAN_READABLE) HUMAN_READABLE
#else
#define yOMM2_NAME(FROM_TYPEID, HUMAN_READABLE) FROM_TYPEID
#define yOMM2_NAME(FROM_TYPEID, HUMAN_READABLE) FROM_TYPEID
#endif

#if !BOOST_PP_VARIADICS_MSVC
#define YOMM2_DEFINE(...) \
BOOST_PP_OVERLOAD(YOMM2_DEFINE_, __VA_ARGS__)(__VA_ARGS__)
#define YOMM2_DEFINE(...) \
BOOST_PP_OVERLOAD(YOMM2_DEFINE_, __VA_ARGS__)(__VA_ARGS__)
#else
#define YOMM2_DEFINE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DEFINE_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_DEFINE(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DEFINE_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#endif

#define YOMM2_DEFINE_3(RETURN_T, ID, ARGS) \
Expand Down Expand Up @@ -143,15 +142,15 @@
NS::_yOMM2_method::return_type NS::_yOMM2_spec::yOMM2_body ARGS

#if !BOOST_PP_VARIADICS_MSVC
#define YOMM2_DECLARE_METHOD_CONTAINER(...) \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_METHOD_CONTAINER_, __VA_ARGS__) \
(__VA_ARGS__)
#define YOMM2_DECLARE_METHOD_CONTAINER(...) \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_METHOD_CONTAINER_, __VA_ARGS__) \
(__VA_ARGS__)
#else
#define YOMM2_DECLARE_METHOD_CONTAINER(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_METHOD_CONTAINER_, __VA_ARGS__)( \
__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_DECLARE_METHOD_CONTAINER(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_DECLARE_METHOD_CONTAINER_, __VA_ARGS__)( \
__VA_ARGS__), \
BOOST_PP_EMPTY())
#endif

#define YOMM2_DECLARE_METHOD_CONTAINER_1(CONTAINER) \
Expand Down Expand Up @@ -195,13 +194,13 @@
INLINE NS::_yOMM2_method::return_type CONTAINER<RETURN_T ARGS>::fn ARGS

#if !BOOST_PP_VARIADICS_MSVC
#define YOMM2_FRIEND(...) \
BOOST_PP_OVERLOAD(YOMM2_FRIEND_, __VA_ARGS__)(__VA_ARGS__)
#define YOMM2_FRIEND(...) \
BOOST_PP_OVERLOAD(YOMM2_FRIEND_, __VA_ARGS__)(__VA_ARGS__)
#else
#define YOMM2_FRIEND(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_FRIEND_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#define YOMM2_FRIEND(...) \
BOOST_PP_CAT( \
BOOST_PP_OVERLOAD(YOMM2_FRIEND_, __VA_ARGS__)(__VA_ARGS__), \
BOOST_PP_EMPTY())
#endif

#define YOMM2_FRIEND_1(CONTAINER) \
Expand All @@ -218,7 +217,7 @@

#define YOMM2_CLASSES(...) \
static ::yorel::yomm2::detail::use_classes_macro< \
YOMM2_DEFAULT_POLICY, __VA_ARGS__> \
__VA_ARGS__, YOMM2_DEFAULT_POLICY> \
YOMM2_GENSYM;

#endif
2 changes: 1 addition & 1 deletion include/yorel/yomm2/policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct virtual_;
template<class Policy, typename Key, typename Signature>
struct method;

template<typename Class, typename... Rest>
template<class... Classes>
struct class_declaration;

// -----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ struct population : abstract_population {
using Base = typename Dispatch::template base_type<Inheritance>;
using varg_type = Base&;

use_classes<Policy, classes> YOMM2_GENSYM;
use_classes<classes, Policy>; YOMM2_GENSYM;

using method1 = method<population, void(virtual_<Base&>), Policy>
using method2 = method<population, void(virtual_<Base&>, virtual_<Base&>), Policy>
Expand All @@ -327,7 +327,7 @@ struct population : abstract_population {
template<class Class> using vptr = virtual_ptr<Class, Policy>;
using varg_type = vptr<Base>;

use_classes<Policy, classes> YOMM2_GENSYM;
use_classes<classes, Policy>; YOMM2_GENSYM;

using method1 = method<population, void(vptr<Base>), Policy>
using method2 = method<
Expand Down
2 changes: 1 addition & 1 deletion tests/blackbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ struct base {

struct derived : base {};

class_declaration<test_policy, derived, base> YOMM2_GENSYM;
class_declaration<derived, base, test_policy> YOMM2_GENSYM;

BOOST_AUTO_TEST_CASE(test_update_error_handling) {
auto prev_handler = test_policy::error;
Expand Down
14 changes: 7 additions & 7 deletions tests/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ struct Human : Carnivore, Herbivore {};
using whole_hierarchy = test_policy_<__COUNTER__>;
using incremental = test_policy_<__COUNTER__>;

use_classes<whole_hierarchy, Animal, Herbivore, Carnivore, Cow, Wolf, Human>
use_classes<Animal, Herbivore, Carnivore, Cow, Wolf, Human, whole_hierarchy>
YOMM2_GENSYM;

use_classes<incremental, Animal, Herbivore, Cow> YOMM2_GENSYM;
use_classes<incremental, Animal, Carnivore, Wolf> YOMM2_GENSYM;
use_classes<incremental, Herbivore, Carnivore, Human> YOMM2_GENSYM;
use_classes<Animal, Herbivore, Cow, incremental> YOMM2_GENSYM;
use_classes<Animal, Carnivore, Wolf, incremental> YOMM2_GENSYM;
use_classes<Herbivore, Carnivore, Human, incremental> YOMM2_GENSYM;

using policies = std::tuple<whole_hierarchy, incremental>;

Expand Down Expand Up @@ -153,10 +153,10 @@ struct Jet : Expense {};
using test_policy = test_policy_<__COUNTER__>;
// any type from this namespace would work.

use_classes<test_policy, Role, Employee, Manager, Founder, Expense>
use_classes<Role, Employee, Manager, Founder, Expense, test_policy>
YOMM2_GENSYM;

use_classes<test_policy, Expense, Public, Bus, Metro, Taxi, Jet> YOMM2_GENSYM;
use_classes<Expense, Public, Bus, Metro, Taxi, Jet, test_policy> YOMM2_GENSYM;

#undef YOMM2_DEFAULT_POLICY
#define YOMM2_DEFAULT_POLICY test_policy
Expand Down Expand Up @@ -708,7 +708,7 @@ struct E : D {};

using test_policy = test_policy_<__COUNTER__>;

use_classes<test_policy, A, B, AB, C, D, E> YOMM2_GENSYM;
use_classes<A, B, AB, C, D, E, test_policy> YOMM2_GENSYM;

BOOST_AUTO_TEST_CASE(test_use_classes_mi) {
std::vector<rt_class*> actual, expected;
Expand Down
39 changes: 39 additions & 0 deletions tests/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,45 @@ static_assert(
>
>);

static_assert(
std::is_same_v<
use_classes<Animal, Dog, Bulldog, Cat, Dolphin>,
std::tuple<
class_declaration_aux<default_policy, types<Animal, Animal>>,
class_declaration_aux<default_policy, types<Dog, Animal, Dog>>,
class_declaration_aux<default_policy, types<Bulldog, Animal, Dog, Bulldog>>,
class_declaration_aux<default_policy, types<Cat, Animal, Cat>>,
class_declaration_aux<default_policy, types<Dolphin, Animal, Dolphin>>
>
>);

static_assert(
std::is_same_v<
use_classes_macro<Animal, default_policy>,
std::tuple<
class_declaration_aux<default_policy, types<Animal, Animal>>
>
>);

struct my_policy : policy::abstract_policy {};

static_assert(
std::is_same_v<
use_classes_macro<Animal, my_policy, default_policy>,
std::tuple<
class_declaration_aux<my_policy, types<Animal, Animal>>
>
>);

static_assert(
std::is_same_v<
use_classes_macro<Animal, my_policy>,
std::tuple<
class_declaration_aux<my_policy, types<Animal, Animal>>
>
>);


} // namespace test_use_classes

namespace facets {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_virtual_ptr_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(
test_virtual_ptr, Policy, policy_types<__COUNTER__>) {
using namespace detail;

static use_classes<Policy, Player, Warrior, Object, Axe, Bear> YOMM2_GENSYM;
static use_classes<Player, Warrior, Object, Axe, Bear, Policy> YOMM2_GENSYM;
using kick = method<void, std::string(virtual_ptr<Player, Policy>), Policy>;
static typename kick::template add_function<
kick_bear<virtual_ptr<Player, Policy>>>
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace test_virtual_ptr_dispatch {
BOOST_AUTO_TEST_CASE_TEMPLATE(
test_virtual_ptr_dispatch, Policy, policy_types<__COUNTER__>) {

static use_classes<Policy, Player, Warrior, Object, Axe, Bear> YOMM2_GENSYM;
static use_classes<Player, Warrior, Object, Axe, Bear, Policy> YOMM2_GENSYM;

using kick = method<void, std::string(virtual_ptr<Player, Policy>), Policy>;
static typename kick::template add_function<
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace test_virtual_shared_ptr_dispatch {
BOOST_AUTO_TEST_CASE_TEMPLATE(
test_virtual_ptr_dispatch, Policy, policy_types<__COUNTER__>) {

static use_classes<Policy, Player, Warrior, Object, Axe, Bear> YOMM2_GENSYM;
static use_classes<Player, Warrior, Object, Axe, Bear, Policy> YOMM2_GENSYM;

using kick =
method<void, std::string(virtual_shared_ptr<Player, Policy>), Policy>;
Expand Down
Loading

0 comments on commit 9ae9f48

Please sign in to comment.