piranha  0.10
convert_to.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_CONVERT_TO_HPP
30 #define PIRANHA_CONVERT_TO_HPP
31 
32 #include <type_traits>
33 #include <utility>
34 
35 #include <piranha/detail/sfinae_types.hpp>
36 #include <piranha/type_traits.hpp>
37 
38 namespace piranha
39 {
40 
42 template <typename To, typename From, typename = void>
44 private:
45  template <typename From2>
46  using return_type_ = decltype(static_cast<To>(std::declval<const From2 &>()));
47  template <typename From2>
48  using return_type = typename std::enable_if<is_returnable<return_type_<From2>>::value, return_type_<From2>>::type;
49 
50 public:
52 
68  template <typename From2>
69  return_type<From2> operator()(const From2 &x) const
70  {
71  return static_cast<To>(x);
72  }
73 };
74 
75 namespace detail
76 {
77 
78 // Enabler for the generic conversion function. We need to check that the impl functor is callable
79 // and that it returns the proper type.
80 template <typename To, typename From>
81 using convert_to_enabler =
82  typename std::enable_if<std::is_same<decltype(convert_to_impl<typename std::decay<To>::type, From>{}(
83  std::declval<const From &>())),
84  typename std::decay<To>::type>::value
85  && is_returnable<typename std::decay<To>::type>::value,
86  int>::type;
87 }
88 
90 
116 template <typename To, typename From, detail::convert_to_enabler<To, From> = 0>
117 inline To convert_to(const From &x)
118 {
120 }
121 
123 
127 template <typename To, typename From>
128 class has_convert_to : detail::sfinae_types
129 {
130  using Tod = typename std::decay<To>::type;
131  using Fromd = typename std::decay<From>::type;
132  template <typename To1, typename From1>
133  static auto test(const To1 &, const From1 &x) -> decltype(convert_to<To1>(x), void(), yes());
134  static no test(...);
135  static const bool implementation_defined
136  = std::is_same<decltype(test(std::declval<Tod>(), std::declval<Fromd>())), yes>::value;
137 
138 public:
140  static const bool value = implementation_defined;
141 };
142 
143 // Static init.
144 template <typename To, typename From>
146 }
147 
148 #endif
return_type< From2 > operator()(const From2 &x) const
Call operator.
Definition: convert_to.hpp:69
static const bool value
Value of the type trait.
Definition: convert_to.hpp:140
Type trait to detect piranha::convert_to().
Definition: convert_to.hpp:128
To convert_to(const From &x)
Generic conversion function.
Definition: convert_to.hpp:117
Default functor for the implementation of piranha::convert_to().
Definition: convert_to.hpp:43
Root piranha namespace.
Definition: array_key.hpp:52
Type traits.