OpenTTD Source 20260218-master-g2123fca5ea
string_inplace.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef STRING_INPLACE_HPP
11#define STRING_INPLACE_HPP
12
13#include "string_builder.hpp"
14#include "string_consumer.hpp"
15
19class InPlaceBuilder final : public BaseStringBuilder {
20 std::span<char> dest;
23
24 friend class InPlaceReplacement;
25
31 explicit InPlaceBuilder(std::span<char> dest, const StringConsumer &consumer) : dest(dest), consumer(consumer) {}
32
39
44 void AssignBuffer(const InPlaceBuilder &src) { this->dest = src.dest; this->position = src.position; }
45public:
46 InPlaceBuilder(const InPlaceBuilder &) = delete;
47 InPlaceBuilder& operator=(const InPlaceBuilder &) = delete;
48
53 [[nodiscard]] bool AnyBytesWritten() const noexcept { return this->position != 0; }
58 [[nodiscard]] size_type GetBytesWritten() const noexcept { return this->position; }
63 [[nodiscard]] std::string_view GetWrittenData() const noexcept { return {this->dest.data(), this->position}; }
64
65 [[nodiscard]] bool AnyBytesUnused() const noexcept;
66 [[nodiscard]] size_type GetBytesUnused() const noexcept;
67
68 void PutBuffer(std::span<const char> str) override;
69
75 public:
76 using value_type = void;
77 using difference_type = void;
78 using iterator_category = std::output_iterator_tag;
79 using pointer = void;
80 using reference = void;
81
87
88 back_insert_iterator &operator++() { return *this; }
89 back_insert_iterator operator++(int) { return *this; }
90 back_insert_iterator &operator*() { return *this; }
91
92 back_insert_iterator &operator=(char value)
93 {
94 this->parent->PutChar(value);
95 return *this;
96 }
97 };
98
106};
107
115public:
118
119public:
120 InPlaceReplacement(std::span<char> buffer);
122 InPlaceReplacement& operator=(const InPlaceReplacement &src);
123};
124
125#endif /* STRING_INPLACE_HPP */
Compose data into a string / buffer.
void PutChar(char c)
Append 8-bit char.
std::string_view::size_type size_type
The type of the size of our strings.
Implementation of std::back_insert_iterator for non-growing destination buffer.
back_insert_iterator(InPlaceBuilder &parent)
Create the iterator.
std::output_iterator_tag iterator_category
C++ specification trait 'iterator_category' of std::back_insert_iterator.
void difference_type
C++ specification trait 'difference_type' of std::back_insert_iterator.
void reference
C++ specification trait 'reference' of std::back_insert_iterator.
void value_type
C++ specification trait 'value_type' of std::back_insert_iterator.
InPlaceBuilder * parent
The builder we belong to.
void pointer
C++ specification trait 'pointer' of std::back_insert_iterator.
Builder implementation for InPlaceReplacement.
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.
void AssignBuffer(const InPlaceBuilder &src)
Copy assignment of the buffer and its position.
bool AnyBytesUnused() const noexcept
Check whether any unused bytes are left between the Builder and Consumer position.
std::string_view GetWrittenData() const noexcept
Get already written data.
InPlaceBuilder(std::span< char > dest, const StringConsumer &consumer)
Create this builder.
back_insert_iterator back_inserter()
Create a back-insert-iterator.
size_type GetBytesUnused() const noexcept
Get number of unused bytes left between the Builder and Consumer position.
InPlaceBuilder(const InPlaceBuilder &src, const StringConsumer &consumer)
Create this builder based on an existing builder's buffer.
const StringConsumer & consumer
The string consumer to read with.
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
std::span< char > dest
The buffer to process.
size_type position
The location to write to.
void PutBuffer(std::span< const char > str) override
Append buffer.
InPlaceReplacement(std::span< char > buffer)
Create coupled Consumer+Builder pair.
InPlaceBuilder builder
Builder into shared buffer.
StringConsumer consumer
Consumer from shared buffer.
Parse data from a string / buffer.
constexpr enum_type & operator++(enum_type &e)
Prefix increment.
Definition enum_type.hpp:41
Compose strings from textual and binary data.
Parse strings.