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