Skip to content

Commit

Permalink
Consolidate the two implementations of is_detected into one.
Browse files Browse the repository at this point in the history
Fixes #25.
  • Loading branch information
tzlaine committed Dec 16, 2023
1 parent efaa7c8 commit 14054ed
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 120 deletions.
41 changes: 32 additions & 9 deletions include/boost/parser/detail/detection.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Copyright (C) 2020 T. Zachary Laine
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_PARSER_DETAIL_DETECTION_HPP
#define BOOST_PARSER_DETAIL_DETECTION_HPP

#include <type_traits>


namespace boost { namespace parser { namespace detail {
namespace boost::parser::detail {

template<typename...>
struct void_
Expand All @@ -16,6 +21,24 @@ namespace boost { namespace parser { namespace detail {
template<typename... T>
using void_t = typename void_<T...>::type;

template<typename T>
struct fixup_ptr
{
using type = T;
};

template<typename T>
using remove_v_t = typename std::remove_volatile<T>::type;

template<typename T>
struct fixup_ptr<T *>
{
using type = remove_v_t<T> const *;
};

template<typename T>
using fixup_ptr_t = typename fixup_ptr<T>::type;

template<typename T>
using remove_cv_ref_t =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
Expand All @@ -26,8 +49,7 @@ namespace boost { namespace parser { namespace detail {
template<
typename Default,
typename AlwaysVoid,
template<typename...>
class Template,
template<typename...> class Template,
typename... Args>
struct detector
{
Expand All @@ -37,8 +59,7 @@ namespace boost { namespace parser { namespace detail {

template<
typename Default,
template<typename...>
class Template,
template<typename...> class Template,
typename... Args>
struct detector<Default, void_t<Template<Args...>>, Template, Args...>
{
Expand All @@ -50,18 +71,20 @@ namespace boost { namespace parser { namespace detail {
using is_detected =
typename detector<nonesuch, void, Template, Args...>::value_t;

template<template<typename...> class Template, typename... Args>
constexpr bool is_detected_v = is_detected<Template, Args...>::value;

template<template<typename...> class Template, typename... Args>
using detected_t =
typename detector<nonesuch, void, Template, Args...>::type;

template<
typename Default,
template<typename...>
class Template,
template<typename...> class Template,
typename... Args>
using detected_or =
using detected_or_t =
typename detector<Default, void, Template, Args...>::type;

}}}
}

#endif
2 changes: 1 addition & 1 deletion include/boost/parser/detail/hl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace boost { namespace parser { namespace detail::hl {
template<typename T, typename U>
constexpr bool operator()(T const & t, U const & u)
{
if constexpr (detail::is_detected<comparable, T, U>::value) {
if constexpr (detail::is_detected_v<comparable, T, U>) {
return t == u;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion include/boost/parser/detail/printing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ namespace boost { namespace parser { namespace detail {
using streamable =
decltype(std::declval<std::ostream &>() << std::declval<T const &>());

template<typename T, bool Streamable = is_detected<streamable, T>{}>
template<typename T, bool Streamable = is_detected_v<streamable, T>>
struct printer
{
std::ostream & operator()(std::ostream & os, T const &)
Expand Down
6 changes: 1 addition & 5 deletions include/boost/parser/detail/stl_interfaces/view_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <boost/parser/detail/stl_interfaces/config.hpp>
#include <boost/parser/detail/stl_interfaces/detail/view_closure.hpp>

#include <boost/parser/detail/text/detail/detection.hpp>
#include <boost/parser/detail/detection.hpp>

#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -50,10 +50,6 @@

namespace boost::parser::detail { namespace stl_interfaces {
namespace detail {
template<template<typename...> class Template, typename... Args>
constexpr bool is_detected_v =
text::detail::is_detected<Template, Args...>::value;

template<typename F, typename... Args>
using invocable_expr =
decltype(std::declval<F>()(std::declval<Args>()...));
Expand Down
2 changes: 1 addition & 1 deletion include/boost/parser/detail/text/detail/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define BOOST_PARSER_DETAIL_TEXT_DETAIL_ALGORITHM_HPP

#include <boost/parser/detail/text/concepts.hpp>
#include <boost/parser/detail/text/detail/detection.hpp>
#include <boost/parser/detail/detection.hpp>
#include <boost/parser/detail/text/detail/iterator.hpp>

#include <numeric>
Expand Down
12 changes: 5 additions & 7 deletions include/boost/parser/detail/text/detail/begin_end.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef BOOST_PARSER_DETAIL_TEXT_BEGIN_END_HPP
#define BOOST_PARSER_DETAIL_TEXT_BEGIN_END_HPP

#include <boost/parser/detail/text/detail/detection.hpp>
#include <boost/parser/detail/detection.hpp>

#include <initializer_list>

Expand Down Expand Up @@ -35,10 +35,9 @@ namespace boost::parser::detail { namespace text { namespace detail {
template<typename T>
using adl_begin_expr = decltype(begin(std::declval<T &>()));
template<typename T>
constexpr bool has_member_begin_v =
is_detected<member_begin_expr, T>::value;
constexpr bool has_member_begin_v = is_detected_v<member_begin_expr, T>;
template<typename T>
constexpr bool has_adl_begin_v = is_detected<adl_begin_expr, T>::value;
constexpr bool has_adl_begin_v = is_detected_v<adl_begin_expr, T>;

template<typename R>
using member_return_t =
Expand Down Expand Up @@ -99,10 +98,9 @@ namespace boost::parser::detail { namespace text { namespace detail {
template<typename T>
using adl_end_expr = decltype(end(std::declval<T &>()));
template<typename T>
constexpr bool has_member_end_v =
is_detected<member_end_expr, T>::value;
constexpr bool has_member_end_v = is_detected_v<member_end_expr, T>;
template<typename T>
constexpr bool has_adl_end_v = is_detected<adl_end_expr, T>::value;
constexpr bool has_adl_end_v = is_detected_v<adl_end_expr, T>;

template<typename R>
using member_return_t =
Expand Down
90 changes: 0 additions & 90 deletions include/boost/parser/detail/text/detail/detection.hpp

This file was deleted.

12 changes: 6 additions & 6 deletions include/boost/parser/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ namespace boost { namespace parser {
template<
typename Context,
typename T,
bool Callable = is_detected<callable, T const &, Context const &>{}>
bool Callable = is_detected_v<callable, T const &, Context const &>>
struct resolve_impl
{
static auto call(Context const &, T const & x) { return x; }
Expand Down Expand Up @@ -857,7 +857,7 @@ namespace boost { namespace parser {

template<typename T, typename U>
constexpr bool is_equality_comparable_with_v =
is_detected<comparison, T, U>::value;
is_detected_v<comparison, T, U>;

template<typename T>
struct is_nope : std::false_type
Expand Down Expand Up @@ -936,7 +936,7 @@ namespace boost { namespace parser {
{};

template<typename F, typename... Args>
constexpr bool is_invocable_v = is_detected<callable, F, Args...>::value;
constexpr bool is_invocable_v = is_detected_v<callable, F, Args...>;

#if BOOST_PARSER_USE_CONCEPTS

Expand Down Expand Up @@ -982,8 +982,8 @@ namespace boost { namespace parser {
std::declval<T>().end()));

template<typename T>
constexpr bool is_container_v = is_detected<has_insert, T>::value &&
is_detected<has_range_insert, T>::value;
constexpr bool is_container_v =
is_detected_v<has_insert, T> && is_detected_v<has_range_insert, T>;

template<typename T, typename U>
constexpr bool container_and_value_type = is_container_v<T> &&
Expand All @@ -1008,7 +1008,7 @@ namespace boost { namespace parser {
template<typename T>
constexpr bool is_parsable_range_v = is_parsable_code_unit_v<
remove_cv_ref_t<detected_t<has_begin, T>>> &&
is_detected<has_end, T>::value;
is_detected_v<has_end, T>;

template<typename T>
constexpr bool is_parsable_pointer_v =
Expand Down

0 comments on commit 14054ed

Please sign in to comment.