\doxysection{temporary\+\_\+conversion.\+hpp}
\hypertarget{temporary__conversion_8hpp_source}{}\label{temporary__conversion_8hpp_source}\index{ginkgo/core/base/temporary\_conversion.hpp@{ginkgo/core/base/temporary\_conversion.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\_CONVERSION\_HPP\_}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#define\ GKO\_PUBLIC\_CORE\_BASE\_TEMPORARY\_CONVERSION\_HPP\_}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ }
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#include\ <memory>}}
\DoxyCodeLine{00010\ \textcolor{preprocessor}{\#include\ <tuple>}}
\DoxyCodeLine{00011\ \textcolor{preprocessor}{\#include\ <type\_traits>}}
\DoxyCodeLine{00012\ }
\DoxyCodeLine{00013\ \textcolor{preprocessor}{\#include\ <ginkgo/core/base/lin\_op.hpp>}}
\DoxyCodeLine{00014\ \textcolor{preprocessor}{\#include\ <ginkgo/core/base/utils.hpp>}}
\DoxyCodeLine{00015\ }
\DoxyCodeLine{00016\ }
\DoxyCodeLine{00017\ \textcolor{keyword}{namespace\ }\doxymbox{\hyperlink{namespacegko}{gko}}\ \{}
\DoxyCodeLine{00018\ \textcolor{keyword}{namespace\ }detail\ \{}
\DoxyCodeLine{00019\ }
\DoxyCodeLine{00020\ }
\DoxyCodeLine{00039\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ CopyType,\ \textcolor{keyword}{typename}\ OrigType>}
\DoxyCodeLine{00040\ \textcolor{keyword}{class\ }convert\_back\_deleter\ \{}
\DoxyCodeLine{00041\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00042\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ CopyType*;}
\DoxyCodeLine{00043\ \ \ \ \ \textcolor{keyword}{using\ }original\_pointer\ =\ OrigType*;}
\DoxyCodeLine{00044\ }
\DoxyCodeLine{00051\ \ \ \ \ convert\_back\_deleter(original\_pointer\ original)\ :\ original\_\{original\}\ \{\}}
\DoxyCodeLine{00052\ }
\DoxyCodeLine{00058\ \ \ \ \ \textcolor{keywordtype}{void}\ operator()(pointer\ ptr)\textcolor{keyword}{\ const}}
\DoxyCodeLine{00059\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ ptr-\/>convert\_to(original\_);}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keyword}{delete}\ ptr;}
\DoxyCodeLine{00062\ \ \ \ \ \}}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00065\ \ \ \ \ original\_pointer\ original\_;}
\DoxyCodeLine{00066\ \};}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00068\ \textcolor{comment}{//\ specialization\ for\ constant\ objects,\ no\ need\ to\ convert\ back\ something\ that}}
\DoxyCodeLine{00069\ \textcolor{comment}{//\ cannot\ change}}
\DoxyCodeLine{00070\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ CopyType,\ \textcolor{keyword}{typename}\ OrigType>}
\DoxyCodeLine{00071\ \textcolor{keyword}{class\ }convert\_back\_deleter<const\ CopyType,\ const\ OrigType>\ \{}
\DoxyCodeLine{00072\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ \textcolor{keyword}{const}\ CopyType*;}
\DoxyCodeLine{00074\ \ \ \ \ \textcolor{keyword}{using\ }original\_pointer\ =\ \textcolor{keyword}{const}\ OrigType*;}
\DoxyCodeLine{00075\ \ \ \ \ convert\_back\_deleter(original\_pointer)\ \{\}}
\DoxyCodeLine{00076\ }
\DoxyCodeLine{00077\ \ \ \ \ \textcolor{keywordtype}{void}\ operator()(pointer\ ptr)\textcolor{keyword}{\ const\ }\{\ \textcolor{keyword}{delete}\ ptr;\ \}}
\DoxyCodeLine{00078\ \};}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00090\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ TargetType>}
\DoxyCodeLine{00091\ \textcolor{keyword}{struct\ }conversion\_target\_helper\ \{}
\DoxyCodeLine{00099\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ SourceType,}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{typename}\ =\ std::enable\_if\_t<std::is\_base\_of<}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ConvertibleTo<TargetType>,\ SourceType>::value>>}
\DoxyCodeLine{00102\ \ \ \ \ \textcolor{keyword}{static}\ std::unique\_ptr<TargetType>\ create\_empty(\textcolor{keyword}{const}\ SourceType*\ source)}
\DoxyCodeLine{00103\ \ \ \ \ \{}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ TargetType::create(source-\/>get\_executor());}
\DoxyCodeLine{00105\ \ \ \ \ \}}
\DoxyCodeLine{00106\ \};}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ }
\DoxyCodeLine{00119\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}...\ ConversionCandidates>}
\DoxyCodeLine{00120\ \textcolor{keyword}{struct\ }conversion\_helper\ \{}
\DoxyCodeLine{00122\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ TargetType,\ \textcolor{keyword}{typename}\ MaybeConstLinOp>}
\DoxyCodeLine{00123\ \ \ \ \ \textcolor{keyword}{static}\ std::unique\_ptr<TargetType,\ std::function<void(TargetType*)>>}
\DoxyCodeLine{00124\ \ \ \ \ convert(MaybeConstLinOp*\ obj)}
\DoxyCodeLine{00125\ \ \ \ \ \{}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ convert\_impl<TargetType,\ MaybeConstLinOp,}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ConversionCandidates...>(obj);}
\DoxyCodeLine{00128\ \ \ \ \ \}}
\DoxyCodeLine{00129\ }
\DoxyCodeLine{00135\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ TargetType,\ \textcolor{keyword}{typename}\ MaybeConstLinOp,}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{typename}\ FirstCandidate,\ \textcolor{keyword}{typename}...\ TrailingCandidates>}
\DoxyCodeLine{00137\ \ \ \ \ \textcolor{keyword}{static}\ std::unique\_ptr<TargetType,\ std::function<void(TargetType*)>>}
\DoxyCodeLine{00138\ \ \ \ \ convert\_impl(MaybeConstLinOp*\ obj)}
\DoxyCodeLine{00139\ \ \ \ \ \{}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ make\ candidate\_type\ conditionally\ const\ based\ on\ whether\ obj\ is\ const}}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }candidate\_type\ =}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \ \ \ \ std::conditional\_t<std::is\_const<MaybeConstLinOp>::value,}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ FirstCandidate,\ FirstCandidate>;}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ candidate\_type*\ cast\_obj\{\};}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((cast\_obj\ =\ \textcolor{keyword}{dynamic\_cast<}candidate\_type*\textcolor{keyword}{>}(obj)))\ \{}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ if\ the\ cast\ is\ successful,\ obj\ is\ of\ dynamic\ type\ candidate\_type}}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ so\ we\ can\ convert\ from\ this\ type\ to\ TargetType}}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ converted\ =\ conversion\_target\_helper<}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::remove\_cv\_t<TargetType>>::create\_empty(cast\_obj);}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \ \ \ \ cast\_obj-\/>convert\_to(converted);}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ Make\ sure\ ConvertibleTo<TargetType>\ is\ available\ and\ symmetric}}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_assert}(}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::is\_base\_of<ConvertibleTo<std::remove\_cv\_t<TargetType>>,}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ FirstCandidate>::value,}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}ConvertibleTo\ not\ implemented"{}});}
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_assert}(std::is\_base\_of<ConvertibleTo<FirstCandidate>,}
\DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ TargetType>::value,}
\DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}ConvertibleTo\ not\ symmetric"{}});}
\DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \{converted.release(),}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ convert\_back\_deleter<TargetType,\ candidate\_type>\{cast\_obj\}\};}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ else\ try\ the\ remaining\ candidates}}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ conversion\_helper<TrailingCandidates...>::template\ convert<}
\DoxyCodeLine{00164\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ TargetType>(obj);}
\DoxyCodeLine{00165\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00166\ \ \ \ \ \}}
\DoxyCodeLine{00167\ \};}
\DoxyCodeLine{00168\ }
\DoxyCodeLine{00169\ \textcolor{keyword}{template}\ <>}
\DoxyCodeLine{00170\ \textcolor{keyword}{struct\ }conversion\_helper<>\ \{}
\DoxyCodeLine{00171\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T,\ \textcolor{keyword}{typename}\ MaybeConstLinOp>}
\DoxyCodeLine{00172\ \ \ \ \ \textcolor{keyword}{static}\ std::unique\_ptr<T,\ std::function<void(T*)>>\ convert(}
\DoxyCodeLine{00173\ \ \ \ \ \ \ \ \ MaybeConstLinOp*\ obj)}
\DoxyCodeLine{00174\ \ \ \ \ \{}
\DoxyCodeLine{00175\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ return\ nullptr\ if\ no\ previous\ candidates\ matched}}
\DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \{\textcolor{keyword}{nullptr},\ null\_deleter<T>\{\}\};}
\DoxyCodeLine{00177\ \ \ \ \ \}}
\DoxyCodeLine{00178\ \};}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ }
\DoxyCodeLine{00193\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ T>}
\DoxyCodeLine{00194\ \textcolor{keyword}{class\ }temporary\_conversion\ \{}
\DoxyCodeLine{00195\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00196\ \ \ \ \ \textcolor{keyword}{using\ }value\_type\ =\ T;}
\DoxyCodeLine{00197\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ =\ T*;}
\DoxyCodeLine{00198\ \ \ \ \ \textcolor{keyword}{using\ }lin\_op\_type\ =}
\DoxyCodeLine{00199\ \ \ \ \ \ \ \ \ std::conditional\_t<std::is\_const<T>::value,\ \textcolor{keyword}{const}\ LinOp,\ LinOp>;}
\DoxyCodeLine{00200\ }
\DoxyCodeLine{00207\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}...\ ConversionCandidates>}
\DoxyCodeLine{00208\ \ \ \ \ \textcolor{keyword}{static}\ temporary\_conversion\ create(ptr\_param<lin\_op\_type>\ ptr)}
\DoxyCodeLine{00209\ \ \ \ \ \{}
\DoxyCodeLine{00210\ \ \ \ \ \ \ \ \ T*\ cast\_ptr\{\};}
\DoxyCodeLine{00211\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((cast\_ptr\ =\ \textcolor{keyword}{dynamic\_cast<}T*\textcolor{keyword}{>}(ptr.get())))\ \{}
\DoxyCodeLine{00212\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ handle\_type\{cast\_ptr,\ null\_deleter<T>\{\}\};}
\DoxyCodeLine{00213\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00214\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ conversion\_helper<ConversionCandidates...>::template\ convert<}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ T>(ptr.get());}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00217\ \ \ \ \ \}}
\DoxyCodeLine{00218\ }
\DoxyCodeLine{00224\ \ \ \ \ T*\ get()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ handle\_.get();\ \}}
\DoxyCodeLine{00225\ }
\DoxyCodeLine{00231\ \ \ \ \ T*\ operator-\/>()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ handle\_.get();\ \}}
\DoxyCodeLine{00232\ }
\DoxyCodeLine{00236\ \ \ \ \ \textcolor{keyword}{explicit}\ \textcolor{keyword}{operator}\ bool()\ \{\ \textcolor{keywordflow}{return}\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{bool}\textcolor{keyword}{>}(handle\_);\ \}}
\DoxyCodeLine{00237\ }
\DoxyCodeLine{00238\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00239\ \ \ \ \ \textcolor{comment}{//\ std::function\ deleter\ allows\ to\ decide\ the\ (type\ of)\ deleter\ at}}
\DoxyCodeLine{00240\ \ \ \ \ \textcolor{comment}{//\ runtime}}
\DoxyCodeLine{00241\ \ \ \ \ \textcolor{keyword}{using\ }handle\_type\ =\ std::unique\_ptr<T,\ std::function<void(T*)>>;}
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \ \ \ \ temporary\_conversion(handle\_type\ handle)\ :\ handle\_\{std::move(handle)\}\ \{\}}
\DoxyCodeLine{00244\ }
\DoxyCodeLine{00245\ \ \ \ \ handle\_type\ handle\_;}
\DoxyCodeLine{00246\ \};}
\DoxyCodeLine{00247\ }
\DoxyCodeLine{00248\ }
\DoxyCodeLine{00249\ \}\ \ \textcolor{comment}{//\ namespace\ detail}}
\DoxyCodeLine{00250\ \}\ \ \textcolor{comment}{//\ namespace\ gko}}
\DoxyCodeLine{00251\ }
\DoxyCodeLine{00252\ }
\DoxyCodeLine{00253\ \textcolor{preprocessor}{\#endif\ \ }\textcolor{comment}{//\ GKO\_PUBLIC\_CORE\_BASE\_TEMPORARY\_CONVERSION\_HPP\_}}

\end{DoxyCode}
