Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
ZLibTransformStreams.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Assert.h"
5#include "AsyncStreams.h"
6#include "Internal/ZLibStream.h"
7
8namespace SC
9{
11{
13 ZLibStream stream;
14
15 private:
16 size_t consumedInputBytes = 0;
17
18 virtual Result asyncWrite(AsyncBufferView::ID bufferID, Function<void(AsyncBufferView::ID)> cb) override;
19 virtual bool canEndWritable() override;
20};
21
22template <typename T_AsyncEventLoop>
24{
26 {
27 asyncWork.work.template bind<AsyncZLibTransformStreamT, &AsyncZLibTransformStreamT::work>(*this);
28 asyncWork.callback.template bind<AsyncZLibTransformStreamT, &AsyncZLibTransformStreamT::afterWork>(*this);
29 }
30
31 ZLibStream stream;
32
33 typename T_AsyncEventLoop::LoopWork asyncWork;
34
35 void setEventLoop(T_AsyncEventLoop& loop) { eventLoop = &loop; }
36
37 private:
38 T_AsyncEventLoop* eventLoop = nullptr;
39
40 virtual Result onProcess(Span<const char> input, Span<char> output) override
41 {
42 SC_ASSERT_RELEASE(not finalizing);
43 savedInput = input;
44 savedOutput = output;
45 finalizing = false;
46 SC_TRY_MSG(eventLoop != nullptr, "AsyncZLibTransformStreamT::setEventLoop not called");
47 return asyncWork.start(*eventLoop);
48 }
49
50 virtual Result onFinalize(Span<char> output) override
51 {
52 // Intentionally not resetting savedInput, that can contain leftover data to process
53 savedOutput = output;
54 finalizing = true;
55 SC_TRY_MSG(eventLoop != nullptr, "AsyncZLibTransformStreamT::setEventLoop not called");
56 return asyncWork.start(*eventLoop);
57 }
58
59 void afterWork(typename T_AsyncEventLoop::LoopWork::Result&)
60 {
61 if (finalizing)
62 {
63 AsyncTransformStream::afterFinalize(savedOutput, streamEnded);
64 }
65 else
66 {
67 AsyncTransformStream::afterProcess(savedInput, savedOutput);
68 }
69 }
70
71 Result work()
72 {
73 return finalizing ? stream.finalize(savedOutput, streamEnded) : stream.process(savedInput, savedOutput);
74 }
75
76 bool finalizing = false;
77 bool streamEnded = false;
78
79 Span<const char> savedInput;
80 Span<char> savedOutput;
81};
82
83} // namespace SC
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:48
#define SC_TRY_MSG(expression, failedMessage)
Checks the value of the given expression and if failed, returns a result with failedMessage to caller...
Definition Result.h:60
struct SC_COMPILER_EXPORT Function
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
Definition AsyncStreams.h:52
A stream that can both produce and consume buffers.
Definition AsyncStreams.h:469
A duplex stream that produces new buffers transforming received buffers.
Definition AsyncStreams.h:480
Definition ZLibTransformStreams.h:24
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Definition ZLibTransformStreams.h:11