SST 15.0
Structural Simulation Toolkit
ser_buffer_accessor.h
1// Copyright 2009-2025 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-2025, 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_SER_BUFFER_ACCESSOR_H
13#define SST_CORE_SERIALIZATION_IMPL_SER_BUFFER_ACCESSOR_H
14
15#ifndef SST_INCLUDING_SERIALIZER_H
16#warning \
17 "The header file sst/core/serialization/impl/ser_buffer_accessor.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/warnmacros.h"
21
22#include <cstring>
23#include <exception>
24
25namespace SST::Core::Serialization::pvt {
26
27// class ser_buffer_overrun : public spkt_error {
28class ser_buffer_overrun : public std::exception
29{
30public:
31 explicit ser_buffer_overrun(int UNUSED(maxsize))
32 // ser_buffer_overrun(int maxsize) :
33 // spkt_error(sprockit::printf("serialization overrun buffer of size %d", maxsize))
34 {}
35};
36
37class ser_buffer_accessor
38{
39public:
40 template <class T>
41 T* next()
42 {
43 T* ser_buffer = reinterpret_cast<T*>(bufptr_);
44 bufptr_ += sizeof(T);
45 size_ += sizeof(T);
46 if ( size_ > max_size_ ) throw ser_buffer_overrun(max_size_);
47 return ser_buffer;
48 }
49
50 char* next_str(size_t size)
51 {
52 char* ser_buffer = reinterpret_cast<char*>(bufptr_);
53 bufptr_ += size;
54 size_ += size;
55 if ( size_ > max_size_ ) throw ser_buffer_overrun(max_size_);
56 return ser_buffer;
57 }
58
59 size_t size() const { return size_; }
60
61 size_t max_size() const { return max_size_; }
62
63 void init(void* buffer, size_t size)
64 {
65 bufstart_ = reinterpret_cast<char*>(buffer);
66 max_size_ = size;
67 reset();
68 }
69
70 void clear()
71 {
72 bufstart_ = bufptr_ = nullptr;
73 max_size_ = size_ = 0;
74 }
75
76 void reset()
77 {
78 bufptr_ = bufstart_;
79 size_ = 0;
80 }
81
82protected:
83 ser_buffer_accessor() :
84 bufstart_(nullptr),
85 bufptr_(nullptr),
86 size_(0),
87 max_size_(0)
88 {}
89
90protected:
91 char* bufstart_;
92 char* bufptr_;
93 size_t size_;
94 size_t max_size_;
95};
96
97} // namespace SST::Core::Serialization::pvt
98
99#endif // SST_CORE_SERIALIZATION_IMPL_SER_BUFFER_ACCESSOR_H
Definition ser_buffer_accessor.h:29