\doxysection{temporary\+\_\+clone.\+hpp}
\hypertarget{temporary__clone_8hpp_source}{}\label{temporary__clone_8hpp_source}\index{ginkgo/core/base/temporary\_clone.hpp@{ginkgo/core/base/temporary\_clone.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{//\ SPDX-\/FileCopyrightText:\ 2017\ -\/\ 2024\ The\ Ginkgo\ authors}}
\DoxyCodeLine{00002\ \textcolor{comment}{//}}
\DoxyCodeLine{00003\ \textcolor{comment}{//\ SPDX-\/License-\/Identifier:\ BSD-\/3-\/Clause}}
\DoxyCodeLine{00004\ }
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#ifndef\ GKO\_PUBLIC\_CORE\_BASE\_TEMPORARY\_CLONE\_HPP\_}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#define\ GKO\_PUBLIC\_CORE\_BASE\_TEMPORARY\_CLONE\_HPP\_}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ }
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#include\ <functional>}}
\DoxyCodeLine{00010\ \textcolor{preprocessor}{\#include\ <memory>}}
\DoxyCodeLine{00011\ \textcolor{preprocessor}{\#include\ <type\_traits>}}
\DoxyCodeLine{00012\ }
\DoxyCodeLine{00013\ \textcolor{preprocessor}{\#include\ <ginkgo/core/base/exception\_helpers.hpp>}}
\DoxyCodeLine{00014\ \textcolor{preprocessor}{\#include\ <ginkgo/core/base/executor.hpp>}}
\DoxyCodeLine{00015\ \textcolor{preprocessor}{\#include\ <ginkgo/core/base/utils\_helper.hpp>}}
\DoxyCodeLine{00016\ }
\DoxyCodeLine{00017\ }
\DoxyCodeLine{00018\ \textcolor{keyword}{namespace\ }\doxymbox{\hyperlink{namespacegko}{gko}}\ \{}
\DoxyCodeLine{00019\ \textcolor{keyword}{namespace\ }detail\ \{}
\DoxyCodeLine{00020\ }
\DoxyCodeLine{00021\ }
\DoxyCodeLine{00036\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00037\ \textcolor{keyword}{class\ }copy\_back\_deleter\ \{}
\DoxyCodeLine{00038\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00039\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ T*;}
\DoxyCodeLine{00040\ }
\DoxyCodeLine{00047\ \ \ \ \ copy\_back\_deleter(pointer\ original)\ :\ original\_\{original\}\ \{\}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00054\ \ \ \ \ \textcolor{keywordtype}{void}\ operator()(pointer\ ptr)\textcolor{keyword}{\ const}}
\DoxyCodeLine{00055\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00056\ \ \ \ \ \ \ \ \ original\_-\/>copy\_from(ptr);}
\DoxyCodeLine{00057\ \ \ \ \ \ \ \ \ \textcolor{keyword}{delete}\ ptr;}
\DoxyCodeLine{00058\ \ \ \ \ \}}
\DoxyCodeLine{00059\ }
\DoxyCodeLine{00060\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00061\ \ \ \ \ pointer\ original\_;}
\DoxyCodeLine{00062\ \};}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ \textcolor{comment}{//\ specialization\ for\ constant\ objects,\ no\ need\ to\ copy\ back\ something\ that}}
\DoxyCodeLine{00065\ \textcolor{comment}{//\ cannot\ change}}
\DoxyCodeLine{00066\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00067\ \textcolor{keyword}{class\ }copy\_back\_deleter<const\ T>\ \{}
\DoxyCodeLine{00068\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ \textcolor{keyword}{const}\ T*;}
\DoxyCodeLine{00070\ \ \ \ \ copy\_back\_deleter(pointer\ original)\ :\ original\_\{original\}\ \{\}}
\DoxyCodeLine{00071\ }
\DoxyCodeLine{00072\ \ \ \ \ \textcolor{keywordtype}{void}\ operator()(pointer\ ptr)\textcolor{keyword}{\ const\ }\{\ \textcolor{keyword}{delete}\ ptr;\ \}}
\DoxyCodeLine{00073\ }
\DoxyCodeLine{00074\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00075\ \ \ \ \ pointer\ original\_;}
\DoxyCodeLine{00076\ \};}
\DoxyCodeLine{00077\ }
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \textcolor{comment}{//\ specialization\ for\ copy\ back\ via\ assignment}}
\DoxyCodeLine{00080\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00081\ \textcolor{keyword}{class\ }copy\_back\_deleter\_from\_assignment\ \{}
\DoxyCodeLine{00082\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ T*;}
\DoxyCodeLine{00084\ }
\DoxyCodeLine{00091\ \ \ \ \ copy\_back\_deleter\_from\_assignment(pointer\ original)\ :\ original\_\{original\}\ \{\}}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00098\ \ \ \ \ \textcolor{keywordtype}{void}\ operator()(pointer\ ptr)\textcolor{keyword}{\ const}}
\DoxyCodeLine{00099\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ *original\_\ =\ *ptr;}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \textcolor{keyword}{delete}\ ptr;}
\DoxyCodeLine{00102\ \ \ \ \ \}}
\DoxyCodeLine{00103\ }
\DoxyCodeLine{00104\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00105\ \ \ \ \ pointer\ original\_;}
\DoxyCodeLine{00106\ \};}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ }
\DoxyCodeLine{00109\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00110\ \textcolor{keyword}{struct\ }temporary\_clone\_helper\ \{}
\DoxyCodeLine{00111\ \ \ \ \ \textcolor{keyword}{static}\ std::unique\_ptr<T>\ create(std::shared\_ptr<const\ Executor>\ exec,}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ T*\ ptr,\ \textcolor{keywordtype}{bool})}
\DoxyCodeLine{00113\ \ \ \ \ \{}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{gko::clone}}(std::move(exec),\ ptr);}
\DoxyCodeLine{00115\ \ \ \ \ \}}
\DoxyCodeLine{00116\ \};}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ }
\DoxyCodeLine{00130\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00131\ \textcolor{keyword}{class\ }temporary\_clone\ \{}
\DoxyCodeLine{00132\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00133\ \ \ \ \ \textcolor{keyword}{using\ }value\_type\ =\ T;}
\DoxyCodeLine{00134\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ T*;}
\DoxyCodeLine{00135\ }
\DoxyCodeLine{00144\ \ \ \ \ \textcolor{keyword}{explicit}\ temporary\_clone(std::shared\_ptr<const\ Executor>\ exec,}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ptr\_param<T>\ ptr,\ \textcolor{keywordtype}{bool}\ copy\_data\ =\ \textcolor{keyword}{true})}
\DoxyCodeLine{00146\ \ \ \ \ \{}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (ptr-\/>get\_executor()-\/>memory\_accessible(exec))\ \{}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ just\ use\ the\ object\ we\ already\ have}}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \ \ \ \ handle\_\ =\ handle\_type(ptr.get(),\ null\_deleter<T>());}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ clone\ the\ object\ to\ the\ new\ executor\ and\ make\ sure\ it's\ copied}}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ back\ before\ we\ delete\ it}}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \ \ \ \ handle\_\ =\ handle\_type(temporary\_clone\_helper<T>::create(}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::move(exec),\ ptr.get(),\ copy\_data)}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .release(),}
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ copy\_back\_deleter<T>(ptr.get()));}
\DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00158\ \ \ \ \ \}}
\DoxyCodeLine{00159\ }
\DoxyCodeLine{00165\ \ \ \ \ T*\ get()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ handle\_.get();\ \}}
\DoxyCodeLine{00166\ }
\DoxyCodeLine{00172\ \ \ \ \ T*\ operator-\/>()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ handle\_.get();\ \}}
\DoxyCodeLine{00173\ }
\DoxyCodeLine{00179\ \ \ \ \ T\&\ operator*()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ *handle\_;\ \}}
\DoxyCodeLine{00180\ }
\DoxyCodeLine{00181\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00182\ \ \ \ \ \textcolor{comment}{//\ std::function\ deleter\ allows\ to\ decide\ the\ (type\ of)\ deleter\ at\ runtime}}
\DoxyCodeLine{00183\ \ \ \ \ \textcolor{keyword}{using\ }handle\_type\ =\ std::unique\_ptr<T,\ std::function<void(T*)>>;}
\DoxyCodeLine{00184\ }
\DoxyCodeLine{00185\ \ \ \ \ handle\_type\ handle\_;}
\DoxyCodeLine{00186\ \};}
\DoxyCodeLine{00187\ }
\DoxyCodeLine{00188\ }
\DoxyCodeLine{00189\ \}\ \ \textcolor{comment}{//\ namespace\ detail}}
\DoxyCodeLine{00190\ }
\DoxyCodeLine{00191\ }
\DoxyCodeLine{00192\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00193\ \textcolor{keyword}{struct\ }\doxymbox{\hyperlink{structgko_1_1err}{err}}\ \{\};}
\DoxyCodeLine{00194\ }
\DoxyCodeLine{00207\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Ptr>}
\DoxyCodeLine{00208\ detail::temporary\_clone<detail::pointee<Ptr>>\ \doxymbox{\hyperlink{namespacegko_adf2f8602dc9453752a787b40bbf13fcf}{make\_temporary\_clone}}(}
\DoxyCodeLine{00209\ \ \ \ \ std::shared\_ptr<const\ Executor>\ exec,\ Ptr\&\&\ ptr)}
\DoxyCodeLine{00210\ \{}
\DoxyCodeLine{00211\ \ \ \ \ \textcolor{keyword}{using\ }T\ =\ detail::pointee<Ptr>;}
\DoxyCodeLine{00212\ \ \ \ \ \textcolor{keywordflow}{return}\ detail::temporary\_clone<T>(std::move(exec),\ std::forward<Ptr>(ptr));}
\DoxyCodeLine{00213\ \}}
\DoxyCodeLine{00214\ }
\DoxyCodeLine{00215\ }
\DoxyCodeLine{00230\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Ptr>}
\DoxyCodeLine{00231\ detail::temporary\_clone<detail::pointee<Ptr>>\ \doxymbox{\hyperlink{namespacegko_a64c4f7d1a2e86dafc644d9d96f7f4514}{make\_temporary\_output\_clone}}(}
\DoxyCodeLine{00232\ \ \ \ \ std::shared\_ptr<const\ Executor>\ exec,\ Ptr\&\&\ ptr)}
\DoxyCodeLine{00233\ \{}
\DoxyCodeLine{00234\ \ \ \ \ \textcolor{keyword}{using\ }T\ =\ detail::pointee<Ptr>;}
\DoxyCodeLine{00235\ \ \ \ \ \textcolor{keyword}{static\_assert}(}
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ !std::is\_const<T>::value,}
\DoxyCodeLine{00237\ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}make\_temporary\_output\_clone\ should\ only\ be\ used\ on\ non-\/const\ objects"{}});}
\DoxyCodeLine{00238\ \ \ \ \ \textcolor{keywordflow}{return}\ detail::temporary\_clone<T>(std::move(exec),\ std::forward<Ptr>(ptr),}
\DoxyCodeLine{00239\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{false});}
\DoxyCodeLine{00240\ \}}
\DoxyCodeLine{00241\ }
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \}\ \ \textcolor{comment}{//\ namespace\ gko}}
\DoxyCodeLine{00244\ }
\DoxyCodeLine{00245\ }
\DoxyCodeLine{00246\ \textcolor{preprocessor}{\#endif\ \ }\textcolor{comment}{//\ GKO\_PUBLIC\_CORE\_BASE\_TEMPORARY\_CLONE\_HPP\_}}

\end{DoxyCode}
