Skip to content

Commit c9d0b6d

Browse files
committed
[WIP] Selectors accept actual compile-time strings
1 parent 13d419b commit c9d0b6d

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

Diff for: example/vtable_traits.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ struct any_iterator {
5959
using Concept = Iterator<reference>;
6060
using Storage = dyno::remote_storage;
6161
using VTable = dyno::vtable<
62-
dyno::local<dyno::only<
63-
decltype("increment"_s), decltype("equal"_s), decltype("dereference"_s)
64-
>>,
62+
dyno::local<dyno::only<"increment"_s, "equal"_s, "dereference"_s>>,
6563
dyno::remote<dyno::everything_else>
6664
>;
6765
dyno::poly<Concept, Storage, VTable> poly_;

Diff for: include/dyno/detail/dsl.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ namespace detail {
104104
return detail::delayed_call<string, Args&&...>{std::forward<Args>(args)...};
105105
}
106106

107+
static constexpr char storage_[] = {c..., '\0'};
108+
constexpr operator char const*() const { return storage_; }
109+
107110
using hana_tag = typename boost::hana::tag_of<boost::hana::string<c...>>::type;
108111
};
109112

@@ -115,6 +118,21 @@ namespace detail {
115118
constexpr auto prepare_string(S) {
116119
return detail::prepare_string_impl<S>(std::make_index_sequence<S::size()>{});
117120
}
121+
122+
constexpr std::size_t constexpr_strlen(char const* s) {
123+
std::size_t len = 0;
124+
while (*s++ != '\0') ++len;
125+
return len;
126+
}
127+
128+
template <char const* s>
129+
constexpr auto make_string() {
130+
struct tmp {
131+
static constexpr std::size_t size() { return detail::constexpr_strlen(s); }
132+
static constexpr char const* get() { return s; }
133+
};
134+
return detail::prepare_string(tmp{});
135+
}
118136
} // end namespace detail
119137

120138
inline namespace literals {

Diff for: include/dyno/vtable.hpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define DYNO_VTABLE_HPP
77

88
#include <dyno/concept.hpp>
9+
#include <dyno/detail/dsl.hpp>
910
#include <dyno/detail/erase_function.hpp>
1011
#include <dyno/detail/erase_signature.hpp>
1112

@@ -208,11 +209,11 @@ struct joined_vtable {
208209

209210
//////////////////////////////////////////////////////////////////////////////
210211
// Selectors
211-
template <typename ...Functions>
212+
template <char const* ...Functions>
212213
struct only {
213214
template <typename All>
214215
constexpr auto operator()(All all) const {
215-
auto matched = boost::hana::make_set(Functions{}...);
216+
auto matched = boost::hana::make_set(detail::make_string<Functions>()...);
216217
static_assert(decltype(boost::hana::is_subset(matched, all))::value,
217218
"dyno::only: Some functions specified in this selector are not part of "
218219
"the concept to which the selector was applied.");
@@ -223,11 +224,11 @@ struct only {
223224
}
224225
};
225226

226-
template <typename ...Functions>
227+
template <char const* ...Functions>
227228
struct except {
228229
template <typename All>
229230
constexpr auto operator()(All all) const {
230-
auto not_matched = boost::hana::make_set(Functions{}...);
231+
auto not_matched = boost::hana::make_set(detail::make_string<Functions>()...);
231232
static_assert(decltype(boost::hana::is_subset(not_matched, all))::value,
232233
"dyno::except: Some functions specified in this selector are not part of "
233234
"the concept to which the selector was applied.");
@@ -251,12 +252,12 @@ namespace detail {
251252
template <typename T>
252253
struct is_valid_selector : boost::hana::false_ { };
253254

254-
template <typename ...Methods>
255+
template <char const* ...Methods>
255256
struct is_valid_selector<dyno::only<Methods...>>
256257
: boost::hana::true_
257258
{ };
258259

259-
template <typename ...Methods>
260+
template <char const* ...Methods>
260261
struct is_valid_selector<dyno::except<Methods...>>
261262
: boost::hana::true_
262263
{ };
@@ -389,12 +390,12 @@ constexpr auto generate_vtable(Policies policies) {
389390
// cumbersome. Selectors provided by the library are:
390391
//
391392
// dyno::only<functions...>
392-
// Picks only the specified functions from a concept. `functions` must be
393-
// compile-time strings, such as `dyno::only<decltype("foo"_s), decltype("bar"_s)>`.
393+
// Picks only the specified functions from a concept. `functions...` must
394+
// be compile-time strings, such as `dyno::only<"foo"_s, "bar"_s>`.
394395
//
395396
// dyno::except<functions...>
396-
// Picks all but the specified functions from a concept. `functions` must
397-
// be compile-time strings, such as `dyno::except<decltype("foo"_s), decltype("bar"_s)>`.
397+
// Picks all but the specified functions from a concept. `functions...`
398+
// must be compile-time strings, such as `dyno::except<"foo"_s, "bar"_s>`.
398399
//
399400
// dyno::everything
400401
// Picks all the functions from a concept.

Diff for: test/detail/dsl.make_string.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Louis Dionne 2017
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <dyno/detail/dsl.hpp>
6+
7+
#include <type_traits>
8+
9+
10+
int main() {
11+
{
12+
static constexpr char const s[] = "";
13+
static_assert(std::is_same<decltype(dyno::detail::make_string<s>()),
14+
dyno::detail::string<>>{});
15+
}
16+
17+
{
18+
static constexpr char const s[] = "a";
19+
static_assert(std::is_same<decltype(dyno::detail::make_string<s>()),
20+
dyno::detail::string<'a'>>{});
21+
}
22+
23+
{
24+
static constexpr char const s[] = "abc";
25+
static_assert(std::is_same<decltype(dyno::detail::make_string<s>()),
26+
dyno::detail::string<'a', 'b', 'c'>>{});
27+
}
28+
}

0 commit comments

Comments
 (0)