1 // Copyright 2012 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Stream buffer for serialization.
28 
29 #ifndef STMLIB_UTILS_STREAM_BUFFER_H_
30 #define STMLIB_UTILS_STREAM_BUFFER_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include <cstring>
35 #include <algorithm>
36 
37 namespace stmlib {
38 
39 template<size_t buffer_size>
40 class StreamBuffer {
41  public:
StreamBuffer()42   StreamBuffer() { Clear(); }
43 
Clear()44   void Clear() {
45     ptr_ = 0;
46     std::fill(&buffer_[0], &buffer_[buffer_size], 0);
47   }
48 
position()49   inline size_t position() const {
50     return ptr_;
51   }
52 
bytes()53   inline const uint8_t* bytes() const {
54     return buffer_;
55   }
56 
mutable_bytes()57   inline uint8_t* mutable_bytes() {
58     return buffer_;
59   }
60 
Write(const void * data,size_t size)61   void Write(const void* data, size_t size) {
62     if (ptr_ + size > buffer_size) {
63       return;
64     }
65     memcpy(&buffer_[ptr_], data, size);
66     ptr_ += size;
67   }
68 
69   template<typename T>
Write(const T & value)70   void Write(const T& value) {
71     Write(&value, sizeof(T));
72   }
73 
74   template<typename T>
Read(T * value)75   void Read(T* value) {
76     if (ptr_ + sizeof(T) > buffer_size) {
77       return;
78     }
79     memcpy((void*)value, &buffer_[ptr_], sizeof(T));
80     ptr_ += sizeof(T);
81   }
82 
Seek(size_t position)83   inline void Seek(size_t position) {
84     ptr_ = position;
85   }
86 
Rewind()87   inline void Rewind() { Seek(0); }
88 
89  private:
90   uint8_t buffer_[buffer_size];
91   size_t ptr_;
92 
93   DISALLOW_COPY_AND_ASSIGN(StreamBuffer);
94 };
95 
96 }  // namespace stmlib
97 
98 #endif   // STMLIB_UTILS_STREAM_BUFFER_H_
99