• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNWIRE_BUFFERCONSUMER_H_
16 #define DAWNWIRE_BUFFERCONSUMER_H_
17 
18 #include "dawn_wire/WireResult.h"
19 
20 #include <cstddef>
21 
22 namespace dawn_wire {
23 
24     // BufferConsumer is a utility class that allows reading bytes from a buffer
25     // while simultaneously decrementing the amount of remaining space by exactly
26     // the amount read. It helps prevent bugs where incrementing a pointer and
27     // decrementing a size value are not kept in sync.
28     // BufferConsumer also contains bounds checks to prevent reading out-of-bounds.
29     template <typename BufferT>
30     class BufferConsumer {
31         static_assert(sizeof(BufferT) == 1,
32                       "BufferT must be 1-byte, but may have const/volatile qualifiers.");
33 
34       public:
BufferConsumer(BufferT * buffer,size_t size)35         BufferConsumer(BufferT* buffer, size_t size) : mBuffer(buffer), mSize(size) {
36         }
37 
Buffer()38         BufferT* Buffer() const {
39             return mBuffer;
40         }
AvailableSize()41         size_t AvailableSize() const {
42             return mSize;
43         }
44 
45       protected:
46         template <typename T, typename N>
47         WireResult NextN(N count, T** data);
48 
49         template <typename T>
50         WireResult Next(T** data);
51 
52         template <typename T>
53         WireResult Peek(T** data);
54 
55       private:
56         BufferT* mBuffer;
57         size_t mSize;
58     };
59 
60     class SerializeBuffer : public BufferConsumer<char> {
61       public:
62         using BufferConsumer::BufferConsumer;
63         using BufferConsumer::Next;
64         using BufferConsumer::NextN;
65     };
66 
67     class DeserializeBuffer : public BufferConsumer<const volatile char> {
68       public:
69         using BufferConsumer::BufferConsumer;
70         using BufferConsumer::Peek;
71 
72         template <typename T, typename N>
ReadN(N count,const volatile T ** data)73         WireResult ReadN(N count, const volatile T** data) {
74             return NextN(count, data);
75         }
76 
77         template <typename T>
Read(const volatile T ** data)78         WireResult Read(const volatile T** data) {
79             return Next(data);
80         }
81     };
82 
83 }  // namespace dawn_wire
84 
85 #endif  // DAWNWIRE_BUFFERCONSUMER_H_