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

OpenTTD Source 15.0-RC2

string_inplace.cpp
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#include "../stdafx.h"
13#include "string_inplace.hpp"
14#include "../safeguards.h"
15
19[[nodiscard]] bool InPlaceBuilder::AnyBytesUnused() const noexcept
20{
21 return this->consumer.GetBytesRead() > this->position;
22}
23
27[[nodiscard]] InPlaceBuilder::size_type InPlaceBuilder::GetBytesUnused() const noexcept
28{
29 return this->consumer.GetBytesRead() - this->position;
30}
31
35 void InPlaceBuilder::PutBuffer(std::span<const char> str)
36{
37 auto unused = this->GetBytesUnused();
38 if (str.size() > unused) NOT_REACHED();
39 std::ranges::copy(str, this->dest.data() + this->position);
40 this->position += str.size();
41}
42
48InPlaceReplacement::InPlaceReplacement(std::span<char> buffer)
49 : consumer(buffer), builder(buffer, consumer)
50{
51}
52
53 InPlaceReplacement::InPlaceReplacement(const InPlaceReplacement &src)
54 : consumer(src.consumer), builder(src.builder, consumer)
55{
56}
57
58 InPlaceReplacement& InPlaceReplacement::operator=(const InPlaceReplacement &src)
59{
60 this->consumer = src.consumer;
61 this->builder.AssignBuffer(src.builder);
62 return *this;
63}
InPlaceBuilder::back_insert_iterator
Implementation of std::back_insert_iterator for non-growing destination buffer.
Definition string_inplace.hpp:55
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::GetBytesUnused
size_type GetBytesUnused() const noexcept
Get number of unused bytes left between the Builder and Consumer position.
Definition string_inplace.cpp:27
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::InPlaceReplacement
InPlaceReplacement(std::span< char > buffer)
Create coupled Consumer+Builder pair.
Definition string_inplace.cpp:48
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
string_inplace.hpp
Inplace-replacement of textual and binary data.