Remove: replace custom span with std::span

This commit is contained in:
Patric Stout 2024-01-16 22:46:00 +01:00 committed by Patric Stout
parent bb49112784
commit fd073a2810
15 changed files with 22 additions and 138 deletions

View File

@ -158,7 +158,7 @@ SpriteID CargoSpec::GetCargoIcon() const
std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
/** Sort cargo specifications by their name. */
static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)

View File

@ -16,7 +16,6 @@
#include "strings_type.h"
#include "landscape_type.h"
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
/** Globally unique label of a cargo type. */
typedef uint32_t CargoLabel;
@ -190,7 +189,7 @@ Dimension GetLargestCargoIconSize();
void InitializeSortedCargoSpecs();
extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
extern std::vector<const CargoSpec *> _sorted_cargo_specs;
extern span<const CargoSpec *> _sorted_standard_cargo_specs;
extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
/**
* Does cargo \a c have cargo class \a cc?

View File

@ -25,6 +25,5 @@ add_files(
random_func.hpp
smallstack_type.hpp
container_func.hpp
span_type.hpp
strong_typedef_type.hpp
)

View File

@ -1,110 +0,0 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file span_type.hpp Minimized implementation of C++20 std::span. */
#ifndef CORE_SPAN_TYPE_HPP
#define CORE_SPAN_TYPE_HPP
/* This is a partial copy/paste from https://github.com/gsl-lite/gsl-lite/blob/master/include/gsl/gsl-lite.hpp */
/* Template to check if a template variable defines size() and data(). */
template <class, class = void>
struct has_size_and_data : std::false_type{};
template <class C>
struct has_size_and_data
<
C, std::void_t<
decltype(std::size(std::declval<C>())),
decltype(std::data(std::declval<C>()))>
> : std::true_type{};
/* Template to check if two elements are compatible. */
template <class, class, class = void>
struct is_compatible_element : std::false_type {};
template <class C, class E>
struct is_compatible_element
<
C, E, std::void_t<
decltype(std::data(std::declval<C>())),
typename std::remove_pointer_t<decltype(std::data( std::declval<C&>()))>(*)[]>
> : std::is_convertible<typename std::remove_pointer_t<decltype(std::data(std::declval<C&>()))>(*)[], E(*)[]>{};
/* Template to check if a container is compatible. gsl-lite also includes is_array and is_std_array, but as we don't use them, they are omitted. */
template <class C, class E>
struct is_compatible_container : std::bool_constant
<
has_size_and_data<C>::value
&& is_compatible_element<C, E>::value
>{};
/**
* A trimmed down version of what std::span will be in C++20.
*
* It is fully forwards compatible, so if this codebase switches to C++20,
* all "span" instances can be replaced by "std::span" without loss of
* functionality.
*
* Currently it only supports basic functionality:
* - size() and friends
* - begin() and friends
*
* It is meant to simplify function parameters, where we only want to walk
* a continuous list.
*/
template<class T>
class span {
public:
typedef T element_type;
typedef typename std::remove_cv< T >::type value_type;
typedef T &reference;
typedef T *pointer;
typedef const T &const_reference;
typedef const T *const_pointer;
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
constexpr span() noexcept : first(nullptr), last(nullptr) {}
constexpr span(pointer data_in, size_t size_in) : first(data_in), last(data_in + size_in) {}
template<class Container, typename std::enable_if<(is_compatible_container<Container, element_type>::value), int>::type = 0>
constexpr span(Container &list) noexcept : first(std::data(list)), last(std::data(list) + std::size(list)) {}
template<class Container, typename std::enable_if<(std::is_const<element_type>::value && is_compatible_container<Container, element_type>::value), int>::type = 0>
constexpr span(const Container &list) noexcept : first(std::data(list)), last(std::data(list) + std::size(list)) {}
constexpr size_t size() const noexcept { return static_cast<size_t>( last - first ); }
constexpr std::ptrdiff_t ssize() const noexcept { return static_cast<std::ptrdiff_t>( last - first ); }
constexpr bool empty() const noexcept { return this->size() == 0; }
constexpr iterator begin() const noexcept { return iterator(first); }
constexpr iterator end() const noexcept { return iterator(last); }
constexpr const_iterator cbegin() const noexcept { return const_iterator(first); }
constexpr const_iterator cend() const noexcept { return const_iterator(last); }
constexpr reference operator[](size_type idx) const { return first[idx]; }
constexpr pointer data() const noexcept { return first; }
constexpr span<element_type> subspan(size_t offset, size_t count)
{
assert(offset + count <= size());
return span(this->data() + offset, count);
}
private:
pointer first;
pointer last;
};
#endif /* CORE_SPAN_TYPE_HPP */

View File

@ -11,7 +11,6 @@
#define ENDIAN_BUFFER_HPP
#include <string_view>
#include "../core/span_type.hpp"
#include "../core/bitmath_func.hpp"
#include "../core/overflowsafe_type.hpp"
@ -119,12 +118,12 @@ private:
*/
class EndianBufferReader {
/** Reference to storage buffer. */
span<const byte> buffer;
std::span<const byte> buffer;
/** Current read position. */
size_t read_pos = 0;
public:
EndianBufferReader(span<const byte> buffer) : buffer(buffer) {}
EndianBufferReader(std::span<const byte> buffer) : buffer(buffer) {}
void rewind() { this->read_pos = 0; }
@ -155,7 +154,7 @@ public:
}
template <typename Tvalue>
static Tvalue ToValue(span<const byte> buffer)
static Tvalue ToValue(std::span<const byte> buffer)
{
Tvalue result{};
EndianBufferReader reader{ buffer };

View File

@ -586,7 +586,7 @@ private:
StringID GetCompanyMessageString() const
{
/* Company news with a face have a separate headline, so the normal message is shifted by two params */
CopyInDParam(span(this->ni->params.data() + 2, this->ni->params.size() - 2));
CopyInDParam(std::span(this->ni->params.data() + 2, this->ni->params.size() - 2));
return this->ni->params[1].data;
}

View File

@ -13,7 +13,6 @@
#include "saveload_error.hpp"
#include "../fileio_type.h"
#include "../fios.h"
#include "../core/span_type.hpp"
/** SaveLoad versions
* Previous savegame versions, the trunk revision where they were
@ -480,13 +479,13 @@ struct ChunkHandler {
using ChunkHandlerRef = std::reference_wrapper<const ChunkHandler>;
/** A table of ChunkHandler entries. */
using ChunkHandlerTable = span<const ChunkHandlerRef>;
using ChunkHandlerTable = std::span<const ChunkHandlerRef>;
/** A table of SaveLoad entries. */
using SaveLoadTable = span<const struct SaveLoad>;
using SaveLoadTable = std::span<const struct SaveLoad>;
/** A table of SaveLoadCompat entries. */
using SaveLoadCompatTable = span<const struct SaveLoadCompat>;
using SaveLoadCompatTable = std::span<const struct SaveLoadCompat>;
/** Handler for saving/loading an object to/from disk. */
class SaveLoadHandler {

View File

@ -360,7 +360,7 @@ static constexpr const SettingDesc *GetSettingDesc(const SettingVariant &desc)
return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc);
}
typedef span<const SettingVariant> SettingTable;
typedef std::span<const SettingVariant> SettingTable;
const SettingDesc *GetSettingFromName(const std::string_view name);
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector<SaveLoad> &saveloads);

View File

@ -70,6 +70,7 @@
#include <numeric>
#include <optional>
#include <set>
#include <span>
#include <stdexcept>
#include <string>
#include <type_traits>

View File

@ -85,7 +85,7 @@ char *strecpy(char *dst, const char *src, const char *last)
* @param data Array to format
* @return Converted string.
*/
std::string FormatArrayAsHex(span<const byte> data)
std::string FormatArrayAsHex(std::span<const byte> data)
{
std::string str;
str.reserve(data.size() * 2 + 1);

View File

@ -15,12 +15,11 @@
#include <iosfwd>
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
#include "string_type.h"
char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
std::string FormatArrayAsHex(span<const byte> data);
std::string FormatArrayAsHex(std::span<const byte> data);
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);

View File

@ -155,7 +155,7 @@ void SetDParamMaxDigits(size_t n, uint count, FontSize size)
* Copy the parameters from the backup into the global string parameter array.
* @param backup The backup to copy from.
*/
void CopyInDParam(const span<const StringParameterBackup> backup)
void CopyInDParam(const std::span<const StringParameterBackup> backup)
{
for (size_t i = 0; i < backup.size(); i++) {
auto &value = backup[i];

View File

@ -14,7 +14,6 @@
#include "string_type.h"
#include "gfx_type.h"
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
#include "core/strong_typedef_type.hpp"
#include "vehicle_type.h"
@ -99,7 +98,7 @@ void SetDParamStr(size_t n, const char *str);
void SetDParamStr(size_t n, const std::string &str);
void SetDParamStr(size_t n, std::string &&str);
void CopyInDParam(const span<const StringParameterBackup> backup);
void CopyInDParam(const std::span<const StringParameterBackup> backup);
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num);
bool HaveDParamChanged(const std::vector<StringParameterBackup> &backup);

View File

@ -12,7 +12,6 @@
#include "strings_func.h"
#include "string_func.h"
#include "core/span_type.hpp"
/** The data required to format and validate a single parameter of a string. */
struct StringParameter {
@ -25,12 +24,12 @@ struct StringParameter {
class StringParameters {
protected:
StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance.
span<StringParameter> parameters = {}; ///< Array with the actual parameters.
std::span<StringParameter> parameters = {}; ///< Array with the actual parameters.
size_t offset = 0; ///< Current offset in the parameters span.
char32_t next_type = 0; ///< The type of the next data that is retrieved.
StringParameters(span<StringParameter> parameters = {}) :
StringParameters(std::span<StringParameter> parameters = {}) :
parameters(parameters)
{}
@ -211,7 +210,7 @@ class ArrayStringParameters : public StringParameters {
public:
ArrayStringParameters()
{
this->parameters = span(params.data(), params.size());
this->parameters = std::span(params.data(), params.size());
}
ArrayStringParameters(ArrayStringParameters&& other) noexcept
@ -224,7 +223,7 @@ public:
this->offset = other.offset;
this->next_type = other.next_type;
this->params = std::move(other.params);
this->parameters = span(params.data(), params.size());
this->parameters = std::span(params.data(), params.size());
return *this;
}

View File

@ -250,9 +250,9 @@ void SurveyConfiguration(nlohmann::json &survey)
survey["graphics_set"] = fmt::format("{}.{}", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
const GRFConfig *extra_cfg = BaseGraphics::GetUsedSet()->GetExtraConfig();
if (extra_cfg != nullptr && extra_cfg->num_params > 0) {
survey["graphics_set_parameters"] = span<const uint32_t>(extra_cfg->param.data(), extra_cfg->num_params);
survey["graphics_set_parameters"] = std::span<const uint32_t>(extra_cfg->param.data(), extra_cfg->num_params);
} else {
survey["graphics_set_parameters"] = span<const uint32_t>();
survey["graphics_set_parameters"] = std::span<const uint32_t>();
}
}
if (BaseMusic::GetUsedSet() != nullptr) {
@ -344,7 +344,7 @@ void SurveyGrfs(nlohmann::json &survey)
if ((c->palette & GRFP_BLT_MASK) == GRFP_BLT_32BPP) grf["blitter"] = "32bpp";
grf["is_static"] = HasBit(c->flags, GCF_STATIC);
grf["parameters"] = span<const uint32_t>(c->param.data(), c->num_params);
grf["parameters"] = std::span<const uint32_t>(c->param.data(), c->num_params);
}
}