meta.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2017 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  * Eduard Valeyev
19  * Department of Chemistry, Virginia Tech
20  *
21  * meta.h
22  * April 11, 2017
23  *
24  */
25 
26 #ifndef SRC_TILEDARRAY_META_H_
27 #define SRC_TILEDARRAY_META_H_
28 
30 #include <madness/world/future.h>
31 #include <madness/world/world.h>
32 #include <madness/world/world_task_queue.h>
33 
34 namespace TiledArray {
35 namespace meta {
36 
38 template <bool head, bool... tail>
39 struct or_reduce {
40  static constexpr bool value = head || or_reduce<tail...>::value;
41 };
42 
43 template <bool b>
44 struct or_reduce<b> {
45  static constexpr bool value = b;
46 };
47 
48 // is any argument a Future?
49 // - yes: async launch
50 // - no: direct launch
51 template <typename Function, typename... Args>
52 auto invoke(Function&& fn, Args&&... args) -> typename std::enable_if<
54  decltype(fn(args...))>::type {
55  return fn(std::forward<Args>(args)...);
56 }
57 
58 template <
59  typename Function, typename... Args,
60  typename = typename std::enable_if<or_reduce<
61  false, madness::is_future<std::decay_t<Args>>::value...>::value>::type>
62 auto invoke(Function&& fn, Args&&... args) {
63  return TiledArray::get_default_world().taskq.add(std::forward<Function>(fn),
64  std::forward<Args>(args)...);
65 }
66 
67 } // namespace meta
68 } // namespace TiledArray
69 
70 #endif // SRC_TILEDARRAY_META_H_
static constexpr bool value
Definition: meta.h:40
World & get_default_world()
Definition: madness.h:90
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