Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SolverGraphSerializable.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
23namespace shamrock::solvergraph {
24
25 class SolverGraphSerializable : public SolverGraph {
26 public:
27 SolverGraphSerializable()
28 : SolverGraph(
30 .name = "SolverGraphSerializable",
31 .node_check = [](const std::shared_ptr<INode> &) -> bool {
32 return false; // there is no clean mechanism to serialize a node + its
33 // connexions
34 },
35 .edge_check = [](const std::shared_ptr<IEdge> &edge) -> bool {
36 return dynamic_cast<const JsonSerializable *>(edge.get()) != nullptr;
37 }}) {}
38
39 ~SolverGraphSerializable() = default;
40 };
41
49 inline void to_json(nlohmann::json &j, const SolverGraphSerializable &p) {
50 nlohmann::json edges = nlohmann::json::object();
51
52 for (const std::string &name : p.get_edge_names()) {
53 const auto &edge_ptr = p.get_edge_ptr_base(name);
54
55 // we use raw pointer to avoir the cost of creating a new shared pointer
56 auto *serializable = dynamic_cast<const JsonSerializable *>(edge_ptr.get());
57 if (serializable == nullptr) {
59 "Edge '{}' is registered in SolverGraphSerializable but is not "
60 "JsonSerializable",
61 name));
62 }
63
64 nlohmann::json edge_j;
65 serializable->to_json(edge_j);
66 edges[name] = std::move(edge_j);
67 }
68
69 j = nlohmann::json{{"edges", std::move(edges)}};
70 }
71
83 inline void from_json(const nlohmann::json &j, SolverGraphSerializable &p) {
84 if (!j.is_object() || !j.contains("edges") || !j.at("edges").is_object()) {
86 "Invalid JSON for SolverGraphSerializable: expected an object with an 'edges' "
87 "object");
88 }
89
91 const auto &edges = j.at("edges");
92
93 for (const auto &[name, edge_json] : edges.items()) {
94 std::shared_ptr<JsonSerializable> serializable = JsonSerializable::from_json(edge_json);
95 auto edge = std::dynamic_pointer_cast<IEdge>(serializable);
96 if (!bool(edge)) {
98 "Deserialized type for edge '{}' does not inherit from IEdge", name));
99 }
100
101 tmp.register_edge_ptr_base(name, std::move(edge));
102 }
103
104 p = std::move(tmp);
105 }
106} // namespace shamrock::solvergraph
Polymorphic JSON serialization via a type registry.
void to_json(nlohmann::json &j, const SolverGraphSerializable &p)
Serialize a SolverGraphSerializable to JSON.
void from_json(const nlohmann::json &j, SolverGraphSerializable &p)
Deserialize a SolverGraphSerializable from JSON.
Declare a class to register and retrieve nodes and edges from a unique container.
std::shared_ptr< IEdge > register_edge_ptr_base(const std::string &name, std::shared_ptr< IEdge > edge)
Register an edge with the graph using a shared pointer.
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.
Base interface for types that can be serialized to and from JSON.
void to_json(nlohmann::json &j) const
Write this object to JSON, including the "type" discriminator.
static std::unique_ptr< JsonSerializable > from_json(const nlohmann::json &j)
Reconstruct a registered type from JSON.