From 78ef049093c987fb70dd546b912184ba8ef2bdef Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Mon, 31 Mar 2025 22:45:40 +0100 Subject: [PATCH] Use std::string_view instead of const char* in the public API --- include/loot/api.h | 3 ++- include/loot/metadata/group.h | 2 +- include/loot/metadata/message_content.h | 2 +- src/api/api.cpp | 2 +- src/api/helpers/logging.cpp | 6 +++--- src/api/helpers/logging.h | 4 +++- src/api/sorting/plugin_sorting_data.cpp | 2 +- src/tests/api/interface/main.cpp | 20 ++++++++++---------- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/loot/api.h b/include/loot/api.h index f3baa6ac..b8d551e1 100644 --- a/include/loot/api.h +++ b/include/loot/api.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "loot/api_decorator.h" #include "loot/enum/game_type.h" @@ -55,7 +56,7 @@ namespace loot { * level of the message being logged, and the second is the message. */ LOOT_API void SetLoggingCallback( - std::function callback); + std::function callback); /** * @brief Set the log severity level. diff --git a/include/loot/metadata/group.h b/include/loot/metadata/group.h index e553f12a..ba5a0071 100644 --- a/include/loot/metadata/group.h +++ b/include/loot/metadata/group.h @@ -39,7 +39,7 @@ public: /** * The name of the group to which all plugins belong by default. */ - static constexpr const char* DEFAULT_NAME = "default"; + static constexpr std::string_view DEFAULT_NAME = "default"; /** * Construct a Group with the name "default" and an empty set of groups to diff --git a/include/loot/metadata/message_content.h b/include/loot/metadata/message_content.h index 6fef605c..9f23335f 100644 --- a/include/loot/metadata/message_content.h +++ b/include/loot/metadata/message_content.h @@ -41,7 +41,7 @@ public: * The code for the default language assumed for message content, which is * "en" (English). */ - static constexpr const char* DEFAULT_LANGUAGE = "en"; + static constexpr std::string_view DEFAULT_LANGUAGE = "en"; /** * Construct a MessageContent object with an empty English message string. diff --git a/src/api/api.cpp b/src/api/api.cpp index 2ce689ad..4a99a3f6 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -83,7 +83,7 @@ std::filesystem::path ResolvePath(const std::filesystem::path& path) { } LOOT_API void SetLoggingCallback( - std::function callback) { + std::function callback) { const auto logger = createLogger(callback); spdlog::drop(logger->name()); diff --git a/src/api/helpers/logging.cpp b/src/api/helpers/logging.cpp index 1a604043..9706044d 100644 --- a/src/api/helpers/logging.cpp +++ b/src/api/helpers/logging.cpp @@ -76,7 +76,7 @@ spdlog::level::level_enum mapToSpdlog(LogLevel severity) { class SpdLoggingSink : public spdlog::sinks::base_sink { public: - explicit SpdLoggingSink(std::function callback) { + explicit SpdLoggingSink(std::function callback) { this->callback = callback; } @@ -91,7 +91,7 @@ protected: void flush_() override {} private: - std::function callback; + std::function callback; }; } @@ -99,7 +99,7 @@ namespace loot { std::shared_ptr getLogger() { return spdlog::get(std::string(LOGGER_NAME)); } std::shared_ptr createLogger( - std::function callback) { + std::function callback) { auto sink = std::make_shared(callback); auto logger = std::make_shared(std::string(LOGGER_NAME), sink); logger->set_level(spdlog::level::level_enum::trace); diff --git a/src/api/helpers/logging.h b/src/api/helpers/logging.h index cce669b0..316fc77d 100644 --- a/src/api/helpers/logging.h +++ b/src/api/helpers/logging.h @@ -24,6 +24,8 @@ #ifndef LOOT_API_HELPERS_LOGGING #define LOOT_API_HELPERS_LOGGING +#include + #include #include "loot/enum/log_level.h" @@ -32,7 +34,7 @@ namespace loot { std::shared_ptr getLogger(); std::shared_ptr createLogger( - std::function callback); + std::function callback); void setLoggerLevel(LogLevel level); } diff --git a/src/api/sorting/plugin_sorting_data.cpp b/src/api/sorting/plugin_sorting_data.cpp index fac5ffc3..ff35a966 100644 --- a/src/api/sorting/plugin_sorting_data.cpp +++ b/src/api/sorting/plugin_sorting_data.cpp @@ -39,7 +39,7 @@ PluginSortingData::PluginSortingData(const PluginSortingInterface* plugin, name_(plugin == nullptr ? std::string() : plugin->GetName()), isMaster_(plugin != nullptr && plugin->IsMaster()), group_(userMetadata.GetGroup().value_or( - masterlistMetadata.GetGroup().value_or(Group::DEFAULT_NAME))), + masterlistMetadata.GetGroup().value_or(std::string(Group::DEFAULT_NAME)))), masterlistLoadAfter_(masterlistMetadata.GetLoadAfterFiles()), userLoadAfter_(userMetadata.GetLoadAfterFiles()), masterlistReq_(masterlistMetadata.GetRequirements()), diff --git a/src/tests/api/interface/main.cpp b/src/tests/api/interface/main.cpp index 34ffffd5..af00ddfd 100644 --- a/src/tests/api/interface/main.cpp +++ b/src/tests/api/interface/main.cpp @@ -37,12 +37,12 @@ int main(int argc, char **argv) { namespace loot { namespace test { -void testLoggingCallback(LogLevel, const char *) { +void testLoggingCallback(LogLevel, std::string_view) { // Do nothing. } struct TestLogger { - void callback(LogLevel, const char *message) { + void callback(LogLevel, std::string_view message) { loggedMessages += std::string(message); } @@ -56,7 +56,7 @@ TEST(SetLoggingCallback, shouldAcceptAFreeFunction) { CreateGameHandle(GameType::tes4, "dummy"); FAIL(); } catch (...) { - SetLoggingCallback([](LogLevel, const char *) {}); + SetLoggingCallback([](LogLevel, std::string_view) {}); } } @@ -77,13 +77,13 @@ TEST(SetLoggingCallback, shouldAcceptAMemberFunction) { "IV: Oblivion\" with game path \"dummy\" and game local path \"\"", testLogger.loggedMessages); - SetLoggingCallback([](LogLevel, const char *) {}); + SetLoggingCallback([](LogLevel, std::string_view) {}); } } TEST(SetLoggingCallback, shouldAcceptALambdaFunction) { std::string loggedMessages; - auto callback = [&](LogLevel, const char *string) { + auto callback = [&](LogLevel, std::string_view string) { loggedMessages += std::string(string); }; SetLoggingCallback(callback); @@ -97,7 +97,7 @@ TEST(SetLoggingCallback, shouldAcceptALambdaFunction) { "IV: Oblivion\" with game path \"dummy\" and game local path \"\"", loggedMessages); - SetLoggingCallback([](LogLevel, const char *) {}); + SetLoggingCallback([](LogLevel, std::string_view) {}); } } @@ -105,7 +105,7 @@ TEST(SetLoggingCallback, shouldNotBreakLoggingIfPassedLambdaFunctionGoesOutOfScope) { std::string loggedMessages; { - SetLoggingCallback([&](LogLevel, const char *string) { + SetLoggingCallback([&](LogLevel, std::string_view string) { loggedMessages += std::string(string); }); } @@ -119,13 +119,13 @@ TEST(SetLoggingCallback, "IV: Oblivion\" with game path \"dummy\" and game local path \"\"", loggedMessages); - SetLoggingCallback([](LogLevel, const char *) {}); + SetLoggingCallback([](LogLevel, std::string_view) {}); } } TEST(SetLogLevel, shouldOnlyRunTheCallbackForMessagesAtOrAboveTheGivenLevel) { std::vector> loggedMessages; - auto callback = [&](LogLevel level, const char *string) { + auto callback = [&](LogLevel level, std::string_view string) { loggedMessages.push_back(std::make_pair(level, std::string(string))); }; SetLoggingCallback(callback); @@ -151,7 +151,7 @@ TEST(SetLogLevel, shouldOnlyRunTheCallbackForMessagesAtOrAboveTheGivenLevel) { "IV: Oblivion\" with game path \"dummy\" and game local path \"\"", loggedMessages[0].second); - SetLoggingCallback([](LogLevel, const char *) {}); + SetLoggingCallback([](LogLevel, std::string_view) {}); } } }