diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 925b8a98f47ec..dafb4e11643e2 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -158,7 +158,7 @@ SpriteID CargoSpec::GetCargoIcon() const std::array _sorted_cargo_types; ///< Sort order of cargoes by cargo ID. std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. -span _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name. +std::span _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) diff --git a/src/cargotype.h b/src/cargotype.h index bc8f6f4043441..50c89b237d3e6 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -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 _sorted_cargo_types; extern std::vector _sorted_cargo_specs; -extern span _sorted_standard_cargo_specs; +extern std::span _sorted_standard_cargo_specs; /** * Does cargo \a c have cargo class \a cc? diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6dd7bbd69bf53..dc233bdd73124 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -25,6 +25,5 @@ add_files( random_func.hpp smallstack_type.hpp container_func.hpp - span_type.hpp strong_typedef_type.hpp ) diff --git a/src/core/span_type.hpp b/src/core/span_type.hpp deleted file mode 100644 index 9a39bdb529e3a..0000000000000 --- a/src/core/span_type.hpp +++ /dev/null @@ -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 . - */ - -/** @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 -struct has_size_and_data : std::false_type{}; -template -struct has_size_and_data -< - C, std::void_t< - decltype(std::size(std::declval())), - decltype(std::data(std::declval()))> -> : std::true_type{}; - -/* Template to check if two elements are compatible. */ -template -struct is_compatible_element : std::false_type {}; -template -struct is_compatible_element -< - C, E, std::void_t< - decltype(std::data(std::declval())), - typename std::remove_pointer_t()))>(*)[]> -> : std::is_convertible()))>(*)[], 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 -struct is_compatible_container : std::bool_constant -< - has_size_and_data::value - && is_compatible_element::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 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::value), int>::type = 0> - constexpr span(Container &list) noexcept : first(std::data(list)), last(std::data(list) + std::size(list)) {} - template::value && is_compatible_container::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( last - first ); } - constexpr std::ptrdiff_t ssize() const noexcept { return static_cast( 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 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 */ diff --git a/src/misc/endian_buffer.hpp b/src/misc/endian_buffer.hpp index 749657786cc8b..3229029c53569 100644 --- a/src/misc/endian_buffer.hpp +++ b/src/misc/endian_buffer.hpp @@ -11,7 +11,6 @@ #define ENDIAN_BUFFER_HPP #include -#include "../core/span_type.hpp" #include "../core/bitmath_func.hpp" #include "../core/overflowsafe_type.hpp" @@ -119,12 +118,12 @@ class EndianBufferWriter { */ class EndianBufferReader { /** Reference to storage buffer. */ - span buffer; + std::span buffer; /** Current read position. */ size_t read_pos = 0; public: - EndianBufferReader(span buffer) : buffer(buffer) {} + EndianBufferReader(std::span buffer) : buffer(buffer) {} void rewind() { this->read_pos = 0; } @@ -155,7 +154,7 @@ class EndianBufferReader { } template - static Tvalue ToValue(span buffer) + static Tvalue ToValue(std::span buffer) { Tvalue result{}; EndianBufferReader reader{ buffer }; diff --git a/src/news_gui.cpp b/src/news_gui.cpp index a6952600d2c36..afd894fee496b 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -586,7 +586,7 @@ struct NewsWindow : Window { 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; } diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index b3bd344cd955e..be139df5ab4de 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -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; /** A table of ChunkHandler entries. */ -using ChunkHandlerTable = span; +using ChunkHandlerTable = std::span; /** A table of SaveLoad entries. */ -using SaveLoadTable = span; +using SaveLoadTable = std::span; /** A table of SaveLoadCompat entries. */ -using SaveLoadCompatTable = span; +using SaveLoadCompatTable = std::span; /** Handler for saving/loading an object to/from disk. */ class SaveLoadHandler { diff --git a/src/settings_internal.h b/src/settings_internal.h index 914071909b55c..aada1a1379685 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -360,7 +360,7 @@ static constexpr const SettingDesc *GetSettingDesc(const SettingVariant &desc) return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc); } -typedef span SettingTable; +typedef std::span SettingTable; const SettingDesc *GetSettingFromName(const std::string_view name); void GetSaveLoadFromSettingTable(SettingTable settings, std::vector &saveloads); diff --git a/src/stdafx.h b/src/stdafx.h index edfd4f02a95cb..ab96be4e5ae00 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -70,6 +70,7 @@ #include #include #include +#include #include #include #include diff --git a/src/string.cpp b/src/string.cpp index 4eb45c2886b5c..c6b1a154a6762 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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 data) +std::string FormatArrayAsHex(std::span data) { std::string str; str.reserve(data.size() * 2 + 1); diff --git a/src/string_func.h b/src/string_func.h index ca23fdf18974c..bd0e5475584d9 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -15,12 +15,11 @@ #include #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 data); +std::string FormatArrayAsHex(std::span 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); diff --git a/src/strings.cpp b/src/strings.cpp index c25e366a5b9b9..6834135880846 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -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 backup) +void CopyInDParam(const std::span backup) { for (size_t i = 0; i < backup.size(); i++) { auto &value = backup[i]; diff --git a/src/strings_func.h b/src/strings_func.h index 4fe2746e8a17a..21a77705d8867 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -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 backup); +void CopyInDParam(const std::span backup); void CopyOutDParam(std::vector &backup, size_t num); bool HaveDParamChanged(const std::vector &backup); diff --git a/src/strings_internal.h b/src/strings_internal.h index 6584827e78287..fc28c3471696b 100644 --- a/src/strings_internal.h +++ b/src/strings_internal.h @@ -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 parameters = {}; ///< Array with the actual parameters. + std::span 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 parameters = {}) : + StringParameters(std::span 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 @@ class ArrayStringParameters : public StringParameters { 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; } diff --git a/src/survey.cpp b/src/survey.cpp index 3f6b1a1593ed8..7dc2d238f8c0e 100644 --- a/src/survey.cpp +++ b/src/survey.cpp @@ -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(extra_cfg->param.data(), extra_cfg->num_params); + survey["graphics_set_parameters"] = std::span(extra_cfg->param.data(), extra_cfg->num_params); } else { - survey["graphics_set_parameters"] = span(); + survey["graphics_set_parameters"] = std::span(); } } 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(c->param.data(), c->num_params); + grf["parameters"] = std::span(c->param.data(), c->num_params); } }