Upgrade to C++17

This commit is contained in:
Kae 2023-06-25 20:01:32 +10:00
parent df661be1a3
commit 13a74602bd
17 changed files with 82 additions and 54 deletions

View File

@ -46,7 +46,7 @@ flags = [
# a "-std=<something>". # a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of # For a C project, you would set this to something like 'c99' instead of
# 'c++11'. # 'c++11'.
'-std=c++14', '-std=c++17',
'-stdlib=libc++', '-stdlib=libc++',
# ...and the same thing goes for the magic -x option which specifies the # ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly # language that the files to be compiled are written in. This is mostly

View File

@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED (VERSION 3.4) CMAKE_MINIMUM_REQUIRED (VERSION 3.16)
# Find CCache # Find CCache
FIND_PROGRAM(CCACHE_PATH ccache) FIND_PROGRAM(CCACHE_PATH ccache)
@ -227,7 +227,7 @@ ENDIF ()
IF (STAR_COMPILER_GNU) IF (STAR_COMPILER_GNU)
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wuninitialized -Wunreachable-code -Wformat -no-pie") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wuninitialized -Wunreachable-code -Wformat -no-pie")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wuninitialized -Wunreachable-code -Wformat -no-pie") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wextra -Wuninitialized -Wunreachable-code -Wformat -no-pie")
IF (STAR_SYSTEM_FAMILY_WINDOWS) IF (STAR_SYSTEM_FAMILY_WINDOWS)
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mthreads") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mthreads")
@ -261,11 +261,11 @@ IF (STAR_COMPILER_GNU)
ELSEIF (STAR_COMPILER_CLANG) ELSEIF (STAR_COMPILER_CLANG)
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wuninitialized -Wno-parentheses-equality -Wno-deprecated-declarations") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wuninitialized -Wno-parentheses-equality -Wno-deprecated-declarations")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wuninitialized -Wno-parentheses-equality -Wno-deprecated-declarations") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wextra -Wuninitialized -Wno-parentheses-equality -Wno-deprecated-declarations")
IF (STAR_SYSTEM_MACOS) IF (STAR_SYSTEM_MACOS)
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
SET (CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++14") SET (CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++17")
SET (CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++") SET (CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
ELSEIF () ELSEIF ()
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -D_REENTRANT") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -D_REENTRANT")
@ -313,7 +313,7 @@ ELSEIF (STAR_COMPILER_MSVC)
# using unions in interesting ways. # using unions in interesting ways.
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP /EHsc /bigobj /wd4996 /wd4351 /wd4800 /wd4244 /wd4305 /wd4267 /wd4456 /wd4503 /wd4250 /wd4624") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP /EHsc /bigobj /wd4996 /wd4351 /wd4800 /wd4244 /wd4305 /wd4267 /wd4456 /wd4503 /wd4250 /wd4624")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /EHsc /bigobj /wd4996 /wd4351 /wd4800 /wd4244 /wd4305 /wd4267 /wd4456 /wd4503 /wd4250 /wd4624") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /MP /EHsc /bigobj /wd4996 /wd4351 /wd4800 /wd4244 /wd4305 /wd4267 /wd4456 /wd4503 /wd4250 /wd4624")
IF (STAR_ENABLE_STATIC_MSVC_RUNTIME) IF (STAR_ENABLE_STATIC_MSVC_RUNTIME)
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT")
@ -351,7 +351,7 @@ ELSEIF (STAR_COMPILER_MSVC)
ELSE () ELSE ()
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pthread -D_REENTRANT") SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pthread -D_REENTRANT")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -pthread -D_REENTRANT") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -pthread -D_REENTRANT")
SET (CMAKE_C_FLAGS_DEBUG "-g") SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_DEBUG "-g") SET (CMAKE_CXX_FLAGS_DEBUG "-g")

View File

@ -63,3 +63,4 @@ IF (STAR_ENABLE_DISCORD_INTEGRATION)
ENDIF () ENDIF ()
ADD_LIBRARY (star_application OBJECT ${star_application_SOURCES} ${star_application_HEADERS}) ADD_LIBRARY (star_application OBJECT ${star_application_SOURCES} ${star_application_HEADERS})
TARGET_PRECOMPILE_HEADERS (star_application REUSE_FROM star_core)

View File

@ -35,3 +35,4 @@ SET (star_base_SOURCES
CONFIGURE_FILE (StarVersion.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp) CONFIGURE_FILE (StarVersion.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
ADD_LIBRARY (star_base OBJECT ${star_base_SOURCES} ${star_base_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp) ADD_LIBRARY (star_base OBJECT ${star_base_SOURCES} ${star_base_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/StarVersion.cpp)
TARGET_PRECOMPILE_HEADERS (star_base REUSE_FROM star_core)

View File

@ -28,4 +28,5 @@ ADD_EXECUTABLE (starbound WIN32
$<TARGET_OBJECTS:star_extern> $<TARGET_OBJECTS:star_core> $<TARGET_OBJECTS:star_base> $<TARGET_OBJECTS:star_game> $<TARGET_OBJECTS:star_extern> $<TARGET_OBJECTS:star_core> $<TARGET_OBJECTS:star_base> $<TARGET_OBJECTS:star_game>
$<TARGET_OBJECTS:star_application> $<TARGET_OBJECTS:star_rendering> $<TARGET_OBJECTS:star_windowing> $<TARGET_OBJECTS:star_frontend> $<TARGET_OBJECTS:star_application> $<TARGET_OBJECTS:star_rendering> $<TARGET_OBJECTS:star_windowing> $<TARGET_OBJECTS:star_frontend>
${star_client_HEADERS} ${star_client_SOURCES} ${star_client_RESOURCES}) ${star_client_HEADERS} ${star_client_SOURCES} ${star_client_RESOURCES})
TARGET_LINK_LIBRARIES (starbound ${STAR_EXT_LIBS} ${STAR_EXT_GUI_LIBS}) TARGET_PRECOMPILE_HEADERS (starbound REUSE_FROM star_core)
TARGET_LINK_LIBRARIES (starbound ${STAR_EXT_LIBS} ${STAR_EXT_GUI_LIBS})

View File

@ -84,6 +84,7 @@ SET (star_core_HEADERS
StarOrderedMap.hpp StarOrderedMap.hpp
StarOrderedSet.hpp StarOrderedSet.hpp
StarParametricFunction.hpp StarParametricFunction.hpp
StarPch.hpp
StarPeriodic.hpp StarPeriodic.hpp
StarPeriodicFunction.hpp StarPeriodicFunction.hpp
StarPerlin.hpp StarPerlin.hpp
@ -203,3 +204,4 @@ ELSEIF (STAR_SYSTEM_FAMILY_WINDOWS)
ENDIF () ENDIF ()
ADD_LIBRARY (star_core OBJECT ${star_core_SOURCES} ${star_core_HEADERS}) ADD_LIBRARY (star_core OBJECT ${star_core_SOURCES} ${star_core_HEADERS})
TARGET_PRECOMPILE_HEADERS (star_core PUBLIC StarPch.hpp)

View File

@ -1,10 +1,6 @@
#ifndef STAR_ALGORITHM_HPP #ifndef STAR_ALGORITHM_HPP
#define STAR_ALGORITHM_HPP #define STAR_ALGORITHM_HPP
#include <type_traits>
#include <vector>
#include <iterator>
#include "StarException.hpp" #include "StarException.hpp"
namespace Star { namespace Star {
@ -267,11 +263,6 @@ void stableSortByComputedValue(Container& container, Getter&& valueGetter) {
return sortByComputedValue(container, forward<Getter>(valueGetter), true); return sortByComputedValue(container, forward<Getter>(valueGetter), true);
} }
template <typename Container>
void shuffle(Container& c) {
std::random_shuffle(c.begin(), c.end());
}
template <typename Container> template <typename Container>
void reverse(Container& c) { void reverse(Container& c) {
std::reverse(c.begin(), c.end()); std::reverse(c.begin(), c.end());

View File

@ -1,23 +1,8 @@
#include "StarPch.hpp"
#ifndef STAR_CONFIG_HPP #ifndef STAR_CONFIG_HPP
#define STAR_CONFIG_HPP #define STAR_CONFIG_HPP
#include <cstdint>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <cmath>
#include <tuple>
#include <memory>
#include <functional>
#include <algorithm>
#include <iostream>
#include <initializer_list>
#include <exception>
#include <stdexcept>
#include <atomic>
#include <string>
#include <iterator>
namespace Star { namespace Star {
// Some really common std namespace includes // Some really common std namespace includes

25
source/core/StarPch.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef STAR_PCH_HPP
#define STAR_PCH_HPP
#include <cstdint>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <cmath>
#include <tuple>
#include <memory>
#include <functional>
#include <algorithm>
#include <iostream>
#include <initializer_list>
#include <exception>
#include <stdexcept>
#include <atomic>
#include <string>
#include <string_view>
#include <iterator>
#include <type_traits>
#include <vector>
#endif

View File

@ -187,7 +187,8 @@ typename Container::value_type RandomSource::randValueFrom(
template <typename Container> template <typename Container>
void RandomSource::shuffle(Container& container) { void RandomSource::shuffle(Container& container) {
std::random_shuffle(container.begin(), container.end(), [this](size_t max) { return randUInt(max - 1); }); size_t max = container.size();
std::shuffle(container.begin(), container.end(), URBG<size_t>([this, max]() { return randUInt(max - 1); }));
} }
template <typename Container> template <typename Container>
@ -208,7 +209,8 @@ typename Container::value_type Random::randValueFrom(
template <typename Container> template <typename Container>
void Random::shuffle(Container& container) { void Random::shuffle(Container& container) {
std::random_shuffle(container.begin(), container.end(), [](size_t max) { return Random::randUInt(max - 1); }); size_t max = container.size();
std::shuffle(container.begin(), container.end(), URBG<size_t>([max]() { return Random::randUInt(max - 1); }));
} }
} }

View File

@ -118,12 +118,28 @@ typename Container::value_type staticRandomValueFrom(Container const& container,
} }
} }
template <typename T>
class URBG {
public:
typedef function <T()> Function;
URBG(Function func) : m_func(func) {};
typedef T result_type;
static constexpr T min() { return std::numeric_limits<T>::min(); };
static constexpr T max() { return std::numeric_limits<T>::max(); };
T operator()() { return m_func(); };
private:
Function m_func;
};
template <typename Container, typename T, typename... TL> template <typename Container, typename T, typename... TL>
void staticRandomShuffle(Container& container, T const& d, TL const&... rest) { void staticRandomShuffle(Container& container, T const& d, TL const&... rest) {
int mix = 0; int mix = 0;
std::random_shuffle(container.begin(), size_t max = container.size();
container.end(), std::shuffle(container.begin(), container.end(), URBG<size_t>([&]() {
[&](size_t max) { return staticRandomU32Range(0, max - 1, ++mix, d, rest...); }); return staticRandomU32Range(0, max - 1, ++mix, d, rest...);
}));
} }
} }

View File

@ -103,3 +103,4 @@ SET (star_frontend_SOURCES
) )
ADD_LIBRARY (star_frontend OBJECT ${star_frontend_SOURCES} ${star_frontend_HEADERS}) ADD_LIBRARY (star_frontend OBJECT ${star_frontend_SOURCES} ${star_frontend_HEADERS})
TARGET_PRECOMPILE_HEADERS (star_frontend REUSE_FROM star_core)

View File

@ -584,8 +584,8 @@ void Projectile::processAction(Json const& action) {
bool allowEntityOverlap = parameters.getBool("allowEntityOverlap", true); bool allowEntityOverlap = parameters.getBool("allowEntityOverlap", true);
random_shuffle(tileDrops.begin(), tileDrops.end(), [](int i) { return Random::randu64() % i; }); Random::shuffle(tileDrops);
for (auto tile : zip(openSpaces, tileDrops)) { for (auto& tile : zip(openSpaces, tileDrops)) {
if (!world()->modifyTile(std::get<0>(tile), PlaceMaterial{TileLayer::Foreground, std::get<1>(tile), MaterialHue()}, allowEntityOverlap)) { if (!world()->modifyTile(std::get<0>(tile), PlaceMaterial{TileLayer::Foreground, std::get<1>(tile), MaterialHue()}, allowEntityOverlap)) {
auto itemDrop = ItemDrop::createRandomizedDrop(materialDatabase->materialItemDrop(std::get<1>(tile)), (Vec2F)std::get<0>(tile)); auto itemDrop = ItemDrop::createRandomizedDrop(materialDatabase->materialItemDrop(std::get<1>(tile)), (Vec2F)std::get<0>(tile));
world()->addEntity(itemDrop); world()->addEntity(itemDrop);

View File

@ -33,3 +33,4 @@ SET (star_rendering_SOURCES
) )
ADD_LIBRARY (star_rendering OBJECT ${star_rendering_SOURCES} ${star_rendering_HEADERS}) ADD_LIBRARY (star_rendering OBJECT ${star_rendering_SOURCES} ${star_rendering_HEADERS})
TARGET_PRECOMPILE_HEADERS (star_rendering REUSE_FROM star_core)

View File

@ -22,4 +22,5 @@ SET (star_server_SOURCES
ADD_EXECUTABLE (starbound_server ADD_EXECUTABLE (starbound_server
$<TARGET_OBJECTS:star_extern> $<TARGET_OBJECTS:star_core> $<TARGET_OBJECTS:star_base> $<TARGET_OBJECTS:star_game> $<TARGET_OBJECTS:star_extern> $<TARGET_OBJECTS:star_core> $<TARGET_OBJECTS:star_base> $<TARGET_OBJECTS:star_game>
${star_server_HEADERS} ${star_server_SOURCES}) ${star_server_HEADERS} ${star_server_SOURCES})
TARGET_LINK_LIBRARIES (starbound_server ${STAR_EXT_LIBS}) TARGET_PRECOMPILE_HEADERS (starbound_server REUSE_FROM star_core)
TARGET_LINK_LIBRARIES (starbound_server ${STAR_EXT_LIBS})

View File

@ -101,25 +101,25 @@ namespace {
db.open(); db.open();
// record writes/reads repeated writeRepeat times randomly each cycle // record writes/reads repeated writeRepeat times randomly each cycle
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
putAll(db, keys); putAll(db, keys);
EXPECT_EQ(db.recordCount(), testCount); EXPECT_EQ(db.recordCount(), testCount);
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
checkAll(db, keys); checkAll(db, keys);
// Random reads/writes with randCount cycles... // Random reads/writes with randCount cycles...
for (uint32_t i = 0; i < randCount; ++i) { for (uint32_t i = 0; i < randCount; ++i) {
List<uint32_t> keysTemp(keys.begin(), keys.begin() + keys.size() / 2); List<uint32_t> keysTemp(keys.begin(), keys.begin() + keys.size() / 2);
std::random_shuffle(keysTemp.begin(), keysTemp.end()); Random::shuffle(keysTemp);
removeAll(db, keysTemp); removeAll(db, keysTemp);
std::random_shuffle(keysTemp.begin(), keysTemp.end()); Random::shuffle(keysTemp);
putAll(db, keysTemp); putAll(db, keysTemp);
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
checkAll(db, keys); checkAll(db, keys);
} }
@ -128,7 +128,7 @@ namespace {
// Random reads/writes/rollbacks with rollbackCount cycles... // Random reads/writes/rollbacks with rollbackCount cycles...
for (uint32_t i = 0; i < rollbackCount ; ++i) { for (uint32_t i = 0; i < rollbackCount ; ++i) {
List<uint32_t> keysTemp(keys.begin(), keys.begin() + keys.size() / 2); List<uint32_t> keysTemp(keys.begin(), keys.begin() + keys.size() / 2);
std::random_shuffle(keysTemp.begin(), keysTemp.end()); Random::shuffle(keysTemp);
removeAll(db, keysTemp); removeAll(db, keysTemp);
db.rollback(); db.rollback();
@ -193,7 +193,7 @@ TEST(BTreeDatabaseTest, Threading) {
if (Random::randf() > 0.3) if (Random::randf() > 0.3)
deleteKeySet.append(key); deleteKeySet.append(key);
} }
std::random_shuffle(writeKeySet.begin(), writeKeySet.end()); Random::shuffle(writeKeySet);
{ {
auto writer = Thread::invoke("databaseTestWriter", auto writer = Thread::invoke("databaseTestWriter",

View File

@ -3,6 +3,7 @@
#include "StarMap.hpp" #include "StarMap.hpp"
#include "StarSet.hpp" #include "StarSet.hpp"
#include "StarLexicalCast.hpp" #include "StarLexicalCast.hpp"
#include "StarRandom.hpp"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -614,25 +615,25 @@ void testBTree(size_t maxIndexSize, size_t maxLeafSize) {
} }
// record writes/reads repeated WriteRepeat times randomly each cycle // record writes/reads repeated WriteRepeat times randomly each cycle
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
putAll(db, keys); putAll(db, keys);
EXPECT_EQ(db.recordCount(), TestCount); EXPECT_EQ(db.recordCount(), TestCount);
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
checkAll(db, keys); checkAll(db, keys);
// Random reads/writes with ShrinkCount cycles... // Random reads/writes with ShrinkCount cycles...
for (size_t i = 0; i < ShrinkCount; ++i) { for (size_t i = 0; i < ShrinkCount; ++i) {
std::random_shuffle(keys.begin(), keys.end()); Random::shuffle(keys);
List<int> keysTemp = keys.slice(0, keys.size() / 2); List<int> keysTemp = keys.slice(0, keys.size() / 2);
removeAll(db, keysTemp); removeAll(db, keysTemp);
std::random_shuffle(keysTemp.begin(), keysTemp.end()); Random::shuffle(keysTemp);
putAll(db, keysTemp); putAll(db, keysTemp);
std::random_shuffle(keysTemp.begin(), keysTemp.end()); Random::shuffle(keysTemp);
checkAll(db, keys); checkAll(db, keys);
} }