\doxysection{yaml\+\_\+config.\+hpp}
\hypertarget{yaml__config_8hpp_source}{}\label{yaml__config_8hpp_source}\index{ginkgo/extensions/config/yaml\_config.hpp@{ginkgo/extensions/config/yaml\_config.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{//\ SPDX-\/FileCopyrightText:\ 2017\ -\/\ 2025\ 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\_EXTENSIONS\_CONFIG\_JSON\_CONFIG\_HPP\_}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#define\ GKO\_PUBLIC\_EXTENSIONS\_CONFIG\_JSON\_CONFIG\_HPP\_}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ \textcolor{preprocessor}{\#include\ <iostream>}}
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#include\ <stdexcept>}}
\DoxyCodeLine{00010\ \textcolor{preprocessor}{\#include\ <string>}}
\DoxyCodeLine{00011\ }
\DoxyCodeLine{00012\ \textcolor{preprocessor}{\#include\ <yaml-\/cpp/yaml.h>}}
\DoxyCodeLine{00013\ }
\DoxyCodeLine{00014\ \textcolor{preprocessor}{\#include\ <ginkgo/core/config/property\_tree.hpp>}}
\DoxyCodeLine{00015\ }
\DoxyCodeLine{00016\ }
\DoxyCodeLine{00017\ \textcolor{keyword}{namespace\ }\doxymbox{\hyperlink{namespacegko}{gko}}\ \{}
\DoxyCodeLine{00018\ \textcolor{keyword}{namespace\ }ext\ \{}
\DoxyCodeLine{00019\ \textcolor{keyword}{namespace\ }config\ \{}
\DoxyCodeLine{00020\ }
\DoxyCodeLine{00021\ }
\DoxyCodeLine{00026\ \textcolor{keyword}{inline}\ gko::config::pnode\ parse\_yaml(\textcolor{keyword}{const}\ YAML::Node\&\ input)}
\DoxyCodeLine{00027\ \{}
\DoxyCodeLine{00028\ \ \ \ \ \textcolor{keyword}{auto}\ parse\_array\ =\ [](\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ arr)\ \{}
\DoxyCodeLine{00029\ \ \ \ \ \ \ \ \ gko::config::pnode::array\_type\ nodes;}
\DoxyCodeLine{00030\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ it\ :\ arr)\ \{}
\DoxyCodeLine{00031\ \ \ \ \ \ \ \ \ \ \ \ \ nodes.emplace\_back(parse\_yaml(it));}
\DoxyCodeLine{00032\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00033\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{nodes\};}
\DoxyCodeLine{00034\ \ \ \ \ \};}
\DoxyCodeLine{00035\ \ \ \ \ \textcolor{keyword}{auto}\ parse\_map\ =\ [](\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ map)\ \{}
\DoxyCodeLine{00036\ \ \ \ \ \ \ \ \ gko::config::pnode::map\_type\ nodes;}
\DoxyCodeLine{00037\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ use\ []\ to\ get\ override\ behavior}}
\DoxyCodeLine{00038\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ yaml\_item\ :\ map)\ \{}
\DoxyCodeLine{00039\ \ \ \ \ \ \ \ \ \ \ \ \ std::string\ key\ =\ yaml\_item.first.template\ \doxymbox{\hyperlink{namespacegko_ae3e355a655feea026a7c46814d37a9c9}{as<std::string>}}();}
\DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ yaml-\/cpp\ keeps\ the\ alias\ without\ resolving\ it\ when\ parsing.}}
\DoxyCodeLine{00041\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ We\ resolve\ them\ here.}}
\DoxyCodeLine{00042\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (key\ ==\ \textcolor{stringliteral}{"{}<<"{}})\ \{}
\DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ node\ =\ parse\_yaml(yaml\_item.second);}
\DoxyCodeLine{00044\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (node.get\_tag()\ ==\ gko::config::pnode::tag\_t::array)\ \{}
\DoxyCodeLine{00045\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ arr\ :\ node.get\_array())\ \{}
\DoxyCodeLine{00046\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (arr.get\_tag()\ !=\ gko::config::pnode::tag\_t::map)\ \{}
\DoxyCodeLine{00047\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{throw}\ std::runtime\_error(}
\DoxyCodeLine{00048\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}YAML\ only\ accepts\ merge\ key\ <<\ to\ merge\ item\ "{}}}
\DoxyCodeLine{00049\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}from\ map\ not\ "{}}\ +}
\DoxyCodeLine{00050\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ YAML::Dump(yaml\_item.second));}
\DoxyCodeLine{00051\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00052\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ item\ :\ arr.get\_map())\ \{}
\DoxyCodeLine{00053\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nodes[item.first]\ =\ item.second;}
\DoxyCodeLine{00054\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00055\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00056\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (node.get\_tag()\ ==\ gko::config::pnode::tag\_t::map)\ \{}
\DoxyCodeLine{00057\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ item\ :\ node.get\_map())\ \{}
\DoxyCodeLine{00058\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nodes[item.first]\ =\ item.second;}
\DoxyCodeLine{00059\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{throw}\ std::runtime\_error(\textcolor{stringliteral}{"{}can\ not\ handle\ this\ alias:\ "{}}\ +}
\DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ YAML::Dump(yaml\_item.second));}
\DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nodes[key]\ =\ parse\_yaml(yaml\_item.second);}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00068\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{nodes\};}
\DoxyCodeLine{00069\ \ \ \ \ \};}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{comment}{//\ yaml-\/cpp\ does\ not\ have\ type\ check}}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keyword}{auto}\ parse\_data\ =\ [](\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ data)\ \{}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (std::int64\_t\ value;}
\DoxyCodeLine{00073\ \ \ \ \ \ \ \ \ \ \ \ \ YAML::convert<std::int64\_t>::decode(data,\ value))\ \{}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{value\};}
\DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\textcolor{keywordtype}{bool}\ value;\ YAML::convert<bool>::decode(data,\ value))\ \{}
\DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{value\};}
\DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00079\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\textcolor{keywordtype}{double}\ value;\ YAML::convert<double>::decode(data,\ value))\ \{}
\DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{value\};}
\DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00082\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (std::string\ value;}
\DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \ \ \ \ YAML::convert<std::string>::decode(data,\ value))\ \{}
\DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ gko::config::pnode\{value\};}
\DoxyCodeLine{00085\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00086\ \ \ \ \ \ \ \ \ std::string\ content\ =\ YAML::Dump(data);}
\DoxyCodeLine{00087\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{throw}\ std::runtime\_error(}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}property\_tree\ can\ not\ handle\ the\ node\ with\ content:\ "{}}\ +\ content);}
\DoxyCodeLine{00089\ \ \ \ \ \};}
\DoxyCodeLine{00090\ }
\DoxyCodeLine{00091\ \ \ \ \ \textcolor{keywordflow}{if}\ (input.IsSequence())\ \{}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ parse\_array(input);}
\DoxyCodeLine{00093\ \ \ \ \ \}}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordflow}{if}\ (input.IsMap())\ \{}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ parse\_map(input);}
\DoxyCodeLine{00096\ \ \ \ \ \}}
\DoxyCodeLine{00097\ \ \ \ \ \textcolor{keywordflow}{return}\ parse\_data(input);}
\DoxyCodeLine{00098\ \}}
\DoxyCodeLine{00099\ }
\DoxyCodeLine{00100\ }
\DoxyCodeLine{00125\ \textcolor{keyword}{inline}\ gko::config::pnode\ parse\_yaml\_file(std::string\ filename)}
\DoxyCodeLine{00126\ \{}
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordflow}{return}\ parse\_yaml(YAML::LoadFile(filename));}
\DoxyCodeLine{00128\ \}}
\DoxyCodeLine{00129\ }
\DoxyCodeLine{00130\ }
\DoxyCodeLine{00131\ \}\ \ \textcolor{comment}{//\ namespace\ config}}
\DoxyCodeLine{00132\ \}\ \ \textcolor{comment}{//\ namespace\ ext}}
\DoxyCodeLine{00133\ \}\ \ \textcolor{comment}{//\ namespace\ gko}}
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ }
\DoxyCodeLine{00136\ \textcolor{preprocessor}{\#endif\ \ }\textcolor{comment}{//\ GKO\_PUBLIC\_EXTENSIONS\_CONFIG\_JSON\_CONFIG\_HPP\_}}

\end{DoxyCode}
