Skip to content

Commit

Permalink
🆕 ⬆️ [NTTP] Support for ""_s using C++20 `non-type template paramet…
Browse files Browse the repository at this point in the history
…ers`

Problem:
- ""_s is using a GNU extension to produce an aux::string.

Solution:
- C++20 `non-type template parameters` instead if available.

Note:
- That makes SML C++20 standard compliant.
  • Loading branch information
krzysztof-jusiak committed May 30, 2020
1 parent da8a0cc commit 2d598be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
27 changes: 26 additions & 1 deletion include/boost/sml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ const char *get_type_name() {
return detail::get_type_name<T, 63>(__PRETTY_FUNCTION__, make_index_sequence<sizeof(__PRETTY_FUNCTION__) - 63 - 2>{});
#endif
}
#if defined(__cpp_nontype_template_parameter_class)
template <auto N>
struct fixed_string {
static constexpr auto size = N;
char data[N + 1]{};
constexpr fixed_string(char const *str) {
for (auto i = 0; i < N; ++i) {
data[i] = str[i];
}
}
};
template <auto N>
fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
#endif
template <class T, T...>
struct string;
template <char... Chrs>
Expand Down Expand Up @@ -2614,7 +2628,18 @@ template <class T>
typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
#endif
inline namespace literals {
#if !(defined(_MSC_VER) && !defined(__clang__))
#if defined(__cpp_nontype_template_parameter_class)
template <aux::fixed_string Str>
constexpr auto operator""_s() {
return []<auto... Ns>(aux::index_sequence<Ns...>) { return front::state<aux::string<char, Str.data[Ns]...>>{}; }
(aux::make_index_sequence<Str.size>{});
}
template <aux::fixed_string Str>
constexpr auto operator""_e() {
return []<auto... Ns>(aux::index_sequence<Ns...>) { return event<aux::string<char, Str.data[Ns]...>>; }
(aux::make_index_sequence<Str.size>{});
}
#elif !(defined(_MSC_VER) && !defined(__clang__))
template <class T, T... Chrs>
constexpr auto operator""_s() {
return front::state<aux::string<T, Chrs...>>{};
Expand Down
16 changes: 16 additions & 0 deletions include/boost/sml/aux_/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ const char *get_type_name() {
#endif // __pph__
}

#if defined(__cpp_nontype_template_parameter_class) // __pph__
template <auto N>
struct fixed_string {
static constexpr auto size = N;
char data[N + 1]{};

constexpr fixed_string(char const *str) {
for (auto i = 0; i < N; ++i) {
data[i] = str[i];
}
}
};
template <auto N>
fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
#endif // __pph__

template <class T, T...>
struct string;

Expand Down
16 changes: 13 additions & 3 deletions include/boost/sml/transition_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
#endif // __pph__

inline namespace literals {
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
#if defined(__cpp_nontype_template_parameter_class) // __pph__
template <aux::fixed_string Str>
constexpr auto operator""_s() {
return []<auto... Ns>(aux::index_sequence<Ns...>) { return front::state<aux::string<char, Str.data[Ns]...>>{}; }
(aux::make_index_sequence<Str.size>{});
}
template <aux::fixed_string Str>
constexpr auto operator""_e() {
return []<auto... Ns>(aux::index_sequence<Ns...>) { return event<aux::string<char, Str.data[Ns]...>>; }
(aux::make_index_sequence<Str.size>{});
}
#elif !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
template <class T, T... Chrs>
constexpr auto operator""_s() {
return front::state<aux::string<T, Chrs...>>{};
Expand All @@ -64,8 +75,7 @@ template <class T, T... Chrs>
constexpr auto operator""_e() {
return event<aux::string<T, Chrs...>>;
}
#endif // __pph__

#endif // __pph__
} // literals

__BOOST_SML_UNUSED static front::state<back::terminate_state> X;
Expand Down

0 comments on commit 2d598be

Please sign in to comment.