cast.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2016 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * cast.h
22  * Jan 10, 2016
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_INTERFACE_CAST_H__INCLUDED
27 #define TILEDARRAY_TILE_INTERFACE_CAST_H__INCLUDED
28 
29 #include "../meta.h"
30 #include "../type_traits.h"
31 
32 namespace TiledArray {
33 
34 template <typename Result, typename Arg, typename Enabler = void>
35 class Cast;
36 
37 namespace tile_interface {
38 
39 template <typename Result, typename Arg, typename Enabler>
40 class Cast;
41 
43 
52 template <typename Result, typename Arg>
53 class Cast<Result, Arg,
54  std::enable_if_t<detail::has_conversion_operator_v<
55  Arg, madness::Future<Result>> ||
56  detail::is_convertible_v<Arg, Result>>> {
57  public:
58  typedef Result result_type;
59  typedef Arg argument_type;
60  static const constexpr bool is_nonblocking =
63 
64  // basic type sanity checks
65  static_assert(std::is_same<result_type, std::decay_t<result_type>>::value,
66  "Cast<Result,Arg>: Result must be a non-const non-ref type");
67  static_assert(std::is_same<argument_type, std::decay_t<argument_type>>::value,
68  "Cast<Result,Arg>: Arg must be a non-const non-ref type");
71  detail::is_convertible_v<argument_type, result_type>,
72  "Cast<Result,Arg> does not know how to construct Result or "
73  "Future<Result> from Arg");
74 
75  private:
76  template <typename Result_, typename Arg_,
77  typename = std::enable_if_t<
79  static auto invoker(Arg_&& arg) {
80  auto exec = [](Arg_&& arg) {
81  return static_cast<Result_>(std::forward<Arg_>(arg));
82  };
83  return TiledArray::meta::invoke(exec, arg);
84  }
85  template <typename Result_, typename Arg_>
86  static auto invoker(
87  Arg_&& arg,
88  std::enable_if_t<
89  !madness::is_future<Result_>::value &&
90  !detail::is_convertible<std::decay_t<Arg_>, Result_>::value &&
92  std::decay_t<Arg_>, madness::Future<Result_>>>* = nullptr) {
93  auto exec = [](Arg_&& arg) {
94  return static_cast<madness::Future<Result_>>(std::forward<Arg_>(arg));
95  };
96  return TiledArray::meta::invoke(exec, std::forward<Arg_>(arg));
97  }
98 
99  public:
103  template <typename Arg_,
104  typename = std::enable_if_t<std::is_same<
105  argument_type, madness::remove_fcvr_t<Arg_>>::value>>
106  auto operator()(Arg_&& arg) const {
107  return this->invoker<result_type>(arg);
108  }
109 
110 }; // class Cast
111 
113 
122 template <typename Result, typename Arg>
123 class Cast<Result, Arg,
124  typename std::enable_if<
125  is_lazy_tile<Arg>::value &&
126  !std::is_same<Result, typename TiledArray::eval_trait<
127  Arg>::type>::value>::type> {
128  private:
129  typedef typename TiledArray::eval_trait<Arg>::type
130  eval_type;
135  public:
136  typedef Result result_type;
137  typedef Arg argument_type;
138 
140 
149  template <typename Arg_, typename = std::enable_if_t<std::is_same<
150  argument_type, std::decay_t<Arg_>>::value>>
151  auto operator()(Arg_&& arg) const {
152  arg_to_eval_caster_type cast_to_eval;
153  eval_to_result_caster_type cast_to_result;
154  return meta::invoke(cast_to_result, meta::invoke(cast_to_eval, arg));
155  }
156 
157 }; // class Cast
158 
159 } // namespace tile_interface
160 
162 
167 template <typename Result, typename Arg, typename Enabler>
168 class Cast : public TiledArray::tile_interface::Cast<Result, Arg, Enabler> {};
169 
174 template <typename Arg, typename Result = typename TiledArray::eval_trait<
175  madness::remove_fcvr_t<Arg>>::type>
176 auto invoke_cast(Arg&& arg) {
178  return TiledArray::meta::invoke(cast, std::forward<Arg>(arg));
179 }
180 
181 } // namespace TiledArray
182 
183 #endif // TILEDARRAY_TILE_INTERFACE_CAST_H__INCLUDED
auto invoke_cast(Arg &&arg)
Definition: cast.h:176
constexpr const bool has_conversion_operator_v
Definition: type_traits.h:496
Tile cast operation.
Definition: cast.h:168
auto invoke(Function &&fn, Args &&... args) -> typename std::enable_if< !or_reduce< false, madness::is_future< std::decay_t< Args >>::value... >::value, decltype(fn(args...))>::type
Definition: meta.h:52
Determine the object type used in the evaluation of tensor expressions.
Definition: type_traits.h:580