feat!: Migrate to std::filesystem consistently

This commit is contained in:
Dennis Klein 2023-02-13 14:12:38 +01:00 committed by Dennis Klein
parent f2dce91098
commit 05b734ee0d
10 changed files with 123 additions and 40 deletions

View File

@ -1,5 +1,5 @@
################################################################################
# Copyright (C) 2018-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
# Copyright (C) 2018-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
# #
# This software is distributed under the terms of the #
# GNU Lesser General Public Licence (LGPL) version 3, #
@ -168,11 +168,15 @@ if(CMAKE_GENERATOR STREQUAL Ninja AND ENABLE_CCACHE)
endif()
endif()
if( CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
set(FAIRMQ_HAS_STD_FILESYSTEM 0)
else()
set(FAIRMQ_HAS_STD_FILESYSTEM 1)
if(NOT DEFINED FAIRMQ_HAS_STD_FILESYSTEM)
if( ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
OR ( CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9))
set(FAIRMQ_HAS_STD_FILESYSTEM 0)
else()
set(FAIRMQ_HAS_STD_FILESYSTEM 1)
endif()
endif()
if(NOT DEFINED FAIRMQ_HAS_STD_PMR)

View File

@ -97,6 +97,6 @@ endmacro()
macro(fairmq_summary_compile_definitions)
message(STATUS " ")
message(STATUS " ${Cyan}COMPILE DEFINITION VALUE${CR}")
message(STATUS " ${BWhite}FAIRMQ_HAS_STD_FILESYSTEM${CR} ${FAIRMQ_HAS_STD_FILESYSTEM}")
message(STATUS " ${BWhite}FAIRMQ_HAS_STD_FILESYSTEM${CR} ${FAIRMQ_HAS_STD_FILESYSTEM} (overridable with ${BMagenta}-DFAIRMQ_HAS_STD_FILESYSTEM=0|1${CR})")
message(STATUS " ${BWhite}FAIRMQ_HAS_STD_PMR${CR} ${FAIRMQ_HAS_STD_PMR} (overridable with ${BMagenta}-DFAIRMQ_HAS_STD_PMR=0|1${CR})")
endmacro()

View File

@ -197,7 +197,7 @@ if(BUILD_FAIRMQ)
$<$<PLATFORM_ID:Linux>:rt>
Boost::boost
Boost::program_options
Boost::filesystem
Boost::filesystem # still needed for Boost.DLL
Boost::regex
FairLogger::FairLogger

View File

@ -22,7 +22,6 @@ using namespace std;
using fair::mq::Plugin;
using fair::mq::tools::ToString;
using fair::mq::tools::ToStrVector;
namespace fs = boost::filesystem;
namespace po = boost::program_options;
namespace dll = boost::dll;
using boost::optional;
@ -171,7 +170,7 @@ auto fair::mq::PluginManager::LoadPluginPrelinkedDynamic(const string& pluginNam
}
auto fair::mq::PluginManager::SearchPluginFile(const string& pluginName) const
-> boost::filesystem::path
-> fs::path
{
for (const auto& searchPath : SearchPaths()) {
for (const auto& libPrefix : {fgkLibPrefix, fgkLibPrefixAlt}) {
@ -181,7 +180,7 @@ auto fair::mq::PluginManager::SearchPluginFile(const string& pluginName) const
libPrefix,
pluginName,
boost::dll::detail::shared_library_impl::suffix().native());
auto const found = boost::filesystem::exists(file);
auto const found = fs::exists(file);
if (found) {
return file;
}

View File

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2017-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
@ -13,9 +13,16 @@
#include <fairmq/PluginServices.h>
#include <fairmq/tools/Strings.h>
#if FAIRMQ_HAS_STD_FILESYSTEM
#include <filesystem>
namespace fs = std::filesystem;
#else
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
#endif
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <boost/dll/import.hpp>
@ -60,13 +67,13 @@ class PluginManager
LOG(debug) << "Shutting down Plugin Manager";
}
auto SetSearchPaths(const std::vector<boost::filesystem::path>&) -> void;
auto AppendSearchPath(const boost::filesystem::path&) -> void;
auto PrependSearchPath(const boost::filesystem::path&) -> void;
auto SearchPaths() const -> const std::vector<boost::filesystem::path>& { return fSearchPaths; }
auto SetSearchPaths(const std::vector<fs::path>&) -> void;
auto AppendSearchPath(const fs::path&) -> void;
auto PrependSearchPath(const fs::path&) -> void;
auto SearchPaths() const -> const std::vector<fs::path>& { return fSearchPaths; }
struct BadSearchPath : std::invalid_argument { using std::invalid_argument::invalid_argument; };
auto SearchPluginFile(const std::string&) const -> boost::filesystem::path;
auto SearchPluginFile(const std::string&) const -> fs::path;
struct PluginNotFound : std::runtime_error { using std::runtime_error::runtime_error; };
auto LoadPlugin(const std::string& pluginName) -> void;
auto LoadPlugins(const std::vector<std::string>& pluginNames) -> void { for(const auto& pluginName : pluginNames) { LoadPlugin(pluginName); } }
@ -88,18 +95,33 @@ class PluginManager
auto WaitForPluginsToReleaseDeviceControl() -> void { fPluginServices->WaitForReleaseDeviceControl(); }
private:
static auto ValidateSearchPath(const boost::filesystem::path&) -> void;
static auto ValidateSearchPath(const fs::path&) -> void;
auto LoadPluginPrelinkedDynamic(const std::string& pluginName) -> void;
auto LoadPluginDynamic(const std::string& pluginName) -> void;
auto LoadPluginStatic(const std::string& pluginName) -> void;
template<typename... Args>
auto LoadSymbols(const std::string& pluginName, Args&&... args) -> void
#if FAIRMQ_HAS_STD_FILESYSTEM
template<typename T>
static auto AdaptPathType(T&& path)
{
if constexpr(std::is_same_v<T, std::filesystem::path>) {
return boost::filesystem::path(std::forward<T>(path));
} else {
return std::forward<T>(path);
}
}
#endif
template<typename FirstArg, typename... Args>
auto LoadSymbols(const std::string& pluginName, FirstArg&& farg, Args&&... args) -> void
{
using namespace boost::dll;
using fair::mq::tools::ToString;
auto lib = shared_library{std::forward<Args>(args)...};
#if FAIRMQ_HAS_STD_FILESYSTEM
auto lib = shared_library{AdaptPathType(std::forward<FirstArg>(farg)), std::forward<Args>(args)...};
#else
auto lib = shared_library{std::forward<FirstArg>(farg), std::forward<Args>(args)...};
#endif
fgDLLKeepAlive.push_back(lib);
fPluginFactories[pluginName] = import_alias<PluginFactory>(
@ -121,7 +143,7 @@ class PluginManager
static const std::string fgkLibPrefix;
static const std::string fgkLibPrefixAlt;
std::vector<boost::filesystem::path> fSearchPaths;
std::vector<fs::path> fSearchPaths;
static std::vector<boost::dll::shared_library> fgDLLKeepAlive; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
std::map<std::string, std::function<PluginFactory>> fPluginFactories;
std::unique_ptr<PluginServices> fPluginServices;

View File

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
@ -8,7 +8,15 @@
#include <fairmq/Properties.h>
#if FAIRMQ_HAS_STD_FILESYSTEM
#include <filesystem>
namespace fs = std::filesystem;
#else
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
#endif
using namespace std;
using boost::any_cast;
@ -84,7 +92,12 @@ unordered_map<type_index, function<pair<string, string>(const Property&)>> Prope
{ type_index(typeid(long double)), [](const Property& p) { return getStringPair<long double>(p, "long double"); } },
{ type_index(typeid(bool)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast<bool>(p); return pair<string, string>{ ss.str(), "bool" }; } },
{ type_index(typeid(vector<bool>)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast<vector<bool>>(p); return pair<string, string>{ ss.str(), "vector<bool>>" }; } },
{ type_index(typeid(boost::filesystem::path)), [](const Property& p) { return getStringPair<boost::filesystem::path>(p, "boost::filesystem::path"); } },
{ type_index(typeid(fs::path)), [](const Property& p) { return getStringPair<fs::path>(p,
#if FAIRMQ_HAS_STD_FILESYSTEM
"std::filesystem::path"); } },
#else
"boost::filesystem::path"); } },
#endif
{ type_index(typeid(vector<char>)), [](const Property& p) { return getStringPair<vector<char>>(p, "vector<char>"); } },
{ type_index(typeid(vector<signed char>)), [](const Property& p) { return getStringPair<vector<signed char>>(p, "vector<signed char>"); } },
{ type_index(typeid(vector<unsigned char>)), [](const Property& p) { return getStringPair<vector<unsigned char>>(p, "vector<unsigned char>"); } },
@ -100,7 +113,12 @@ unordered_map<type_index, function<pair<string, string>(const Property&)>> Prope
{ type_index(typeid(vector<float>)), [](const Property& p) { return getStringPair<vector<float>>(p, "vector<float>"); } },
{ type_index(typeid(vector<double>)), [](const Property& p) { return getStringPair<vector<double>>(p, "vector<double>"); } },
{ type_index(typeid(vector<long double>)), [](const Property& p) { return getStringPair<vector<long double>>(p, "vector<long double>"); } },
{ type_index(typeid(vector<boost::filesystem::path>)), [](const Property& p) { return getStringPair<vector<boost::filesystem::path>>(p, "vector<boost::filesystem::path>"); } },
{ type_index(typeid(vector<fs::path>)), [](const Property& p) { return getStringPair<vector<fs::path>>(p,
#if FAIRMQ_HAS_STD_FILESYSTEM
"vector<std::filesystem::path>"); } },
#else
"vector<boost::filesystem::path>"); } },
#endif
};
unordered_map<type_index, void(*)(const EventManager&, const string&, const Property&)> PropertyHelper::fEventEmitters = {
@ -122,7 +140,7 @@ unordered_map<type_index, void(*)(const EventManager&, const string&, const Prop
{ type_index(typeid(long double)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, long double>(k, any_cast<long double>(p)); } },
{ type_index(typeid(bool)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, bool>(k, any_cast<bool>(p)); } },
{ type_index(typeid(vector<bool>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<bool>>(k, any_cast<vector<bool>>(p)); } },
{ type_index(typeid(boost::filesystem::path)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, boost::filesystem::path>(k, any_cast<boost::filesystem::path>(p)); } },
{ type_index(typeid(fs::path)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, fs::path>(k, any_cast<fs::path>(p)); } },
{ type_index(typeid(vector<char>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<char>>(k, any_cast<vector<char>>(p)); } },
{ type_index(typeid(vector<signed char>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<signed char>>(k, any_cast<vector<signed char>>(p)); } },
{ type_index(typeid(vector<unsigned char>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<unsigned char>>(k, any_cast<vector<unsigned char>>(p)); } },
@ -138,7 +156,7 @@ unordered_map<type_index, void(*)(const EventManager&, const string&, const Prop
{ type_index(typeid(vector<float>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<float>>(k, any_cast<vector<float>>(p)); } },
{ type_index(typeid(vector<double>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<double>>(k, any_cast<vector<double>>(p)); } },
{ type_index(typeid(vector<long double>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<long double>>(k, any_cast<vector<long double>>(p)); } },
{ type_index(typeid(vector<boost::filesystem::path>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<boost::filesystem::path>>(k, any_cast<vector<boost::filesystem::path>>(p)); } },
{ type_index(typeid(vector<fs::path>)), [](const EventManager& em, const string& k, const Property& p) { em.Emit<PropertyChange, vector<fs::path>>(k, any_cast<vector<fs::path>>(p)); } },
};
} // namespace fair::mq

View File

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
@ -20,7 +20,6 @@
#include <fairlogger/Logger.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>

View File

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
@ -16,7 +16,6 @@
#include <fairlogger/Logger.h>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/file_mapping.hpp>

View File

@ -12,6 +12,7 @@
#include <fairmq/PluginServices.h>
#include <fairmq/Device.h>
#include <fairmq/ProgOptions.h>
#include <fairmq/Tools.h>
#include <gtest/gtest.h>
@ -26,7 +27,7 @@ namespace _plugin_manager
{
using namespace fair::mq;
using namespace boost::filesystem;
using namespace fs;
using namespace boost::program_options;
using namespace std;
@ -134,7 +135,7 @@ TEST(PluginManager, SearchPathValidation)
TEST(PluginManager, SearchPaths)
{
const auto temp = temp_directory_path() / unique_path();
const auto temp = temp_directory_path() / fair::mq::tools::Uuid();
create_directories(temp);
const auto non_existing_dir = temp / "non-existing-dir";
const auto existing_dir = temp / "existing-dir";

View File

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2019-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
@ -8,7 +8,16 @@
#include <fairmq/ProgOptions.h>
#if FAIRMQ_HAS_STD_FILESYSTEM
#include <filesystem>
namespace fs = std::filesystem;
#else
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
#endif
#include <gtest/gtest.h>
#include <iostream>
#include <string>
@ -52,7 +61,13 @@ TEST(ProgOptions, SetAndGet)
set_and_get<double>(o, "_double", 33.22);
set_and_get<long double>(o, "_long double", 333.222);
set_and_get<bool>(o, "_bool", true);
set_and_get<boost::filesystem::path>(o, "_boost::filesystem::path", boost::filesystem::path("C:\\Windows"));
set_and_get<fs::path>(o,
#if FAIRMQ_HAS_STD_FILESYSTEM
"_std::filesystem::path",
#else
"_boost::filesystem::path",
#endif
fs::path("C:\\Windows"));
set_and_get<vector<bool>>(o, "_vector<bool>", { true, true });
set_and_get<vector<char>>(o, "_vector<char>", { 'a', 'b', 'c' });
set_and_get<vector<signed char>>(o, "_vector<signed char>", { -1, -2, -3 });
@ -71,7 +86,13 @@ TEST(ProgOptions, SetAndGet)
set_and_get<vector<float>>(o, "_vector<float>", { 3.2, 3.3, 3.4 });
set_and_get<vector<double>>(o, "_vector<double>", { 33.22, 33.23, 33.24 });
set_and_get<vector<long double>>(o, "_vector<long double>", { 333.222, 333.223, 333.224 });
set_and_get<vector<boost::filesystem::path>>(o, "_vector<boost::filesystem::path>", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") });
set_and_get<vector<fs::path>>(o,
#if FAIRMQ_HAS_STD_FILESYSTEM
"_vector<std::filesystem::path>",
#else
"_vector<boost::filesystem::path>",
#endif
{ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") });
ASSERT_THROW(o.ParseAll({"cmd", "--unregistered", "option"}, false), boost::program_options::unknown_option);
ASSERT_NO_THROW(o.ParseAll({"cmd", "--unregistered", "option"}, true));
@ -136,7 +157,13 @@ TEST(ProgOptions, SubscribeAndSet)
subscribe_and_set<double>(o, "_double", 33.22);
subscribe_and_set<long double>(o, "_long double", 333.222);
subscribe_and_set<bool>(o, "_bool", true);
subscribe_and_set<boost::filesystem::path>(o, "_boost::filesystem::path", boost::filesystem::path("C:\\Windows"));
subscribe_and_set<fs::path>(o,
#if FAIRMQ_HAS_STD_FILESYSTEM
"_std::filesystem::path",
#else
"_boost::filesystem::path",
#endif
fs::path("C:\\Windows"));
subscribe_and_set<vector<bool>>(o, "_vector<bool>", { true, true });
subscribe_and_set<vector<char>>(o, "_vector<char>", { 'a', 'b', 'c' });
subscribe_and_set<vector<signed char>>(o, "_vector<signed char>", { -1, -2, -3 });
@ -155,7 +182,13 @@ TEST(ProgOptions, SubscribeAndSet)
subscribe_and_set<vector<float>>(o, "_vector<float>", { 3.2, 3.3, 3.4 });
subscribe_and_set<vector<double>>(o, "_vector<double>", { 33.22, 33.23, 33.24 });
subscribe_and_set<vector<long double>>(o, "_vector<long double>", { 333.222, 333.223, 333.224 });
subscribe_and_set<vector<boost::filesystem::path>>(o, "_vector<boost::filesystem::path>", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") });
subscribe_and_set<vector<fs::path>>(o,
#if FAIRMQ_HAS_STD_FILESYSTEM
"_vector<std::filesystem::path>",
#else
"_vector<boost::filesystem::path>",
#endif
{ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") });
}
TEST(PropertyHelper, ConvertPropertyToString)
@ -179,7 +212,11 @@ TEST(PropertyHelper, ConvertPropertyToString)
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast<double>(33.22))), "33.22");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast<long double>(333.222))), "333.222");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(static_cast<bool>(true))), "true");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(boost::filesystem::path("C:\\Windows"))), "\"C:\\Windows\"");
#if FAIRMQ_HAS_STD_FILESYSTEM
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(fs::path("C:\\Windows"))), "\"C:\\\\Windows\"");
#else
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(fs::path("C:\\Windows"))), "\"C:\\Windows\"");
#endif
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<char>({ 'a', 'b', 'c' }))), "a, b, c");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<signed char>({ -1, -2, -3 }))), "-1, -2, -3");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<unsigned char>({ 1, 2, 3 }))), "1, 2, 3");
@ -198,7 +235,11 @@ TEST(PropertyHelper, ConvertPropertyToString)
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<double>({ 33.22, 33.23, 33.24 }))), "33.22, 33.23, 33.24");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<long double>({ 333.222, 333.223, 333.224 }))), "333.222, 333.223, 333.224");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<bool>({ true, true }))), "true, true");
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<boost::filesystem::path>({ boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }))), "\"C:\\Windows\", \"C:\\Windows\\System32\"");
#if FAIRMQ_HAS_STD_FILESYSTEM
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<fs::path>({ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }))), "\"C:\\\\Windows\", \"C:\\\\Windows\\\\System32\"");
#else
EXPECT_EQ(PropertyHelper::ConvertPropertyToString(Property(vector<fs::path>({ fs::path("C:\\Windows"), fs::path("C:\\Windows\\System32") }))), "\"C:\\Windows\", \"C:\\Windows\\System32\"");
#endif
}
} // namespace