timemory  3.2.1
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
derive.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020, The Regents of the University of California,
4 // through Lawrence Berkeley National Laboratory (subject to receipt of any
5 // required approvals from the U.S. Dept. of Energy). All rights reserved.
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 /**
26  * \file timemory/operations/types/derive.hpp
27  * \brief Definition for various functions for derive in operations
28  */
29 
30 #pragma once
31 
36 
37 namespace tim
38 {
39 namespace operation
40 {
41 //
42 //--------------------------------------------------------------------------------------//
43 //
44 //
45 //
46 //--------------------------------------------------------------------------------------//
47 //
48 template <typename Tp>
49 struct fold_derive;
50 
51 template <template <typename...> class CompT, typename... C>
52 struct fold_derive<CompT<C...>>
53 {
54 public:
56 
57  template <typename Up, typename Arg, typename... Args>
58  fold_derive(bool& b, Up& obj, Arg&& arg, Args&&...)
59  {
60  if(!b)
61  sfinae(b, obj, 0, std::forward<Arg>(arg));
62  }
63 
64 private:
65  // satisfies mpl condition and accepts arguments
66  template <typename Up, typename Arg>
67  auto sfinae(bool& b, Up& obj, int, Arg&& arg)
68  -> decltype(obj.derive(arg.template get_component<C>()...), void())
69  {
70  b = obj.derive(arg.template get_component<C>()...);
71  }
72 
73  template <typename Up, typename Arg>
74  void sfinae(bool&, Up&, long, Arg&&)
75  {}
76 };
77 //
78 //--------------------------------------------------------------------------------------//
79 //
80 //
81 //
82 //--------------------------------------------------------------------------------------//
83 //
84 template <typename Tp>
85 struct derive
86 {
87  using type = Tp;
88 
90 
91 private:
92  using derived_tuple_t = typename trait::derivation_types<Tp>::type;
93  static constexpr size_t derived_tuple_v = std::tuple_size<derived_tuple_t>::value;
94  template <size_t Idx>
95  using derived_t = typename std::tuple_element<Idx, derived_tuple_t>::type;
96 
97 public:
98  template <typename... Args>
99  explicit derive(type& obj, Args&&... args);
100 
101  template <typename Arg, size_t N = derived_tuple_v, std::enable_if_t<N != 0> = 0>
102  explicit derive(type& obj, Arg&& arg)
103  {
104  bool b = false;
105  sfinae(b, obj, make_index_sequence<N>{}, std::forward<Arg>(arg));
106  if(!b)
107  sfinae(obj, 0, 0, std::forward<Arg>(arg));
108  }
109 
110  template <template <typename...> class BundleT, typename... Args>
111  explicit derive(type& obj, BundleT<Args...>& arg)
112  {
113  bool b = false;
114  constexpr auto N = derived_tuple_v;
115  sfinae(b, obj, make_index_sequence<N>{}, arg);
116  if(!b)
117  sfinae(obj, 0, 0, arg);
118  }
119 
120 private:
121  // satisfies mpl condition and accepts arguments
122  template <typename Up, size_t... Idx, typename... Args>
123  auto sfinae(bool& b, Up& obj, index_sequence<Idx...>, Args&&... args)
124  {
126  fold_derive<derived_t<Idx>>(b, obj, std::forward<Args>(args)...));
127  }
128 
129 private:
130  // satisfies mpl condition and accepts arguments
131  template <typename Up, typename... Args>
132  auto sfinae(Up& obj, int, int, Args&&... args)
133  -> decltype(obj.derive(std::forward<Args>(args)...), void())
134  {
135  obj.derive(std::forward<Args>(args)...);
136  }
137 
138  // satisfies mpl condition but does not accept arguments
139  template <typename Up, typename... Args>
140  auto sfinae(Up& obj, int, long, Args&&...) -> decltype(obj.derive(), void())
141  {
142  obj.derive();
143  }
144 
145  // no member function or does not satisfy mpl condition
146  template <typename Up, typename... Args>
147  void sfinae(Up&, long, long, Args&&...)
148  {
149  // SFINAE_WARNING(type);
150  }
151 };
152 //
153 //--------------------------------------------------------------------------------------//
154 //
155 template <typename Tp>
156 template <typename... Args>
157 derive<Tp>::derive(type& obj, Args&&... args)
158 {
159  sfinae(obj, 0, 0, std::forward<Args>(args)...);
160 }
161 //
162 //--------------------------------------------------------------------------------------//
163 //
164 } // namespace operation
165 } // namespace tim
Definition: kokkosp.cpp:38
std::make_integer_sequence< size_t, Num > make_index_sequence
Alias template make_index_sequence.
Definition: types.hpp:182
std::integer_sequence< size_t, Idx... > index_sequence
Alias template index_sequence.
Definition: types.hpp:178
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
typename std::tuple_element< Idx, derived_tuple_t >::type derived_t
Definition: derive.hpp:95
derive(type &obj, Arg &&arg)
Definition: derive.hpp:102
static constexpr TIMEMORY_DELETED_OBJECT(derive) private size_t derived_tuple_v
Definition: derive.hpp:93
derive(type &obj, BundleT< Args... > &arg)
Definition: derive.hpp:111
derive(type &obj, Args &&... args)
Definition: derive.hpp:157
TIMEMORY_DELETED_OBJECT(fold_derive) template< typename Up
std::tuple< type_list<> > type
#define TIMEMORY_DELETED_OBJECT(NAME)
Definition: types.hpp:95
#define TIMEMORY_FOLD_EXPRESSION(...)
Definition: types.hpp:55
typename typename typename
Definition: types.hpp:226