/src/brunsli/c/dec/output_chunk.h
Line | Count | Source |
1 | | // Copyright (c) Google LLC 2020 |
2 | | // |
3 | | // Use of this source code is governed by an MIT-style |
4 | | // license that can be found in the LICENSE file or at |
5 | | // https://opensource.org/licenses/MIT. |
6 | | |
7 | | #ifndef BRUNSLI_DEC_OUPUT_CHUNK_H_ |
8 | | #define BRUNSLI_DEC_OUPUT_CHUNK_H_ |
9 | | |
10 | | #include <initializer_list> |
11 | | #include <memory> |
12 | | #include <vector> |
13 | | |
14 | | #include <brunsli/types.h> |
15 | | |
16 | | namespace brunsli { |
17 | | namespace internal { |
18 | | namespace dec { |
19 | | |
20 | | /** |
21 | | * A chunk of output data. |
22 | | * |
23 | | * Data producer creates OutputChunks and adds them to the end output queue. |
24 | | * Once control flow leaves the producer code, it is considered that chunk of |
25 | | * data is final and can not be changed; to underline this fact |next| is a |
26 | | * const-pointer. |
27 | | * |
28 | | * Data consumer removes OutputChunks from the beginning of the output queue. |
29 | | * It is possible to consume OutputChunks partially, by updating |next| and |
30 | | * |len|. |
31 | | * |
32 | | * There are 2 types of output chunks: |
33 | | * - owning: actual data is stored in |buffer| field; producer fills data after |
34 | | * the instance it created; it is legal to reduce |len| to show that not all |
35 | | * the capacity of |buffer| is used |
36 | | * - non-owning: represents the data stored (owned) somewhere else |
37 | | */ |
38 | | struct OutputChunk { |
39 | | // Non-owning |
40 | | template<typename Bytes> |
41 | 549 | OutputChunk(Bytes& bytes) : len(bytes.size()) { |
42 | | // Deal both with const qualifier and data type. |
43 | 549 | const void* src = bytes.data(); |
44 | 549 | next = reinterpret_cast<const uint8_t*>(src); |
45 | 549 | } |
46 | | |
47 | | // Non-owning |
48 | 7.80k | OutputChunk(const uint8_t* data, size_t size) : next(data), len(size) {} |
49 | | |
50 | | // Owning |
51 | 83.2k | explicit OutputChunk(size_t size = 0) { |
52 | 83.2k | buffer.reset(new std::vector<uint8_t>(size)); |
53 | 83.2k | next = buffer->data(); |
54 | 83.2k | len = size; |
55 | 83.2k | } |
56 | | |
57 | | // Owning |
58 | 25.8k | OutputChunk(std::initializer_list<uint8_t> bytes) { |
59 | 25.8k | buffer.reset(new std::vector<uint8_t>(bytes)); |
60 | 25.8k | next = buffer->data(); |
61 | 25.8k | len = bytes.size(); |
62 | 25.8k | } |
63 | | |
64 | | const uint8_t* next; |
65 | | size_t len; |
66 | | std::unique_ptr<std::vector<uint8_t>> buffer; |
67 | | }; |
68 | | |
69 | | } // namespace dec |
70 | | } // namespace internal |
71 | | } // namespace brunsli |
72 | | |
73 | | #endif // BRUNSLI_DEC_OUPUT_CHUNK_H_ |