Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
FieldVariant.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21#include <variant>
22
23namespace shamrock::patch {
24
30 template<template<class> class Container>
32
33 public:
34 using var_t_template = std::variant<
35 Container<f32>,
36 Container<f32_2>,
37 Container<f32_3>,
38 Container<f32_4>,
39 Container<f32_8>,
40 Container<f32_16>,
41 Container<f64>,
42 Container<f64_2>,
43 Container<f64_3>,
44 Container<f64_4>,
45 Container<f64_8>,
46 Container<f64_16>,
47 Container<u32>,
48 Container<u64>,
49 Container<u32_3>,
50 Container<u64_3>,
51 Container<i64_3>,
52 Container<i64>>;
53
54 var_t_template value;
56 template<class T>
57 explicit FieldVariant(Container<T> &&val) : value(std::move(val)) {}
58
59 template<class T>
60 Container<T> &get_if_ref_throw() {
61 if (Container<T> *pval = std::get_if<Container<T>>(&value)) {
62 return *pval;
63 }
65 "the type asked is not correct");
66 }
67
68 template<class Func>
69 void visit(Func &&f) {
70 std::visit(
71 [&](auto &arg) {
72 f(arg);
73 },
74 value);
75 }
76
77 template<class Func>
78 auto visit_return(Func &&f) {
79 return std::visit(
80 [&](auto &arg) {
81 return f(arg);
82 },
83 value);
84 }
85
86 template<class Func>
87 void visit(Func &&f) const {
88 std::visit(
89 [&](auto &arg) {
90 f(arg);
91 },
92 value);
93 }
94
95 template<class Func>
96 auto visit_return(Func &&f) const {
97 return std::visit(
98 [&](auto &arg) {
99 return f(arg);
100 },
101 value);
102 }
103
104 template<template<class> class Container2, class Func>
105 FieldVariant<Container2> convert(Func &&f) {
106 return std::visit(
107 [&](auto &arg) {
108 return f(arg);
109 },
110 value);
111 }
112 };
113
114 template<template<class> class Container>
115 using var_t_template = typename FieldVariant<Container>::var_t_template;
116
117} // namespace shamrock::patch
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
STL namespace.