WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
raw_uv_ostream.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <span>
9#include <utility>
10
11#include "wpi/net/uv/Buffer.hpp"
12#include "wpi/util/SmallVector.hpp"
13#include "wpi/util/raw_ostream.hpp"
14
15namespace wpi::net {
16
17/**
18 * wpi::util::raw_ostream style output to a wpi::util::SmallVector of uv::Buffer
19 * buffers. Fixed-size buffers are allocated and appended as necessary to fit
20 * the data being output. The wpi::util::SmallVector need not be empty at start.
21 */
22class raw_uv_ostream : public wpi::util::raw_ostream {
23 public:
24 /**
25 * Construct a new raw_uv_ostream.
26 * @param bufs Buffers vector. NOT cleared on construction.
27 * @param allocSize Size to allocate for each buffer; allocation will be
28 * performed using Buffer::Allocate().
29 */
31 : m_bufs(bufs), m_alloc([=] { return uv::Buffer::Allocate(allocSize); }) {
32 SetUnbuffered();
33 }
34
35 /**
36 * Construct a new raw_uv_ostream.
37 * @param bufs Buffers vector. NOT cleared on construction.
38 * @param alloc Allocator.
39 */
41 std::function<uv::Buffer()> alloc)
42 : m_bufs(bufs), m_alloc(std::move(alloc)) {
43 SetUnbuffered();
44 }
45
46 ~raw_uv_ostream() override = default;
47
48 /**
49 * Returns an span to the buffers.
50 */
51 std::span<uv::Buffer> bufs() { return m_bufs; }
52
53 void flush() = delete;
54
55 /**
56 * Resets the amount of allocated space.
57 */
58 void reset() { m_left = 0; }
59
60 private:
61 void write_impl(const char* data, size_t len) override;
62 uint64_t current_pos() const override;
63
65 std::function<uv::Buffer()> m_alloc;
66
67 // How much allocated space is left in the current buffer.
68 size_t m_left = 0;
69};
70
71} // namespace wpi::net
raw_uv_ostream(wpi::util::SmallVectorImpl< uv::Buffer > &bufs, std::function< uv::Buffer()> alloc)
Construct a new raw_uv_ostream.
Definition raw_uv_ostream.hpp:40
void reset()
Resets the amount of allocated space.
Definition raw_uv_ostream.hpp:58
raw_uv_ostream(wpi::util::SmallVectorImpl< uv::Buffer > &bufs, size_t allocSize)
Construct a new raw_uv_ostream.
Definition raw_uv_ostream.hpp:30
std::span< uv::Buffer > bufs()
Returns an span to the buffers.
Definition raw_uv_ostream.hpp:51
~raw_uv_ostream() override=default
Data buffer.
Definition Buffer.hpp:22
static Buffer Allocate(size_t size)
Definition Buffer.hpp:64
Definition BooleanTopic.hpp:24
Definition StringMap.hpp:773
Definition raw_socket_ostream.hpp:9