timemory 3.3.0
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.
copy.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/copy.hpp
27 * \brief Definition for various functions for copy in operations
28 */
29
30#pragma once
31
36
37#include <memory>
38
39namespace tim
40{
41namespace operation
42{
43//
44//--------------------------------------------------------------------------------------//
45//
46///
47/// \struct tim::operation::copy
48/// \brief This operation class is used for copying the object generically
49///
50//
51//--------------------------------------------------------------------------------------//
52//
53template <typename Tp>
54struct copy
55{
56 using type = Tp;
57
58 TIMEMORY_DEFAULT_OBJECT(copy)
59
60 copy(Tp& obj, const Tp& rhs) { (*this)(obj, rhs); }
61 copy(Tp*& obj, const Tp* rhs) { (*this)(obj, rhs); }
62 copy(Tp*& obj, const std::shared_ptr<Tp>& rhs) { (*this)(obj, rhs.get()); }
63 copy(Tp*& obj, const std::unique_ptr<Tp>& rhs) { (*this)(obj, rhs.get()); }
64
65 template <typename Up = Tp>
66 TIMEMORY_INLINE auto operator()(Tp& obj, Up&& v) const
67 {
68 return sfinae(obj, std::forward<Up>(v));
69 }
70
71 template <typename Up = Tp>
72 TIMEMORY_INLINE auto operator()(Tp*& obj, Up&& v) const
73 {
74 return sfinae(obj, std::forward<Up>(v));
75 }
76
77private:
78 template <typename Up, typename Dp = decay_t<Up>>
79 TIMEMORY_INLINE Tp& sfinae(Tp& obj, Up&& rhs,
81 !std::is_pointer<decay_t<Dp>>::value,
82 int> = 0) const;
83
84 template <typename Up, typename Dp = decay_t<Up>>
85 TIMEMORY_INLINE Tp* sfinae(
86 Tp*& obj, Up&& rhs,
88 long> = 0) const;
89
90 template <typename Up, typename Dp = decay_t<Up>>
91 TIMEMORY_INLINE auto sfinae(
92 Tp&, Up&&, enable_if_t<!trait::is_available<Dp>::value, int> = 0) const
93 {}
94
95 template <typename Up, typename Dp = decay_t<Up>>
96 TIMEMORY_INLINE auto sfinae(
97 Tp*&, Up&&, enable_if_t<!trait::is_available<Dp>::value, int> = 0) const
98 {}
99};
100//
101template <typename Tp>
102template <typename Up, typename Dp>
103Tp&
104copy<Tp>::sfinae(
105 Tp& obj, Up&& rhs,
106 enable_if_t<trait::is_available<Dp>::value && !std::is_pointer<decay_t<Dp>>::value,
107 int>) const
108{
109 obj = Up{ std::forward<Up>(rhs) };
110 operation::set_iterator<Tp>{}(obj, nullptr);
111 return obj;
112}
113
114template <typename Tp>
115template <typename Up, typename Dp>
116Tp*
117copy<Tp>::sfinae(
118 Tp*& obj, Up&& rhs,
119 enable_if_t<trait::is_available<Dp>::value && std::is_pointer<decay_t<Dp>>::value,
120 long>) const
121{
122 if(rhs)
123 {
124 if(!obj)
125 {
126 obj = new type{ *std::forward<Up>(rhs) };
127 }
128 else
129 {
130 *obj = type{ *std::forward<Up>(rhs) };
131 }
132 operation::set_iterator<Tp>{}(*obj, nullptr);
133 }
134 return obj;
135}
136//
137//--------------------------------------------------------------------------------------//
138//
139} // namespace operation
140} // namespace tim
Definition: kokkosp.cpp:39
typename std::decay< T >::type decay_t
Alias template for decay.
Definition: types.hpp:194
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
This operation class is used for copying the object generically.
Definition: copy.hpp:55
copy(Tp *&obj, const Tp *rhs)
Definition: copy.hpp:61
auto operator()(Tp *&obj, Up &&v) const
Definition: copy.hpp:72
copy(Tp *&obj, const std::unique_ptr< Tp > &rhs)
Definition: copy.hpp:63
auto operator()(Tp &obj, Up &&v) const
Definition: copy.hpp:66
copy(Tp *&obj, const std::shared_ptr< Tp > &rhs)
Definition: copy.hpp:62
trait that signifies that an implementation for the component is available. When this is set to false...
Definition: types.hpp:355