Skip to content

Commit 4d20a7e

Browse files
Simplified dispatch iface
1 parent 7d40a9a commit 4d20a7e

File tree

9 files changed

+40
-46
lines changed

9 files changed

+40
-46
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ int main() {
6363
- `composes<Affordances...>`
6464
- `chooses<Affordances...>`
6565
- `destructible`
66-
- `move_constructible`
67-
- `copy_constructible`
66+
- `movable`
67+
- `copyable`
6868
- `extractable<In>`
6969
- `insertable<Out>`
7070
- `allocator_storage<Allocator>`

include/simply/copyable.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#ifndef SIMPLY_COPYABLE_HPP
22
#define SIMPLY_COPYABLE_HPP
33

4-
#include <simply/movable.hpp>
4+
#include <simply/type_traits.hpp>
55

66
namespace simply {
77

8-
struct copy_constructible : simply::copy_affordance_base {
8+
struct copyable : simply::copy_affordance_base {
99
template <typename T>
1010
requires std::is_copy_constructible_v<T>
1111
static constexpr auto fn(const std::type_identity_t<T> &self) noexcept(
@@ -14,9 +14,6 @@ struct copy_constructible : simply::copy_affordance_base {
1414
}
1515
};
1616

17-
struct copyable
18-
: simply::composes<simply::copy_constructible, simply::movable> {};
19-
2017
} // namespace simply
2118

2219
#endif // SIMPLY_COPYABLE_HPP

include/simply/dispatch.hpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,12 @@
88

99
namespace simply {
1010

11-
struct indirect_dispatch : simply::dispatch_affordance_base {
12-
template <simply::affordance Affordance>
13-
struct type : simply::affordance_base {
14-
using affordance_type = Affordance;
15-
};
16-
};
11+
struct indirect_dispatch : simply::dispatch_affordance_base {};
1712

18-
struct inplace_dispatch : simply::dispatch_affordance_base {
19-
template <simply::affordance Affordance>
20-
struct type : simply::affordance_base {
21-
using affordance_type = Affordance;
22-
};
23-
};
13+
struct inplace_dispatch : simply::dispatch_affordance_base {};
2414

25-
template <simply::specialization_of<simply::indirect_dispatch::type> Dispatch,
26-
typename Self>
27-
requires simply::affordance<Dispatch>
28-
struct iface<Dispatch, Self> {
15+
template <typename Self>
16+
struct iface<simply::indirect_dispatch, Self> {
2917
template <typename T>
3018
constexpr iface([[maybe_unused]] std::in_place_type_t<T> tag)
3119
: vtable_ptr(
@@ -44,16 +32,14 @@ struct iface<Dispatch, Self> {
4432
return static_cast<const base_type &>(*vtable_ptr).fn;
4533
}
4634

47-
using affordance_type = Dispatch::affordance_type;
35+
using affordance_type = simply::affordance_type_t<Self>;
4836
using vtable_type = simply::vtable<affordance_type, Self>;
4937

5038
const vtable_type *vtable_ptr;
5139
};
5240

53-
template <simply::specialization_of<simply::inplace_dispatch::type> Dispatch,
54-
typename Self>
55-
requires simply::affordance<Dispatch>
56-
struct iface<Dispatch, Self> {
41+
template <typename Self>
42+
struct iface<simply::inplace_dispatch, Self> {
5743
template <typename T>
5844
constexpr iface([[maybe_unused]] std::in_place_type_t<T> tag)
5945
: vtable(simply::vtable_for<affordance_type, Self, T>) {}
@@ -71,7 +57,7 @@ struct iface<Dispatch, Self> {
7157
return static_cast<const base_type &>(vtable).fn;
7258
}
7359

74-
using affordance_type = Dispatch::affordance_type;
60+
using affordance_type = simply::affordance_type_t<Self>;
7561
using vtable_type = simply::vtable<affordance_type, Self>;
7662

7763
vtable_type vtable;

include/simply/dyn.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class dyn
2222
: public simply::iface<Affordance,
2323
simply::dyn<Affordance, Storage, Dispatch>>,
2424
public simply::iface<Storage, simply::dyn<Affordance, Storage, Dispatch>>,
25-
public simply::iface<typename Dispatch::template type<Affordance>,
25+
public simply::iface<Dispatch,
2626
simply::dyn<Affordance, Storage, Dispatch>> {
2727
using storage_base = simply::iface<Storage, dyn>;
28-
using dispatch_base =
29-
simply::iface<typename Dispatch::template type<Affordance>, dyn>;
28+
using dispatch_base = simply::iface<Dispatch, dyn>;
3029

3130
public:
3231
using affordance_type = Affordance;
@@ -102,6 +101,11 @@ using dyn = simply::dyn<Affordance, simply::pmr::polymorphic_allocator_storage,
102101

103102
} // namespace pmr
104103

104+
template <typename Affordance, typename Storage, typename Dispatch>
105+
struct affordance_type<simply::dyn<Affordance, Storage, Dispatch>> {
106+
using type = Affordance;
107+
};
108+
105109
template <simply::member_affordance Affordance,
106110
simply::specialization_of<simply::dyn> Dyn, typename R, typename Self,
107111
typename... Args, bool NoExcept>

include/simply/elide.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class elide {
3939
template <typename U>
4040
void operator,(U &&) = delete;
4141

42-
constexpr ~elide() = default;
42+
~elide() = default;
4343
};
4444

4545
template <typename F>

include/simply/movable.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
#ifndef SIMPLY_MOVABLE_HPP
22
#define SIMPLY_MOVABLE_HPP
33

4-
#include <simply/destructible.hpp>
4+
#include <simply/type_traits.hpp>
5+
6+
#include <utility>
57

68
namespace simply {
79

8-
struct move_constructible : simply::move_affordance_base {
10+
struct movable : simply::move_affordance_base {
911
template <std::move_constructible T>
1012
static constexpr auto fn(std::type_identity_t<T> &&self) noexcept(
1113
std::is_nothrow_move_constructible_v<T>) -> T {
1214
return std::move(self);
1315
}
1416
};
1517

16-
struct movable
17-
: simply::composes<simply::move_constructible, simply::destructible> {};
18-
1918
} // namespace simply
2019

2120
#endif // SIMPLY_MOVABLE_HPP

include/simply/storage.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct allocator_storage : simply::storage_affordance_base {
2121
using void_pointer = traits::void_pointer;
2222
using const_void_pointer = traits::const_void_pointer;
2323

24+
// TODO express this by dispatching directly from iface to reduce boilerplate
2425
template <typename T>
2526
struct fn_t {
2627
using rebound = traits::template rebind_traits<T>;
@@ -103,10 +104,10 @@ struct iface<Storage, Self> {
103104
: alloc(), object_ptr(_construct_with(simply::_rebind_alloc<T>(alloc),
104105
std::forward<Args>(args)...)) {}
105106

106-
constexpr auto operator=(const iface &other) -> iface & = delete;
107-
constexpr auto operator=(iface &&other) noexcept -> iface & = delete;
107+
auto operator=(const iface &other) -> iface & = default;
108+
auto operator=(iface &&other) noexcept -> iface & = default;
108109

109-
constexpr ~iface() noexcept = default;
110+
~iface() = default;
110111

111112
[[nodiscard]] constexpr auto get_allocator() const noexcept
112113
-> allocator_type {

include/simply/type_traits.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct dispatch_affordance_base : simply::affordance_base {};
2727
template <typename Affordance, typename T>
2828
struct affordance_traits;
2929

30+
template <typename T>
31+
struct affordance_type;
32+
33+
template <typename T>
34+
using affordance_type_t = affordance_type<T>::type;
35+
3036
template <typename Affordance, typename T,
3137
typename Fn = affordance_traits<Affordance, T>::function_type>
3238
struct impl;

tests/counters.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <simply/copyable.hpp>
2+
#include <simply/destructible.hpp>
23
#include <simply/dyn.hpp>
34

45
#include <algorithm>
@@ -8,14 +9,14 @@
89
// example of a class with a label that counts how many times it is copied
910
template <char Label>
1011
struct counter {
11-
constexpr counter() = default;
12+
counter() = default;
1213
constexpr counter(const counter &other) : copied(other.copied + 1) {}
1314
counter(counter &&other) = delete;
1415

1516
auto operator=(const counter &) -> counter & = delete;
1617
auto operator=(counter &&) -> counter & = delete;
1718

18-
constexpr ~counter() = default;
19+
~counter() = default;
1920

2021
[[nodiscard]] constexpr auto copy_count() const { return copied; }
2122
[[nodiscard]] constexpr auto label() const { return Label; }
@@ -59,9 +60,9 @@ struct simply::iface<labeled<LabelT>, Self> {
5960

6061
// test dyn<countable> at compile-time
6162
static_assert([] {
62-
struct countable
63-
: simply::composes<copy_countable<int>, labeled<char>,
64-
simply::copy_constructible, simply::destructible> {};
63+
struct countable : simply::composes<copy_countable<int>, labeled<char>,
64+
simply::copyable, simply::destructible> {
65+
};
6566

6667
// initialize a vector of dyn<countable> with three different counter types
6768
std::vector<simply::dyn<countable>> v;

0 commit comments

Comments
 (0)