OpenTTD Source 20250523-master-g321f7e8683
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 <http://www.gnu.org/licenses/>.
6 */
7
12#ifndef STRING_INPLACE_HPP
13#define STRING_INPLACE_HPP
14
15#include "string_builder.hpp"
16#include "string_consumer.hpp"
17
22 std::span<char> dest;
23 size_type position = 0;
24 const StringConsumer &consumer;
25
26 friend class InPlaceReplacement;
27 explicit InPlaceBuilder(std::span<char> dest, const StringConsumer &consumer) : dest(dest), consumer(consumer) {}
28 InPlaceBuilder(const InPlaceBuilder &src, const StringConsumer &consumer) : dest(src.dest), position(src.position), consumer(consumer) {}
29 void AssignBuffer(const InPlaceBuilder &src) { this->dest = src.dest; this->position = src.position; }
30public:
31 InPlaceBuilder(const InPlaceBuilder &) = delete;
32 InPlaceBuilder& operator=(const InPlaceBuilder &) = delete;
33
37 [[nodiscard]] bool AnyBytesWritten() const noexcept { return this->position != 0; }
41 [[nodiscard]] size_type GetBytesWritten() const noexcept { return this->position; }
45 [[nodiscard]] std::string_view GetWrittenData() const noexcept { return {this->dest.data(), this->position}; }
46
47 [[nodiscard]] bool AnyBytesUnused() const noexcept;
48 [[nodiscard]] size_type GetBytesUnused() const noexcept;
49
50 void PutBuffer(std::span<const char> str) override;
51
56 InPlaceBuilder *parent = nullptr;
57 public:
58 using value_type = void;
59 using difference_type = void;
60 using iterator_category = std::output_iterator_tag;
61 using pointer = void;
62 using reference = void;
63
64 back_insert_iterator(InPlaceBuilder &parent) : parent(&parent) {}
65
66 back_insert_iterator &operator++() { return *this; }
67 back_insert_iterator operator++(int) { return *this; }
68 back_insert_iterator &operator*() { return *this; }
69
70 back_insert_iterator &operator=(char value)
71 {
72 this->parent->PutChar(value);
73 return *this;
74 }
75 };
83};
84
92public:
95
96public:
97 InPlaceReplacement(std::span<char> buffer);
99 InPlaceReplacement& operator=(const InPlaceReplacement &src);
100};
101
102#endif /* STRING_INPLACE_HPP */
Compose data into a string / buffer.
void PutChar(char c)
Append 8-bit char.
Implementation of std::back_insert_iterator for non-growing destination buffer.
Builder implementation for InPlaceReplacement.
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.
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.
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.
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
void PutBuffer(std::span< const char > str) override
Append buffer.
Compose data into a fixed size buffer, which is consumed at the same time.
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:30
Compose strings from textual and binary data.
Parse strings.