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