AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
ADynamicPipe.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14
15#include "IInputStream.h"
16#include "IOutputStream.h"
17#include "AUI/Common/AQueue.h"
18#include <array>
19
29class API_AUI_CORE ADynamicPipe: public IInputStream, public IOutputStream {
30public:
31 ~ADynamicPipe() override = default;
32
33 void write(const char* src, size_t size) override;
34 size_t read(char* dst, size_t size) override;
35
36
37 [[nodiscard]]
38 bool empty() const noexcept {
39 return mQueue.empty();
40 }
41
42private:
43 static constexpr std::size_t FRAGMENT_CAPACITY = 0x10000;
44
45 struct Fragment {
46 std::array<std::uint8_t, FRAGMENT_CAPACITY> data;
47 std::size_t writerPos = 0;
48 std::size_t readerPos = 0;
49
50 [[nodiscard]]
51 std::size_t writerRemaining() const noexcept { return FRAGMENT_CAPACITY - writerPos; }
52
53 [[nodiscard]]
54 std::size_t readerRemaining() const noexcept { return writerPos - readerPos; }
55
56 void push(const char*& src, size_t& size) {
57 std::size_t sizeForCurrentFragment = glm::min(size, writerRemaining());
58 std::memcpy(data.data() + writerPos, src, sizeForCurrentFragment);
59 writerPos += sizeForCurrentFragment;
60 src += sizeForCurrentFragment;
61 size -= sizeForCurrentFragment;
62 }
63 };
64
65 AQueue<Fragment> mQueue;
66};
67
68
An asynchronous buffer that converts an IInputStream to IOutputStream (and otherwise).
Definition: ADynamicPipe.h:29
A std::queue with AUI extensions.
Definition: AQueue.h:24
Represents an input stream.
Definition: IInputStream.h:26
virtual size_t read(char *dst, size_t size)=0
Reads up to size bytes from stream. Blocking (waiting for new data) is allowed.
Definition: IOutputStream.h:20
virtual void write(const char *src, size_t size)=0
Writes exact size bytes to stream. Blocking (waiting for write all data) is allowed.
Definition: Empty.h:19