SST 16.0.0
Structural Simulation Toolkit
packer.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_PACKER_H
13#define SST_CORE_SERIALIZATION_IMPL_PACKER_H
14
15#ifndef SST_INCLUDING_SERIALIZER_H
16#warning \
17 "The header file sst/core/serialization/impl/packer.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serializer.h"
18#endif
19
20#include "sst/core/serialization/impl/get_array_size.h"
21#include "sst/core/serialization/impl/ser_buffer_accessor.h"
22#include "sst/core/serialization/impl/ser_shared_ptr_tracker.h"
23
24#include <cstdint>
25#include <cstring>
26#include <set>
27#include <string>
28#include <type_traits>
29
30namespace SST::Core::Serialization::pvt {
31
32class ser_packer : public ser_buffer_accessor, public ser_shared_ptr_packer
33{
34 std::set<uintptr_t> pointer_set;
35
36public:
37 // inherit ser_buffer_accessor constructors
38 using ser_buffer_accessor::ser_buffer_accessor;
39
40 template <typename T>
41 void pack(const T& t)
42 {
43 memcpy(buf_next(sizeof(t)), &t, sizeof(t));
44 }
45
46 template <typename T, typename SIZE_T>
47 void pack_buffer(const T* buffer, SIZE_T size)
48 {
49 if ( buffer != nullptr )
50 get_array_size(size, "Serialization Error: Size in SST::Core::Serialization:pvt::pack_buffer() cannot fit "
51 "inside size_t. size_t should be used for sizes.\n");
52 else
53 size = 0;
54 using ELEM_T = std::conditional_t<std::is_void_v<T>, char, T>; // Use char if T == void
55 pack(size);
56 memcpy(buf_next(size * sizeof(ELEM_T)), buffer, size * sizeof(ELEM_T));
57 }
58
59 void pack_string(const std::string& str) { pack_buffer(str.data(), str.size()); }
60 bool check_pointer_pack(uintptr_t ptr) { return !pointer_set.insert(ptr).second; }
61}; // class ser_packer
62
63} // namespace SST::Core::Serialization::pvt
64
65#endif // SST_CORE_SERIALIZATION_IMPL_PACKER_H