cpp-mate  0.7
Helpful library for C++.
BinarySink.hpp
1 /*
2  * Copyright (C) 2020 Alexander Kornilov (akornilov.82@gmail.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_MATE_BINARY_SINK_HPP
18 #define CPP_MATE_BINARY_SINK_HPP
19 
20 #include <CppMate/BinaryData.hpp>
21 #include <CppMate/Checkers.hpp>
22 
23 #include <vector>
24 #include <limits>
25 
26 #include <cstdint>
27 
28 #include <memory.h>
29 
30 namespace CppMate {
31 
36 {
37 public:
38  class Closeable;
39  class Null;
40  class Chunk;
41  class Memory;
42  class Counter;
43 
47  virtual ~BinarySink() = default;
48 
55  virtual void write(const void* data, uint64_t size) = 0;
56 };
57 
61 class BinarySink::Closeable: public virtual BinarySink
62 {
63 public:
64 
69  virtual bool isClosed() const = 0;
70 
76  virtual void close() = 0;
77 };
78 
83 {
84 public:
85  void write(const void* data, uint64_t) override {
86  checkArg(data);
87  }
88 };
89 
93 class BinarySink::Chunk: public virtual BinarySink
94 {
95 public:
96 
101  Chunk(size_t chunkSize = std::numeric_limits<size_t>::max()):
102  _chunkSize(chunkSize)
103  {
104  checkArg(chunkSize > 0);
105  }
106 
107  void write(const void* data, uint64_t size) override {
108  checkArg(data);
109  const auto* source = static_cast<const uint8_t*>(data);
110  for (uint64_t i = 0; i < size / static_cast<uint64_t>(_chunkSize); i++) {
111  writeChunk(source, _chunkSize);
112  source += _chunkSize;
113  }
114  const auto lastChunkSize = size % _chunkSize;
115  if (lastChunkSize) {
116  writeChunk(source, lastChunkSize);
117  }
118  }
119 
120 protected:
121 
127  virtual void writeChunk(const void* data, size_t size) = 0;
128 
129 private:
130  size_t _chunkSize;
131 };
132 
137 {
138 public:
139  const uint8_t* getData() const override { return &_buffer[0]; }
140  uint64_t getSize() const override { return _buffer.size(); }
141 
145  void clear() { _buffer.clear(); }
146 
147 protected:
148  void writeChunk(const void* data, size_t size) override {
149  const auto offset = _buffer.size();
150  _buffer.resize(offset + size);
151  memcpy(&_buffer[offset], data ,size);
152  }
153 
154 private:
155  std::vector<uint8_t> _buffer;
156 };
157 
161 class BinarySink::Counter: public virtual BinarySink
162 {
163 public:
164 
170  _sink(sink),
171  _bytesWritten(0) {}
172 
178  Counter(std::shared_ptr<BinarySink> sink):
179  _sink(*sink),
180  _sinkPtr(std::move(sink)),
181  _bytesWritten(0)
182  {
183  checkArg(_sinkPtr.get());
184  }
185 
190  uint64_t getBytesWritten() const { return _bytesWritten; }
191 
195  void resetCounter() { _bytesWritten = 0; }
196 
197  void write(const void* data, uint64_t size) override {
198  checkArg(data);
199  _sink.write(data, size);
200  _bytesWritten += size;
201  }
202 
203 private:
204  BinarySink& _sink;
205  std::shared_ptr<BinarySink> _sinkPtr;
206  uint64_t _bytesWritten;
207 };
208 
209 } // namespace CppMate
210 
211 #endif // CPP_MATE_BINARY_SINK_HPP
Represents interface of abstract binary data.
Definition: BinaryData.hpp:34
Represents sink implementation which splits source data into chunks with specified size.
Definition: BinarySink.hpp:94
void write(const void *data, uint64_t size) override
Writes data into the sink.
Definition: BinarySink.hpp:107
Chunk(size_t chunkSize=std::numeric_limits< size_t >::max())
Constructor.
Definition: BinarySink.hpp:101
virtual void writeChunk(const void *data, size_t size)=0
Writes chunk of the data.
Represents closable extension of the BinarySink interface.
Definition: BinarySink.hpp:62
virtual bool isClosed() const =0
Indicates that sink is closed.
virtual void close()=0
Closes sink.
Represents sink implementation which splits source data into chunks with specified size.
Definition: BinarySink.hpp:162
void write(const void *data, uint64_t size) override
Writes data into the sink.
Definition: BinarySink.hpp:197
Counter(BinarySink &sink)
Constructor.
Definition: BinarySink.hpp:169
void resetCounter()
Resets counter.
Definition: BinarySink.hpp:195
uint64_t getBytesWritten() const
Returns total bytes written into the sink.
Definition: BinarySink.hpp:190
Counter(std::shared_ptr< BinarySink > sink)
Constructor.
Definition: BinarySink.hpp:178
Represents binary sink to memory.
Definition: BinarySink.hpp:137
void clear()
Clears buffer.
Definition: BinarySink.hpp:145
uint64_t getSize() const override
Returns size of data in bytes.
Definition: BinarySink.hpp:140
const uint8_t * getData() const override
Returns address of data.
Definition: BinarySink.hpp:139
void writeChunk(const void *data, size_t size) override
Writes chunk of the data.
Definition: BinarySink.hpp:148
Represents sink which just ignore all input data.
Definition: BinarySink.hpp:83
void write(const void *data, uint64_t) override
Writes data into the sink.
Definition: BinarySink.hpp:85
Represents low-level interface of abstract binary sink.
Definition: BinarySink.hpp:36
virtual void write(const void *data, uint64_t size)=0
Writes data into the sink.
virtual ~BinarySink()=default
Definition: BinaryData.hpp:28