Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rts/Lua/LuaHandleSynced.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "System/RangesCompat.h"
#include "LuaHandleSynced.h"

#include "LuaInclude.h"
Expand Down Expand Up @@ -1220,7 +1221,7 @@ bool CSyncedLuaHandle::ResourceExcess(const std::map <int, SResourcePack>& exces

for (const auto &[teamID, excess] : excesses) {
lua_createtable(L, excess.MAX_RESOURCES, 0);
for (const auto &[resourceID, resource] : std::views::enumerate(excess)) {
for (const auto &[resourceID, resource] : spring::views::enumerate(excess)) {
lua_pushnumber(L, resource);
lua_rawseti(L, -2, resourceID + 1);
}
Expand Down
3 changes: 2 additions & 1 deletion rts/Sim/Units/Unit.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "System/RangesCompat.h"
#include "UnitDef.h"
#include "Unit.h"
#include "UnitHandler.h"
Expand Down Expand Up @@ -965,7 +966,7 @@ static auto SplitResourcePackIntoPositiveNegative (const SResourcePack &pack)
{
SResourcePack positive {0.0f}, negative {0.0f};

for (auto [resourceID, value] : std::views::enumerate (pack)) {
for (auto [resourceID, value] : spring::views::enumerate (pack)) {
if (value < 0.0f)
negative[resourceID] = -value;
else
Expand Down
31 changes: 17 additions & 14 deletions rts/System/MemPoolTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstddef>
#include <cstring> // memset
#include <cmath>
#include <algorithm>
#include <array>
#include <deque>
#include <vector>
Expand Down Expand Up @@ -62,14 +63,16 @@ template<uint32_t NumBuckets, size_t BucketSize> struct PassThroughPool {
};

// Helper to infer the memory alignment and size from a set of types.
template <class ...T>
#if 0 // doesn't compile on MSVC 19.37
struct TypesMem {
alignas(alignof(T)...) uint8_t data[std::max({sizeof(T)...})];
};
#else
using TypesMem = std::aligned_storage_t< std::max({ sizeof(T)... }), std::max({ alignof(T)... }) >;
#endif
template<class... T>
inline constexpr size_t TypesMemAlignment = std::max({alignof(T)...});

template<class... T>
inline constexpr size_t TypesMemSize = [] {
constexpr size_t alignment = TypesMemAlignment<T...>;
constexpr size_t size = std::max({sizeof(T)...});

return ((size + alignment - 1) / alignment) * alignment;
}();

template<size_t S, size_t Alignment> struct DynMemPool {
public:
Expand Down Expand Up @@ -163,8 +166,8 @@ template<size_t S, size_t Alignment> struct DynMemPool {
};

// Helper to infer the DynMemPool pool parameters from a types.
template<class ...T>
using DynMemPoolT = DynMemPool<sizeof(TypesMem<T...>), alignof(TypesMem<T...>)>;
template<class... T>
using DynMemPoolT = DynMemPool<TypesMemSize<T...>, TypesMemAlignment<T...>>;

// fixed-size dynamic version
// page size per chunk, number of chunks, number of pages per chunk
Expand Down Expand Up @@ -286,8 +289,8 @@ template<size_t S, size_t N, size_t K, size_t Alignment> struct FixedDynMemPool
};

// Helper to infer the FixedDynMemPool pool parameters from a types.
template<size_t N, size_t K, class ...T>
using FixedDynMemPoolT = FixedDynMemPool<sizeof(TypesMem<T...>), N, K, alignof(TypesMem<T...>)>;
template<size_t N, size_t K, class... T>
using FixedDynMemPoolT = FixedDynMemPool<TypesMemSize<T...>, N, K, TypesMemAlignment<T...>>;

// fixed-size version.
template<size_t N, size_t S, size_t Alignment> struct StaticMemPool {
Expand Down Expand Up @@ -372,8 +375,8 @@ template<size_t N, size_t S, size_t Alignment> struct StaticMemPool {
};

// Helper to infer the StaticMemPool pool parameters from a types.
template<size_t N, class ...T>
using StaticMemPoolT = StaticMemPool<N, sizeof(TypesMem<T...>), alignof(TypesMem<T...>)>;
template<size_t N, class... T>
using StaticMemPoolT = StaticMemPool<N, TypesMemSize<T...>, TypesMemAlignment<T...>>;


// dynamic memory allocator operating with stable index positions
Expand Down
48 changes: 48 additions & 0 deletions rts/System/RangesCompat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef RANGES_COMPAT_H
#define RANGES_COMPAT_H

#include <version>
#include <iterator>
#include <ranges>
#include <utility>

namespace spring::views {

#ifdef __cpp_lib_ranges_enumerate

using std::views::enumerate;

#else

template<typename Rng> auto enumerate(Rng& rng)
{
struct Iterator {
decltype(std::begin(rng)) it;
std::ptrdiff_t idx;

// the element half stays a reference, copying it would turn a
// mutating loop into a no-op
auto operator * () const { return std::pair<std::ptrdiff_t, decltype(*it)>(idx, *it); }
Iterator& operator ++ () { ++it; ++idx; return *this; }
bool operator != (const Iterator& o) const { return it != o.it; }
};

struct View {
Rng& rng;
Iterator begin() const { return {std::begin(rng), 0}; }
Iterator end () const { return {std::end (rng), 0}; }
};

return View{rng};
}

// the view holds a reference, so a temporary would dangle
template<typename Rng> void enumerate(Rng&&) = delete;

#endif

}

#endif // RANGES_COMPAT_H
11 changes: 6 additions & 5 deletions rts/System/Sync/DumpState.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "System/RangesCompat.h"
#include <string>
#include <fstream>
#include <vector>
Expand Down Expand Up @@ -687,15 +688,15 @@ void DumpState(int newMinFrameNum, int newMaxFrameNum, int newFramePeriod, std::
const CTeam* t = teamHandler.Team(a);

file << "\t\tteamID: " << t->teamNum << " (controller: " << t->GetControllerName() << ")\n";
for (const auto &[resourceID, value] : std::views::enumerate(t->res))
for (const auto &[resourceID, value] : spring::views::enumerate(t->res))
file << "\t\t\tstored[" << resourceID << "]: " << TapFloats(value);
for (const auto &[resourceID, value] : std::views::enumerate(t->resStorage))
for (const auto &[resourceID, value] : spring::views::enumerate(t->resStorage))
file << "\t\t\tmaxStorage[" << resourceID << "]: " << TapFloats(value);
for (const auto &[resourceID, value] : std::views::enumerate(t->resPull))
for (const auto &[resourceID, value] : spring::views::enumerate(t->resPull))
file << "\t\t\tpull[" << resourceID << "]: " << TapFloats(value);
for (const auto &[resourceID, value] : std::views::enumerate(t->resIncome))
for (const auto &[resourceID, value] : spring::views::enumerate(t->resIncome))
file << "\t\t\tincome[" << resourceID << "]: " << TapFloats(value);
for (const auto &[resourceID, value] : std::views::enumerate(t->resExpense))
for (const auto &[resourceID, value] : spring::views::enumerate(t->resExpense))
file << "\t\t\texpense[" << resourceID << "]: " << TapFloats(value);
}
#endif
Expand Down
Loading