WPILibC++ 2024.3.2
raw_uv_ostream.h
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#ifndef WPINET_RAW_UV_OSTREAM_H_
6#define WPINET_RAW_UV_OSTREAM_H_
7
8#include <functional>
9#include <span>
10#include <utility>
11
12#include <wpi/SmallVector.h>
13#include <wpi/raw_ostream.h>
14
15#include "wpinet/uv/Buffer.h"
16
17namespace wpi {
18
19/**
20 * raw_ostream style output to a SmallVector of uv::Buffer buffers. Fixed-size
21 * buffers are allocated and appended as necessary to fit the data being output.
22 * The SmallVector need not be empty at start.
23 */
25 public:
26 /**
27 * Construct a new raw_uv_ostream.
28 * @param bufs Buffers vector. NOT cleared on construction.
29 * @param allocSize Size to allocate for each buffer; allocation will be
30 * performed using Buffer::Allocate().
31 */
33 : m_bufs(bufs), m_alloc([=] { return uv::Buffer::Allocate(allocSize); }) {
35 }
36
37 /**
38 * Construct a new raw_uv_ostream.
39 * @param bufs Buffers vector. NOT cleared on construction.
40 * @param alloc Allocator.
41 */
43 std::function<uv::Buffer()> alloc)
44 : m_bufs(bufs), m_alloc(std::move(alloc)) {
46 }
47
48 ~raw_uv_ostream() override = default;
49
50 /**
51 * Returns an span to the buffers.
52 */
53 std::span<uv::Buffer> bufs() { return m_bufs; }
54
55 void flush() = delete;
56
57 /**
58 * Resets the amount of allocated space.
59 */
60 void reset() { m_left = 0; }
61
62 private:
63 void write_impl(const char* data, size_t len) override;
64 uint64_t current_pos() const override;
65
67 std::function<uv::Buffer()> m_alloc;
68
69 // How much allocated space is left in the current buffer.
70 size_t m_left = 0;
71};
72
73} // namespace wpi
74
75#endif // WPINET_RAW_UV_OSTREAM_H_
This file defines the SmallVector class.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:579
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:43
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:162
raw_ostream style output to a SmallVector of uv::Buffer buffers.
Definition: raw_uv_ostream.h:24
raw_uv_ostream(SmallVectorImpl< uv::Buffer > &bufs, std::function< uv::Buffer()> alloc)
Construct a new raw_uv_ostream.
Definition: raw_uv_ostream.h:42
void reset()
Resets the amount of allocated space.
Definition: raw_uv_ostream.h:60
std::span< uv::Buffer > bufs()
Returns an span to the buffers.
Definition: raw_uv_ostream.h:53
~raw_uv_ostream() override=default
raw_uv_ostream(SmallVectorImpl< uv::Buffer > &bufs, size_t allocSize)
Construct a new raw_uv_ostream.
Definition: raw_uv_ostream.h:32
void flush()=delete
Data buffer.
Definition: Buffer.h:23
static Buffer Allocate(size_t size)
Definition: Buffer.h:65
Definition: array.h:89
Definition: ntcore_cpp.h:26