SST 16.0.0
Structural Simulation Toolkit
serialize_tuple.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TUPLE_H
13#define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TUPLE_H
14
15#ifndef SST_INCLUDING_SERIALIZE_H
16#warning \
17 "The header file sst/core/serialization/impl/serialize_tuple.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serialize.h"
18#endif
19
20#include "sst/core/serialization/serializer.h"
21
22#include <string>
23#include <tuple>
24#include <type_traits>
25#include <utility>
26
27namespace SST::Core::Serialization {
28
29// Serialize tuples and pairs
30template <typename T>
32 std::enable_if_t<is_same_type_template_v<T, std::tuple> || is_same_type_template_v<T, std::pair>>>
33{
34 template <typename>
35 struct serialize_tuple;
36
37 // For a sequence of indices, serialize each element of the std::tuple
38 // This is only used in mapping mode
39 template <size_t... INDEX>
40 struct serialize_tuple<std::index_sequence<INDEX...>>
41 {
42 void operator()(T& t, serializer& ser, ser_opt_t opt)
43 {
44 (SST_SER_NAME(std::get<INDEX>(t), std::to_string(INDEX).c_str(), opt), ...);
45 }
46 };
47
48 void operator()(T& t, serializer& ser, ser_opt_t options)
49 {
50 ser_opt_t opt = SerOption::is_set(options, SerOption::as_ptr_elem) ? SerOption::as_ptr : SerOption::none;
51 switch ( ser.mode() ) {
52 case serializer::MAP:
53 ser.mapper().map_hierarchy_start(ser.getMapName(), new ObjectMapContainer<T>(&t));
54 if constexpr ( is_same_type_template_v<T, std::pair> ) {
55 // Serialize first and second members of std::pair
56 SST_SER_NAME(t.first, "first", opt);
57 SST_SER_NAME(t.second, "second", opt);
58 }
59 else {
60 // Serialize each element in a std::tuple
61 serialize_tuple<std::make_index_sequence<std::tuple_size_v<T>>>()(t, ser, opt);
62 }
63 ser.mapper().map_hierarchy_end();
64 break;
65
66 default:
67 // Serialize each element in a std::tuple or std::pair
68 std::apply([&](auto&... e) { ((SST_SER(e, opt)), ...); }, t);
69 break;
70 }
71 }
72
73 SST_FRIEND_SERIALIZE();
74};
75
76// Serialize pointers to tuple and pairs
77template <typename T>
79 std::enable_if_t<is_same_type_template_v<T, std::tuple> || is_same_type_template_v<T, std::pair>>>
80{
81 void operator()(T*& obj, serializer& ser, ser_opt_t options)
82 {
83 if ( ser.mode() == serializer::UNPACK ) obj = new T;
84 SST_SER(*obj, options);
85 }
86 SST_FRIEND_SERIALIZE();
87};
88
89} // namespace SST::Core::Serialization
90
91#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TUPLE_H
Class used to map containers.
Definition objectMap.h:1420
Base serialize class.
Definition serialize.h:132
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43