Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
stream.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * output buffer and 'stream' layered on top of a compression codec
25 */
26
27#ifndef INCLUDED_STREAM
28#define INCLUDED_STREAM
29
30#include "lib/code_annotation.h"
32#include "lib/status.h"
33
34#include <cstddef>
35#include <memory>
36
37// note: this is similar in function to std::vector, but we don't need
38// iterators etc. and would prefer to avoid initializing each byte.
40{
41public:
43
44 void Reset();
45 void SetBuffer(std::uint8_t* buffer, size_t size);
46
47 /**
48 * allocate a new output buffer.
49 *
50 * @param size [bytes] to allocate.
51 *
52 * notes:
53 * - if a buffer had previously been allocated and is large enough,
54 * it is reused (this reduces the number of allocations).
55 * - this class manages the lifetime of the buffer.
56 **/
57 void AllocateBuffer(size_t size);
58
59 std::uint8_t* Buffer() const
60 {
61 return m_buffer;
62 }
63
64 size_t Size() const
65 {
66 return m_size;
67 }
68
69private:
70 bool IsAllowableBuffer(std::uint8_t* buffer, size_t size);
71
72 std::uint8_t* m_buffer;
73 size_t m_size;
74
75 std::shared_ptr<std::uint8_t> m_mem;
76 // size of m_mem. allows reusing previously allocated buffers
77 // (user-specified buffers can't be reused because we have no control
78 // over their lifetime)
79 size_t m_capacity;
80};
81
82
83class Stream
84{
85public:
86 Stream(const PICodec& codec);
87
88 void SetOutputBuffer(std::uint8_t* out, size_t outSize);
89
90 void AllocateOutputBuffer(size_t outSizeMax);
91
92 /**
93 * 'feed' the codec with a data block.
94 **/
95 Status Feed(const std::uint8_t* in, size_t inSize);
96
97 Status Finish();
98
99 size_t OutSize() const
100 {
101 return m_outProduced;
102 }
103
104 std::uint32_t Checksum() const
105 {
106 return m_checksum;
107 }
108
109private:
112
115 std::uint32_t m_checksum;
116};
117
118// avoids the need for std::bind (not supported on all compilers) and boost::bind (can't be
119// used at work)
121{
123public:
125 : stream(stream)
126 {
127 }
128
129 Status operator()(const std::uint8_t* data, size_t size) const
130 {
131 return stream.Feed(data, size);
132 }
133
134private:
136};
137
138#endif // #ifndef INCLUDED_STREAM
Definition: stream.h:40
OutputBufferManager()
Definition: stream.cpp:35
size_t m_capacity
Definition: stream.h:79
void Reset()
Definition: stream.cpp:40
size_t Size() const
Definition: stream.h:64
size_t m_size
Definition: stream.h:73
bool IsAllowableBuffer(std::uint8_t *buffer, size_t size)
Definition: stream.cpp:75
void SetBuffer(std::uint8_t *buffer, size_t size)
Definition: stream.cpp:47
std::shared_ptr< std::uint8_t > m_mem
Definition: stream.h:75
std::uint8_t * Buffer() const
Definition: stream.h:59
void AllocateBuffer(size_t size)
allocate a new output buffer.
Definition: stream.cpp:55
std::uint8_t * m_buffer
Definition: stream.h:72
Definition: stream.h:84
Status Finish()
Definition: stream.cpp:133
size_t m_inConsumed
Definition: stream.h:113
Status Feed(const std::uint8_t *in, size_t inSize)
'feed' the codec with a data block.
Definition: stream.cpp:117
std::uint32_t Checksum() const
Definition: stream.h:104
void SetOutputBuffer(std::uint8_t *out, size_t outSize)
Definition: stream.cpp:111
Stream(const PICodec &codec)
Definition: stream.cpp:98
std::uint32_t m_checksum
Definition: stream.h:115
PICodec m_codec
Definition: stream.h:110
size_t OutSize() const
Definition: stream.h:99
OutputBufferManager m_outputBufferManager
Definition: stream.h:111
size_t m_outProduced
Definition: stream.h:114
void AllocateOutputBuffer(size_t outSizeMax)
Definition: stream.cpp:105
std::shared_ptr< ICodec > PICodec
Definition: codec.h:99
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: stream.h:121
Stream & stream
Definition: stream.h:135
StreamFeeder(Stream &stream)
Definition: stream.h:124
NONCOPYABLE(StreamFeeder)
Status operator()(const std::uint8_t *data, size_t size) const
Definition: stream.h:129
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407