5 #ifndef GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_
6 #define GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_
11 #include <type_traits>
13 #include <ginkgo/core/base/exception_helpers.hpp>
14 #include <ginkgo/core/base/executor.hpp>
15 #include <ginkgo/core/base/utils_helper.hpp>
37 class copy_back_deleter {
47 copy_back_deleter(pointer original) : original_{original} {}
54 void operator()(pointer ptr)
const
56 original_->copy_from(ptr);
67 class copy_back_deleter<const T> {
69 using pointer =
const T*;
70 copy_back_deleter(pointer original) : original_{original} {}
72 void operator()(pointer ptr)
const {
delete ptr; }
81 class copy_back_deleter_from_assignment {
91 copy_back_deleter_from_assignment(pointer original) : original_{original} {}
98 void operator()(pointer ptr)
const
109 template <
typename T>
110 struct temporary_clone_helper {
111 static std::unique_ptr<T> create(std::shared_ptr<const Executor> exec,
130 template <
typename T>
131 class temporary_clone {
133 using value_type = T;
144 explicit temporary_clone(std::shared_ptr<const Executor> exec,
145 ptr_param<T> ptr,
bool copy_data =
true)
147 if (ptr->get_executor()->memory_accessible(exec)) {
149 handle_ = handle_type(ptr.get(), null_deleter<T>());
153 handle_ = handle_type(temporary_clone_helper<T>::create(
154 std::move(exec), ptr.get(), copy_data)
156 copy_back_deleter<T>(ptr.get()));
165 T* get()
const {
return handle_.get(); }
172 T* operator->()
const {
return handle_.get(); }
179 T& operator*()
const {
return *handle_; }
183 using handle_type = std::unique_ptr<T, std::function<void(T*)>>;
192 template <
typename T>
207 template <
typename Ptr>
209 std::shared_ptr<const Executor> exec, Ptr&& ptr)
211 using T = detail::pointee<Ptr>;
212 return detail::temporary_clone<T>(std::move(exec), std::forward<Ptr>(ptr));
230 template <
typename Ptr>
232 std::shared_ptr<const Executor> exec, Ptr&& ptr)
234 using T = detail::pointee<Ptr>;
236 !std::is_const<T>::value,
237 "make_temporary_output_clone should only be used on non-const objects");
238 return detail::temporary_clone<T>(std::move(exec), std::forward<Ptr>(ptr),
246 #endif // GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_