SST/macro
serialize_buffer_accessor.h
Go to the documentation of this file.
1 #ifndef SERIALIZE_ACCESSOR_H
2 #define SERIALIZE_ACCESSOR_H
3 
4 #include <cstring>
5 #include <sprockit/errors.h>
6 
7 namespace sprockit {
8 namespace pvt {
9 
11  public:
12  ser_buffer_overrun(int maxsize) :
13  spkt_error(sprockit::printf("serialization overrun buffer of size %d", maxsize))
14  {
15  }
16 };
17 
19  public:
20  template <class T>
21  T*
22  next(){
23  T* ser_buffer = reinterpret_cast<T*>(bufptr_);
24  bufptr_ += sizeof(T);
25  size_ += sizeof(T);
26  if (size_ > max_size_) throw ser_buffer_overrun(max_size_);
27  return ser_buffer;
28  }
29 
30  char*
31  next_str(size_t size){
32  char* ser_buffer = reinterpret_cast<char*>(bufptr_);
33  bufptr_ += size;
34  size_ += size;
35  if (size_ > max_size_) throw ser_buffer_overrun(max_size_);
36  return ser_buffer;
37  }
38 
39  size_t
40  size() const {
41  return size_;
42  }
43 
44  size_t
45  max_size() const {
46  return max_size_;
47  }
48 
49  void
50  init(void* buffer, size_t size){
51  bufstart_ = reinterpret_cast<char*>(buffer);
52  max_size_ = size;
53  reset();
54  }
55 
56  void
57  clear(){
58  bufstart_ = bufptr_ = 0;
59  max_size_ = size_ = 0;
60  }
61 
62  void
63  reset(){
64  bufptr_ = bufstart_;
65  size_ = 0;
66  }
67 
68  protected:
70  bufstart_(0),
71  bufptr_(0),
72  size_(0)
73  {
74  }
75 
76  protected:
77  char* bufstart_;
78  char* bufptr_;
79  size_t size_;
80  size_t max_size_;
81 
82 };
83 
84 } }
85 
86 #endif // SERIALIZE_ACCESSOR_H
std::string printf(const char *fmt,...)
void init(void *buffer, size_t size)
General errors, or base class for more specific errors.
Definition: errors.h:54