Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
IDataEdgeSerializable.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
18
22#include "nlohmann/json_fwd.hpp"
25#include <stdexcept>
26
27namespace shamrock::solvergraph {
28
29 template<class T>
31 public:
32 using IDataEdge<T>::IDataEdge;
33 using IDataEdge<T>::data;
34
35 void _impl_to_json(nlohmann::json &j) const override {
36 j["data"] = data;
37 j["label"] = this->get_label();
38 j["tex_symbol"] = this->get_raw_tex_symbol();
39 };
40
41 static IDataEdgeSerializable<T> from_json(const nlohmann::json &j) {
42 std::string label = j.at("label").get<std::string>();
43 std::string tex_symbol = j.at("tex_symbol").get<std::string>();
44
45 auto tmp = IDataEdgeSerializable<T>(label, tex_symbol);
46 tmp.data = j.at("data").get<T>();
47 return tmp;
48 };
49
50 inline static std::string type_name_static() {
51 return "IDataEdgeSerializable<" + shambase::get_type_name<T>() + ">";
52 }
53
54 std::string type_name() const override { return type_name_static(); };
55 };
56
57} // namespace shamrock::solvergraph
58
59#define REGISTER_IDATAEDGESERIALIZABLE(Tin) \
60 PRE_MAIN_FUNCTION_CALL([&]() { \
61 using T = Tin; \
62 auto &instance = shamrock::solvergraph::JsonSerializable_registry::instance(); \
63 if (!instance.is_type_registered(T::type_name_static())) { \
64 instance.register_type<T>(T::type_name_static()); \
65 } \
66 })
67
68REGISTER_IDATAEDGESERIALIZABLE(shamrock::solvergraph::IDataEdgeSerializable<f64>);
Polymorphic JSON serialization via a type registry.
void from_json(const nlohmann::json &j, SolverGraphSerializable &p)
Deserialize a SolverGraphSerializable from JSON.
void _impl_to_json(nlohmann::json &j) const override
Write derived-class fields into a JSON object.
std::string type_name() const override
Return the type discriminator string for this class.
This header file contains utility functions related to exception handling in the code.
Provides a macro to call a lambda before main.
Base interface for types that can be serialized to and from JSON.