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