From 13a74602bd4c46149da9949d448387a40b8ebd1c Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sun, 25 Jun 2023 20:01:32 +1000 Subject: [PATCH] Upgrade to C++17 --- scripts/osx/ycm_extra_conf.py | 2 +- source/CMakeLists.txt | 12 ++++++------ source/application/CMakeLists.txt | 1 + source/base/CMakeLists.txt | 1 + source/client/CMakeLists.txt | 3 ++- source/core/CMakeLists.txt | 2 ++ source/core/StarAlgorithm.hpp | 9 --------- source/core/StarConfig.hpp | 19 ++----------------- source/core/StarPch.hpp | 25 +++++++++++++++++++++++++ source/core/StarRandom.hpp | 6 ++++-- source/core/StarStaticRandom.hpp | 22 +++++++++++++++++++--- source/frontend/CMakeLists.txt | 1 + source/game/StarProjectile.cpp | 4 ++-- source/rendering/CMakeLists.txt | 1 + source/server/CMakeLists.txt | 3 ++- source/test/btree_database_test.cpp | 14 +++++++------- source/test/btree_test.cpp | 11 ++++++----- 17 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 source/core/StarPch.hpp diff --git a/scripts/osx/ycm_extra_conf.py b/scripts/osx/ycm_extra_conf.py index b9b969d..5098960 100644 --- a/scripts/osx/ycm_extra_conf.py +++ b/scripts/osx/ycm_extra_conf.py @@ -46,7 +46,7 @@ flags = [ # a "-std=". # For a C project, you would set this to something like 'c99' instead of # 'c++11'. -'-std=c++14', +'-std=c++17', '-stdlib=libc++', # ...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 diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index d8f01a4..1b65707 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,4 +1,4 @@ -CMAKE_MINIMUM_REQUIRED (VERSION 3.4) +CMAKE_MINIMUM_REQUIRED (VERSION 3.16) # Find CCache FIND_PROGRAM(CCACHE_PATH ccache) @@ -227,7 +227,7 @@ ENDIF () IF (STAR_COMPILER_GNU) 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) SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mthreads") @@ -261,11 +261,11 @@ IF (STAR_COMPILER_GNU) ELSEIF (STAR_COMPILER_CLANG) 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) 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++") ELSEIF () SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -D_REENTRANT") @@ -313,7 +313,7 @@ ELSEIF (STAR_COMPILER_MSVC) # 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_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) SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT") @@ -351,7 +351,7 @@ ELSEIF (STAR_COMPILER_MSVC) ELSE () 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_CXX_FLAGS_DEBUG "-g") diff --git a/source/application/CMakeLists.txt b/source/application/CMakeLists.txt index db557aa..0394655 100644 --- a/source/application/CMakeLists.txt +++ b/source/application/CMakeLists.txt @@ -63,3 +63,4 @@ IF (STAR_ENABLE_DISCORD_INTEGRATION) ENDIF () ADD_LIBRARY (star_application OBJECT ${star_application_SOURCES} ${star_application_HEADERS}) +TARGET_PRECOMPILE_HEADERS (star_application REUSE_FROM star_core) \ No newline at end of file diff --git a/source/base/CMakeLists.txt b/source/base/CMakeLists.txt index 03f767f..d5e0750 100644 --- a/source/base/CMakeLists.txt +++ b/source/base/CMakeLists.txt @@ -35,3 +35,4 @@ SET (star_base_SOURCES 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) +TARGET_PRECOMPILE_HEADERS (star_base REUSE_FROM star_core) diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index 0e6335a..b9a3e1c 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -28,4 +28,5 @@ ADD_EXECUTABLE (starbound WIN32 $ $ $ $ $ $ $ $ ${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}) \ No newline at end of file diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index 6668109..34d890f 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -84,6 +84,7 @@ SET (star_core_HEADERS StarOrderedMap.hpp StarOrderedSet.hpp StarParametricFunction.hpp + StarPch.hpp StarPeriodic.hpp StarPeriodicFunction.hpp StarPerlin.hpp @@ -203,3 +204,4 @@ ELSEIF (STAR_SYSTEM_FAMILY_WINDOWS) ENDIF () ADD_LIBRARY (star_core OBJECT ${star_core_SOURCES} ${star_core_HEADERS}) +TARGET_PRECOMPILE_HEADERS (star_core PUBLIC StarPch.hpp) \ No newline at end of file diff --git a/source/core/StarAlgorithm.hpp b/source/core/StarAlgorithm.hpp index 331f653..e26917a 100644 --- a/source/core/StarAlgorithm.hpp +++ b/source/core/StarAlgorithm.hpp @@ -1,10 +1,6 @@ #ifndef STAR_ALGORITHM_HPP #define STAR_ALGORITHM_HPP -#include -#include -#include - #include "StarException.hpp" namespace Star { @@ -267,11 +263,6 @@ void stableSortByComputedValue(Container& container, Getter&& valueGetter) { return sortByComputedValue(container, forward(valueGetter), true); } -template -void shuffle(Container& c) { - std::random_shuffle(c.begin(), c.end()); -} - template void reverse(Container& c) { std::reverse(c.begin(), c.end()); diff --git a/source/core/StarConfig.hpp b/source/core/StarConfig.hpp index 22c2607..e7a5ff0 100644 --- a/source/core/StarConfig.hpp +++ b/source/core/StarConfig.hpp @@ -1,23 +1,8 @@ +#include "StarPch.hpp" + #ifndef STAR_CONFIG_HPP #define STAR_CONFIG_HPP -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - namespace Star { // Some really common std namespace includes diff --git a/source/core/StarPch.hpp b/source/core/StarPch.hpp new file mode 100644 index 0000000..809156c --- /dev/null +++ b/source/core/StarPch.hpp @@ -0,0 +1,25 @@ +#ifndef STAR_PCH_HPP +#define STAR_PCH_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#endif \ No newline at end of file diff --git a/source/core/StarRandom.hpp b/source/core/StarRandom.hpp index b3c8ba2..c39f6ef 100644 --- a/source/core/StarRandom.hpp +++ b/source/core/StarRandom.hpp @@ -187,7 +187,8 @@ typename Container::value_type RandomSource::randValueFrom( template 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([this, max]() { return randUInt(max - 1); })); } template @@ -208,7 +209,8 @@ typename Container::value_type Random::randValueFrom( template 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([max]() { return Random::randUInt(max - 1); })); } } diff --git a/source/core/StarStaticRandom.hpp b/source/core/StarStaticRandom.hpp index 97cfdef..b55b411 100644 --- a/source/core/StarStaticRandom.hpp +++ b/source/core/StarStaticRandom.hpp @@ -118,12 +118,28 @@ typename Container::value_type staticRandomValueFrom(Container const& container, } } +template +class URBG { +public: + typedef function Function; + + URBG(Function func) : m_func(func) {}; + + typedef T result_type; + static constexpr T min() { return std::numeric_limits::min(); }; + static constexpr T max() { return std::numeric_limits::max(); }; + T operator()() { return m_func(); }; +private: + Function m_func; +}; + template void staticRandomShuffle(Container& container, T const& d, TL const&... rest) { int mix = 0; - std::random_shuffle(container.begin(), - container.end(), - [&](size_t max) { return staticRandomU32Range(0, max - 1, ++mix, d, rest...); }); + size_t max = container.size(); + std::shuffle(container.begin(), container.end(), URBG([&]() { + return staticRandomU32Range(0, max - 1, ++mix, d, rest...); + })); } } diff --git a/source/frontend/CMakeLists.txt b/source/frontend/CMakeLists.txt index 75145bd..cd06ce7 100644 --- a/source/frontend/CMakeLists.txt +++ b/source/frontend/CMakeLists.txt @@ -103,3 +103,4 @@ SET (star_frontend_SOURCES ) ADD_LIBRARY (star_frontend OBJECT ${star_frontend_SOURCES} ${star_frontend_HEADERS}) +TARGET_PRECOMPILE_HEADERS (star_frontend REUSE_FROM star_core) \ No newline at end of file diff --git a/source/game/StarProjectile.cpp b/source/game/StarProjectile.cpp index d094797..698428b 100644 --- a/source/game/StarProjectile.cpp +++ b/source/game/StarProjectile.cpp @@ -584,8 +584,8 @@ void Projectile::processAction(Json const& action) { bool allowEntityOverlap = parameters.getBool("allowEntityOverlap", true); - random_shuffle(tileDrops.begin(), tileDrops.end(), [](int i) { return Random::randu64() % i; }); - for (auto tile : zip(openSpaces, tileDrops)) { + Random::shuffle(tileDrops); + for (auto& tile : zip(openSpaces, tileDrops)) { 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)); world()->addEntity(itemDrop); diff --git a/source/rendering/CMakeLists.txt b/source/rendering/CMakeLists.txt index d45153a..c683daa 100644 --- a/source/rendering/CMakeLists.txt +++ b/source/rendering/CMakeLists.txt @@ -33,3 +33,4 @@ SET (star_rendering_SOURCES ) ADD_LIBRARY (star_rendering OBJECT ${star_rendering_SOURCES} ${star_rendering_HEADERS}) +TARGET_PRECOMPILE_HEADERS (star_rendering REUSE_FROM star_core) \ No newline at end of file diff --git a/source/server/CMakeLists.txt b/source/server/CMakeLists.txt index d6a22e6..98c1179 100644 --- a/source/server/CMakeLists.txt +++ b/source/server/CMakeLists.txt @@ -22,4 +22,5 @@ SET (star_server_SOURCES ADD_EXECUTABLE (starbound_server $ $ $ $ ${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}) \ No newline at end of file diff --git a/source/test/btree_database_test.cpp b/source/test/btree_database_test.cpp index 9b9bee4..09b4b16 100644 --- a/source/test/btree_database_test.cpp +++ b/source/test/btree_database_test.cpp @@ -101,25 +101,25 @@ namespace { db.open(); // record writes/reads repeated writeRepeat times randomly each cycle - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); putAll(db, keys); EXPECT_EQ(db.recordCount(), testCount); - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); checkAll(db, keys); // Random reads/writes with randCount cycles... for (uint32_t i = 0; i < randCount; ++i) { List keysTemp(keys.begin(), keys.begin() + keys.size() / 2); - std::random_shuffle(keysTemp.begin(), keysTemp.end()); + Random::shuffle(keysTemp); removeAll(db, keysTemp); - std::random_shuffle(keysTemp.begin(), keysTemp.end()); + Random::shuffle(keysTemp); putAll(db, keysTemp); - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); checkAll(db, keys); } @@ -128,7 +128,7 @@ namespace { // Random reads/writes/rollbacks with rollbackCount cycles... for (uint32_t i = 0; i < rollbackCount ; ++i) { List keysTemp(keys.begin(), keys.begin() + keys.size() / 2); - std::random_shuffle(keysTemp.begin(), keysTemp.end()); + Random::shuffle(keysTemp); removeAll(db, keysTemp); db.rollback(); @@ -193,7 +193,7 @@ TEST(BTreeDatabaseTest, Threading) { if (Random::randf() > 0.3) deleteKeySet.append(key); } - std::random_shuffle(writeKeySet.begin(), writeKeySet.end()); + Random::shuffle(writeKeySet); { auto writer = Thread::invoke("databaseTestWriter", diff --git a/source/test/btree_test.cpp b/source/test/btree_test.cpp index aa413d6..2d22631 100644 --- a/source/test/btree_test.cpp +++ b/source/test/btree_test.cpp @@ -3,6 +3,7 @@ #include "StarMap.hpp" #include "StarSet.hpp" #include "StarLexicalCast.hpp" +#include "StarRandom.hpp" #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 - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); putAll(db, keys); EXPECT_EQ(db.recordCount(), TestCount); - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); checkAll(db, keys); // Random reads/writes with ShrinkCount cycles... for (size_t i = 0; i < ShrinkCount; ++i) { - std::random_shuffle(keys.begin(), keys.end()); + Random::shuffle(keys); List keysTemp = keys.slice(0, keys.size() / 2); removeAll(db, keysTemp); - std::random_shuffle(keysTemp.begin(), keysTemp.end()); + Random::shuffle(keysTemp); putAll(db, keysTemp); - std::random_shuffle(keysTemp.begin(), keysTemp.end()); + Random::shuffle(keysTemp); checkAll(db, keys); }