5 #ifndef GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_
6 #define GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_
11 #include <type_traits>
14 #include <ginkgo/core/base/exception_helpers.hpp>
15 #include <ginkgo/core/base/executor.hpp>
16 #include <ginkgo/core/base/utils_helper.hpp>
38 class copy_back_deleter {
48 copy_back_deleter(pointer original) : original_{original} {}
55 void operator()(pointer ptr)
const
57 original_->copy_from(ptr);
68 class copy_back_deleter<const T> {
70 using pointer =
const T*;
71 copy_back_deleter(pointer original) : original_{original} {}
73 void operator()(pointer ptr)
const {
delete ptr; }
82 class copy_back_deleter_from_assignment {
92 copy_back_deleter_from_assignment(pointer original) : original_{original} {}
99 void operator()(pointer ptr)
const
110 template <
typename T>
111 struct temporary_clone_helper {
112 static std::unique_ptr<T> create(std::shared_ptr<const Executor> exec,
131 template <
typename T>
132 class temporary_clone {
134 using value_type = T;
145 explicit temporary_clone(std::shared_ptr<const Executor> exec,
146 ptr_param<T> ptr,
bool copy_data =
true)
148 if (ptr->get_executor()->memory_accessible(exec)) {
150 handle_ = handle_type(ptr.get(), null_deleter<T>());
154 handle_ = handle_type(temporary_clone_helper<T>::create(
155 std::move(exec), ptr.get(), copy_data)
157 copy_back_deleter<T>(ptr.get()));
166 T* get()
const {
return handle_.get(); }
173 T* operator->()
const {
return handle_.get(); }
180 T& operator*()
const {
return *handle_; }
184 using handle_type = std::unique_ptr<T, std::function<void(T*)>>;
193 template <
typename T>
208 template <
typename Ptr>
210 std::shared_ptr<const Executor> exec, Ptr&& ptr)
212 using T = detail::pointee<Ptr>;
213 return detail::temporary_clone<T>(std::move(exec), std::forward<Ptr>(ptr));
231 template <
typename Ptr>
233 std::shared_ptr<const Executor> exec, Ptr&& ptr)
235 using T = detail::pointee<Ptr>;
237 !std::is_const<T>::value,
238 "make_temporary_output_clone should only be used on non-const objects");
239 return detail::temporary_clone<T>(std::move(exec), std::forward<Ptr>(ptr),
247 #endif // GKO_PUBLIC_CORE_BASE_TEMPORARY_CLONE_HPP_