Merge branch 'master' into jgrpp
# Conflicts: # src/cargotype.h # src/core/CMakeLists.txt # src/core/span_type.hpp # src/fileio.cpp # src/fios.cpp # src/misc/endian_buffer.hpp # src/misc_gui.cpp # src/saveload/saveload.h # src/saveload/vehicle_sl.cpp # src/screenshot.cpp # src/settings.cpp # src/settings_internal.h # src/stdafx.h # src/string_func.h # src/strings.cpp # src/strings_func.h # src/strings_internal.h
This commit is contained in:
@@ -33,7 +33,6 @@ add_files(
|
||||
serialisation.cpp
|
||||
serialisation.hpp
|
||||
smallstack_type.hpp
|
||||
span_type.hpp
|
||||
strong_typedef_type.hpp
|
||||
tinystring_type.hpp
|
||||
y_combinator.hpp
|
||||
|
@@ -391,38 +391,6 @@ inline bool HasAtMostOneBit(T value)
|
||||
return (value & (value - 1)) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ROtate \a x Left by \a n
|
||||
*
|
||||
* @note Assumes a byte has 8 bits
|
||||
* @param x The value which we want to rotate
|
||||
* @param n The number how many we want to rotate
|
||||
* @pre n < sizeof(T) * 8
|
||||
* @return A bit rotated number
|
||||
*/
|
||||
template <typename T>
|
||||
inline T ROL(const T x, const uint8_t n)
|
||||
{
|
||||
if (n == 0) return x;
|
||||
return (T)(x << n | x >> (sizeof(x) * 8 - n));
|
||||
}
|
||||
|
||||
/**
|
||||
* ROtate \a x Right by \a n
|
||||
*
|
||||
* @note Assumes a byte has 8 bits
|
||||
* @param x The value which we want to rotate
|
||||
* @param n The number how many we want to rotate
|
||||
* @pre n < sizeof(T) * 8
|
||||
* @return A bit rotated number
|
||||
*/
|
||||
template <typename T>
|
||||
inline T ROR(const T x, const uint8_t n)
|
||||
{
|
||||
if (n == 0) return x;
|
||||
return (T)(x >> n | x << (sizeof(x) * 8 - n));
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterable ensemble of each set bit in a value.
|
||||
* @tparam Tbitpos Type of the position variable.
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include "../fileio_func.h"
|
||||
#include "../date_func.h"
|
||||
#include "../debug.h"
|
||||
#include <bit>
|
||||
#endif /* RANDOM_DEBUG */
|
||||
|
||||
struct SimpleChecksum64 {
|
||||
@@ -27,7 +28,7 @@ struct SimpleChecksum64 {
|
||||
|
||||
void Update(uint64_t input)
|
||||
{
|
||||
this->state = ROL(this->state, 1) ^ input ^ 0x123456789ABCDEF7ULL;
|
||||
this->state = std::rotl(this->state, 1) ^ input ^ 0x123456789ABCDEF7ULL;
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#include "../stdafx.h"
|
||||
#include "random_func.hpp"
|
||||
#include "bitmath_func.hpp"
|
||||
#include <bit>
|
||||
|
||||
#ifdef RANDOM_DEBUG
|
||||
#include "../network/network.h"
|
||||
@@ -33,8 +34,8 @@ uint32_t Randomizer::Next()
|
||||
const uint32_t s = this->state[0];
|
||||
const uint32_t t = this->state[1];
|
||||
|
||||
this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
|
||||
return this->state[1] = ROR(s, 3) - 1;
|
||||
this->state[0] = s + std::rotr(t ^ 0x1234567F, 7) + 1;
|
||||
return this->state[1] = std::rotr(s, 3) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -11,7 +11,6 @@
|
||||
#define SERIALISATION_HPP
|
||||
|
||||
#include "bitmath_func.hpp"
|
||||
#include "span_type.hpp"
|
||||
#include "../string_type.h"
|
||||
#include "../string_func.h"
|
||||
|
||||
@@ -269,13 +268,13 @@ public:
|
||||
* @param size The size of the data.
|
||||
* @return The view of the data.
|
||||
*/
|
||||
span<const uint8_t> Recv_binary_view(size_t size)
|
||||
std::span<const uint8_t> Recv_binary_view(size_t size)
|
||||
{
|
||||
if (!this->CanRecvBytes(size, true)) return {};
|
||||
|
||||
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
||||
|
||||
span<const uint8_t> view { &this->GetBuffer()[pos], size };
|
||||
std::span<const uint8_t> view { &this->GetBuffer()[pos], size };
|
||||
pos += (decltype(pos)) size;
|
||||
|
||||
return view;
|
||||
@@ -288,7 +287,7 @@ public:
|
||||
*/
|
||||
std::vector<uint8_t> Recv_binary(size_t size)
|
||||
{
|
||||
span<const uint8_t> view = this->Recv_binary_view(size);
|
||||
std::span<const uint8_t> view = this->Recv_binary_view(size);
|
||||
|
||||
return { view.begin(), view.end() };
|
||||
}
|
||||
@@ -297,14 +296,14 @@ public:
|
||||
* Returns a view of a length-prefixed binary buffer from the packet.
|
||||
* @return The binary buffer.
|
||||
*/
|
||||
span<const uint8_t> Recv_buffer_view()
|
||||
std::span<const uint8_t> Recv_buffer_view()
|
||||
{
|
||||
uint16_t length = this->Recv_uint16();
|
||||
|
||||
if (!this->CanRecvBytes(length, true)) return {};
|
||||
|
||||
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
||||
span<const uint8_t> buffer { &this->GetBuffer()[pos], length };
|
||||
std::span<const uint8_t> buffer { &this->GetBuffer()[pos], length };
|
||||
pos += length;
|
||||
|
||||
return buffer;
|
||||
@@ -316,7 +315,7 @@ public:
|
||||
*/
|
||||
std::vector<uint8_t> Recv_buffer()
|
||||
{
|
||||
span<const uint8_t> view = this->Recv_buffer_view();
|
||||
std::span<const uint8_t> view = this->Recv_buffer_view();
|
||||
|
||||
return { view.begin(), view.end() };
|
||||
}
|
||||
|
@@ -1,109 +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<decltype(std::data( std::declval<C&>()))>::type(*)[]>
|
||||
> : std::is_convertible<typename std::remove_pointer<decltype(std::data(std::declval<C&>()))>::type(*)[], 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 pointer data() const noexcept { return first; }
|
||||
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 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 */
|
Reference in New Issue
Block a user