OpenTTD Source: src/core/string_inplace.hpp Source File

OpenTTD Source 15.0-RC2

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
12#ifndef STRING_INPLACE_HPP
13#define STRING_INPLACE_HPP
14
15#include "string_builder.hpp"
16#include "string_consumer.hpp"
17
21 class InPlaceBuilder final : public BaseStringBuilder {
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
55 class back_insert_iterator {
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 };
79 back_insert_iterator back_inserter()
80 {
81 return back_insert_iterator(*this);
82 }
83};
84
91 class InPlaceReplacement {
92public:
93 StringConsumer consumer;
94 InPlaceBuilder builder;
95
96public:
97 InPlaceReplacement(std::span<char> buffer);
98 InPlaceReplacement(const InPlaceReplacement &src);
99 InPlaceReplacement& operator=(const InPlaceReplacement &src);
100};
101
102#endif /* STRING_INPLACE_HPP */
BaseStringBuilder
Compose data into a string / buffer.
Definition string_builder.hpp:20
BaseStringBuilder::PutChar
void PutChar(char c)
Append 8-bit char.
Definition string_builder.cpp:105
InPlaceBuilder::back_insert_iterator
Implementation of std::back_insert_iterator for non-growing destination buffer.
Definition string_inplace.hpp:55
InPlaceBuilder
Builder implementation for InPlaceReplacement.
Definition string_inplace.hpp:21
InPlaceBuilder::AnyBytesWritten
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.
Definition string_inplace.hpp:37
InPlaceBuilder::AnyBytesUnused
bool AnyBytesUnused() const noexcept
Check whether any unused bytes are left between the Builder and Consumer position.
Definition string_inplace.cpp:19
InPlaceBuilder::GetWrittenData
std::string_view GetWrittenData() const noexcept
Get already written data.
Definition string_inplace.hpp:45
InPlaceBuilder::back_inserter
back_insert_iterator back_inserter()
Create a back-insert-iterator.
Definition string_inplace.hpp:79
InPlaceBuilder::GetBytesUnused
size_type GetBytesUnused() const noexcept
Get number of unused bytes left between the Builder and Consumer position.
Definition string_inplace.cpp:27
InPlaceBuilder::GetBytesWritten
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
Definition string_inplace.hpp:41
InPlaceBuilder::PutBuffer
void PutBuffer(std::span< const char > str) override
Append buffer.
Definition string_inplace.cpp:35
InPlaceReplacement
Compose data into a fixed size buffer, which is consumed at the same time.
Definition string_inplace.hpp:91
InPlaceReplacement::builder
InPlaceBuilder builder
Builder into shared buffer.
Definition string_inplace.hpp:94
InPlaceReplacement::consumer
StringConsumer consumer
Consumer from shared buffer.
Definition string_inplace.hpp:93
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
operator++
constexpr enum_type & operator++(enum_type &e)
Prefix increment.
Definition enum_type.hpp:30
string_builder.hpp
Compose strings from textual and binary data.
string_consumer.hpp
Parse strings.