overload.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_UTIL_OVERLOAD_HPP
23 #define NDN_CXX_UTIL_OVERLOAD_HPP
24 
25 #include <boost/predef/compiler/gcc.h>
26 #include <boost/version.hpp>
27 
28 // Hana does not support GCC < 6.0.0
29 #if (BOOST_VERSION >= 106100) && (!BOOST_COMP_GNUC || (BOOST_COMP_GNUC >= BOOST_VERSION_NUMBER(6,0,0)))
30 
31 #include <boost/hana/functional/overload.hpp>
32 
33 namespace ndn {
34 constexpr boost::hana::make_overload_t overload{};
35 } // namespace ndn
36 
37 #else
38 
39 #include <type_traits>
40 
41 namespace ndn {
42 namespace detail {
43 
44 // The following code is copied from the Boost.Hana library.
45 // Copyright Louis Dionne 2013-2017
46 // Distributed under the Boost Software License, Version 1.0.
47 // (See http://boost.org/LICENSE_1_0.txt)
48 
61 #ifndef DOXYGEN
62 template <typename F, typename ...G>
63 struct overload_t
64  : overload_t<F>::type
65  , overload_t<G...>::type
66 {
67  using type = overload_t;
68  using overload_t<F>::type::operator();
69  using overload_t<G...>::type::operator();
70 
71  template <typename F_, typename ...G_>
72  constexpr explicit overload_t(F_&& f, G_&& ...g)
73  : overload_t<F>::type(static_cast<F_&&>(f))
74  , overload_t<G...>::type(static_cast<G_&&>(g)...)
75  { }
76 };
77 
78 template <typename F>
79 struct overload_t<F> { using type = F; };
80 
81 template <typename R, typename ...Args>
82 struct overload_t<R(*)(Args...)> {
83  using type = overload_t;
84  R (*fptr_)(Args...);
85 
86  explicit constexpr overload_t(R (*fp)(Args...))
87  : fptr_(fp)
88  { }
89 
90  constexpr R operator()(Args ...args) const
91  { return fptr_(static_cast<Args&&>(args)...); }
92 };
93 
94 struct make_overload_t {
95  template <typename ...F,
96  typename Overload = typename overload_t<
97  typename std::decay<F>::type...
98  >::type
99  >
100  constexpr Overload operator()(F&& ...f) const {
101  return Overload(static_cast<F&&>(f)...);
102  }
103 };
104 #endif // DOXYGEN
105 
106 } // namespace detail
107 
108 constexpr detail::make_overload_t overload{};
109 
110 } // namespace ndn
111 
112 #endif // BOOST_VERSION >= 106100
113 #endif // NDN_CXX_UTIL_OVERLOAD_HPP
Definition: data.cpp:25
constexpr detail::make_overload_t overload
Definition: overload.hpp:108