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