Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
DataNode.cpp
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
16#include "DataNode.hpp"
18#include "shambase/string.hpp"
19
20namespace shamtest::details {
21
22 std::string serialize_vec(const std::vector<f64> &vec) {
23 std::string acc = "\n[\n";
24
25 for (u32 i = 0; i < vec.size(); i++) {
26 acc += shambase::format_printf("%e", vec[i]);
27 if (i < vec.size() - 1) {
28 acc += ", ";
29 }
30 }
31
32 acc += "\n]";
33 return acc;
34 }
35
36 std::string DataNode::serialize_json() {
37 std::string acc = "\n{\n";
38
39 acc += R"( "name" : ")" + name + "\",\n";
40
41 acc += R"( "data" : )"
42 "\n"
43 + serialize_vec(data) + "\n";
44
45 acc += "\n}";
46 return acc;
47 }
48
49 void DataNode::serialize(std::basic_stringstream<byte> &stream) {
50 shambase::stream_write_string(stream, name);
51 shambase::stream_write_vector_trivial(stream, data);
52 }
53 DataNode DataNode::deserialize(std::basic_stringstream<byte> &stream) {
54
55 DataNode out{};
56
57 shambase::stream_read_string(stream, out.name);
59 return out;
60 }
61} // namespace shamtest::details
This file hold the definitions for a test DataNode.
std::uint32_t u32
32 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void stream_read_vector_trivial(std::basic_stringstream< byte > &stream, std::vector< T > &vec)
read a vector from the bytestream Note : this appends read objects to the vector without resetting it
implementation details of the test library
Definition DataNode.hpp:23
std::string serialize_json()
Serialize the assertion in JSON.
std::string name
Name of the data node.
Definition DataNode.hpp:27
static DataNode deserialize(std::basic_stringstream< byte > &reader)
DeSerialize the assertion from binary format.
void serialize(std::basic_stringstream< byte > &stream)
Serialize the assertion in binary format.
std::vector< f64 > data
Held data.
Definition DataNode.hpp:28