SST 16.0.0
Structural Simulation Toolkit
sizer.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_SIZER_H
13#define SST_CORE_SERIALIZATION_IMPL_SIZER_H
14
15#ifndef SST_INCLUDING_SERIALIZER_H
16#warning \
17 "The header file sst/core/serialization/impl/sizer.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_shared_ptr_tracker.h"
22
23#include <cstddef>
24#include <cstdint>
25#include <set>
26#include <string>
27#include <type_traits>
28
29namespace SST::Core::Serialization::pvt {
30
31class ser_sizer : public ser_shared_ptr_packer
32{
33 size_t size_ = 0;
34 std::set<uintptr_t> pointer_set;
35
36public:
37 explicit ser_sizer() = default;
38
39 template <typename T>
40 void size(const T&)
41 {
42 size_ += sizeof(T);
43 }
44
45 template <typename T, typename SIZE_T>
46 void size_buffer(const T* buffer, SIZE_T size)
47 {
48 if ( buffer != nullptr )
49 get_array_size(size, "Serialization Error: Size in SST::Core::Serialization:pvt::size_buffer() cannot fit "
50 "inside size_t. size_t should be used for sizes.\n");
51 else
52 size = 0;
53 using ELEM_T = std::conditional_t<std::is_void_v<T>, char, T>; // Use char if T == void
54 size_ += sizeof(size) + size * sizeof(ELEM_T);
55 }
56
57 void size_string(std::string& str) { size_ += sizeof(size_t) + str.size(); }
58 void add(size_t s) { size_ += s; }
59 size_t size() const { return size_; }
60 bool check_pointer_sizer(uintptr_t ptr) { return !pointer_set.insert(ptr).second; }
61}; // class ser_sizer
62
63} // namespace SST::Core::Serialization::pvt
64
65#endif // SST_CORE_SERIALIZATION_IMPL_SIZER_H