From cba03b873afb631944cd23f21c526936bd04b582 Mon Sep 17 00:00:00 2001 From: JOAN VINYALS YLLA CATALA Date: Wed, 3 Jul 2024 16:23:18 +0200 Subject: [PATCH 1/3] Add choosing backend from env --- src/delegator.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/delegator.cpp b/src/delegator.cpp index 142fea7..b0d187a 100644 --- a/src/delegator.cpp +++ b/src/delegator.cpp @@ -91,6 +91,19 @@ void Delegator::Init(std::string_view nesting_mode, std::string_view backend) { break; } + if (backend.compare("env") == 0) { + // We instanciate the variable here, because its only required if the + // backend is set to "env". + EnvironmentVariable env_backend = EnvironmentVariable< + std::string>( + "BACKEND", + "Set the backend to use, when init with \"env\" backend name.", + true); + + // Unwraping the std::optional is safe because the variable is required. + backend = env_backend.getValue().value(); + } + // next choose backend switch (nesting_mode_) { case NestingMode::Detection: -- GitLab From 6c4231e6a2a80fc408d74116a1d0ca9497e81812 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Thu, 4 Jul 2024 19:43:25 +0200 Subject: [PATCH 2/3] Move string handling into class --- src/utils/CMakeLists.txt | 2 +- src/utils/environment_variable.cpp | 24 ++++++++---------------- src/utils/environment_variable.hpp | 5 ----- src/utils/string_helpers.cpp | 14 ++++++++++++++ src/utils/string_helpers.hpp | 8 ++++++++ 5 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 src/utils/string_helpers.cpp create mode 100644 src/utils/string_helpers.hpp diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 369cedf..ea714ac 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1 +1 @@ -target_sources(nesmik PRIVATE environment_variable.cpp parallelism_helper.cpp) \ No newline at end of file +target_sources(nesmik PRIVATE environment_variable.cpp parallelism_helper.cpp string_helpers.cpp) \ No newline at end of file diff --git a/src/utils/environment_variable.cpp b/src/utils/environment_variable.cpp index 589d53d..9b1df9d 100644 --- a/src/utils/environment_variable.cpp +++ b/src/utils/environment_variable.cpp @@ -4,26 +4,18 @@ #include #include -#include "environment_variable.hpp" - -// some helper function to compare case insenstive strings -bool charToLowerEquals(char a, char b) { - return std::tolower(static_cast(a)) == - std::tolower(static_cast(b)); -} +#include -bool compareCaseInsenstive(const std::string &a, const std::string &b) { - return std::equal(a.begin(), a.end(), b.begin(), b.end(), charToLowerEquals); -} +#include "environment_variable.hpp" template <> std::optional fromEnvString(const std::string &env_string) { - if (compareCaseInsenstive(env_string, "0") || - compareCaseInsenstive(env_string, "false") || - compareCaseInsenstive(env_string, "off")) { + if (stringsAreCaseInsensitiveEqual(env_string, "0") || + stringsAreCaseInsensitiveEqual(env_string, "false") || + stringsAreCaseInsensitiveEqual(env_string, "off")) { return std::optional{false}; - } else if (compareCaseInsenstive(env_string, "1") || - compareCaseInsenstive(env_string, "true") || - compareCaseInsenstive(env_string, "on")) { + } else if (stringsAreCaseInsensitiveEqual(env_string, "1") || + stringsAreCaseInsensitiveEqual(env_string, "true") || + stringsAreCaseInsensitiveEqual(env_string, "on")) { return std::optional{true}; } else { return std::nullopt; diff --git a/src/utils/environment_variable.hpp b/src/utils/environment_variable.hpp index d4543b0..97834f0 100644 --- a/src/utils/environment_variable.hpp +++ b/src/utils/environment_variable.hpp @@ -7,11 +7,6 @@ #include #include -// some helper function to compare case insenstive strings -bool charToLowerEquals(char a, char b); - -bool compareCaseInsenstive(const std::string &a, const std::string &b); - // dont allow the compiler to guess, but force user to implement conversion template std::optional fromEnvString(const std::string &env_string) = delete; diff --git a/src/utils/string_helpers.cpp b/src/utils/string_helpers.cpp new file mode 100644 index 0000000..15de8fc --- /dev/null +++ b/src/utils/string_helpers.cpp @@ -0,0 +1,14 @@ +#include +#include + +#include "string_helpers.hpp" + +// some helper function to compare case insenstive strings +bool charToLowerEquals(char a, char b) { + return std::tolower(static_cast(a)) == + std::tolower(static_cast(b)); +} + +bool stringsAreCaseInsensitiveEqual(const std::string &a, const std::string &b) { + return std::equal(a.begin(), a.end(), b.begin(), b.end(), charToLowerEquals); +} diff --git a/src/utils/string_helpers.hpp b/src/utils/string_helpers.hpp new file mode 100644 index 0000000..043600d --- /dev/null +++ b/src/utils/string_helpers.hpp @@ -0,0 +1,8 @@ +#ifndef NESMIK_STRING_HELPERS_HPP +#define NESMIK_STRING_HELPERS_HPP + +#include + +bool stringsAreCaseInsensitiveEqual(const std::string &a, const std::string &b); + +#endif // NESMIK_STRING_HELPERS_HPP -- GitLab From 75b922a10bfb2ce2501984f22cae2f4ed147e05e Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Thu, 4 Jul 2024 19:43:51 +0200 Subject: [PATCH 3/3] Make comparison CaseInsensitive --- src/delegator.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/delegator.cpp b/src/delegator.cpp index b0d187a..72958f7 100644 --- a/src/delegator.cpp +++ b/src/delegator.cpp @@ -10,6 +10,7 @@ #include #include +#include #ifdef ENABLE_DLB #include "backends/dlb/dlb/dlb.hpp" #include "backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp" @@ -91,7 +92,7 @@ void Delegator::Init(std::string_view nesting_mode, std::string_view backend) { break; } - if (backend.compare("env") == 0) { + if (stringsAreCaseInsensitiveEqual("ENV",std::string(backend))) { // We instanciate the variable here, because its only required if the // backend is set to "env". EnvironmentVariable env_backend = EnvironmentVariable< -- GitLab