From c379241007ed72bc100afd72d4bcbf896c645796 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:41:26 +0200 Subject: [PATCH] Code improvements through clang tidy comments. --- ecal/core/include/ecal/config/configuration.h | 2 +- .../ecal/types/ecal_custom_data_types.h | 2 +- ecal/core/src/config/configuration_reader.cpp | 18 +++++----- .../core/src/config/configuration_to_yaml.cpp | 36 +++++++++---------- ecal/core/src/config/default_config.h | 2 +- .../src/config/ecal_config_initializer.cpp | 19 +++++----- .../core/src/types/ecal_custom_data_types.cpp | 10 +++--- 7 files changed, 45 insertions(+), 44 deletions(-) diff --git a/ecal/core/include/ecal/config/configuration.h b/ecal/core/include/ecal/config/configuration.h index 6622cefcf6..898211b94c 100644 --- a/ecal/core/include/ecal/config/configuration.h +++ b/ecal/core/include/ecal/config/configuration.h @@ -65,7 +65,7 @@ namespace eCAL ECAL_API void InitConfigWithDefaultYaml(); ECAL_API void InitConfigWithDefaults(); - ECAL_API void InitConfigFromFile(const std::string yaml_path_); + ECAL_API void InitConfigFromFile(const std::string& yaml_path_); ECAL_API std::string GetYamlFilePath(); diff --git a/ecal/core/include/ecal/types/ecal_custom_data_types.h b/ecal/core/include/ecal/types/ecal_custom_data_types.h index e32da0274a..0d833f15a2 100644 --- a/ecal/core/include/ecal/types/ecal_custom_data_types.h +++ b/ecal/core/include/ecal/types/ecal_custom_data_types.h @@ -54,7 +54,7 @@ namespace eCAL ECAL_API IpAddressV4& operator=(const std::string& ip_string_); ECAL_API IpAddressV4& operator=(const char* ip_string_); ECAL_API operator std::string(); - ECAL_API bool operator==(const eCAL::Types::IpAddressV4 rhs) const; + ECAL_API bool operator==(const eCAL::Types::IpAddressV4& rhs) const; ECAL_API friend bool operator==(eCAL::Types::IpAddressV4 lhs, const char* ip_string_); ECAL_API friend bool operator==(const char* ip_string_, eCAL::Types::IpAddressV4 rhs); ECAL_API friend bool operator==(eCAL::Types::IpAddressV4 lhs, const std::string& ip_string_); diff --git a/ecal/core/src/config/configuration_reader.cpp b/ecal/core/src/config/configuration_reader.cpp index 4cbdbc749e..a1eb5c0695 100644 --- a/ecal/core/src/config/configuration_reader.cpp +++ b/ecal/core/src/config/configuration_reader.cpp @@ -23,21 +23,21 @@ namespace eCAL { void YamlFileToConfig(const std::string& filename_, eCAL::Configuration& config_) { - YAML::Node yaml = YAML::LoadFile(filename_); + const YAML::Node yaml = YAML::LoadFile(filename_); MapConfiguration(yaml, config_); }; void YamlStringToConfig(const std::string& yaml_string_, eCAL::Configuration& config_) { - YAML::Node yaml = YAML::Load(yaml_string_); + const YAML::Node yaml = YAML::Load(yaml_string_); MapConfiguration(yaml, config_); }; bool ConfigToYamlFile(const std::string& file_name_, const eCAL::Configuration& config_) { - YAML::Node node(config_); + const YAML::Node node(config_); std::ofstream file(file_name_); if (file.is_open()) { @@ -52,11 +52,11 @@ namespace eCAL void MergeYamlNodes(YAML::Node& base, const YAML::Node& other) { std::stack> nodes; - nodes.push(std::make_pair(base, other)); + nodes.emplace(std::make_pair(base, other)); while (!nodes.empty()) { - std::pair nodePair = nodes.top(); + const std::pair nodePair = nodes.top(); nodes.pop(); YAML::Node baseNode = nodePair.first; @@ -64,10 +64,10 @@ namespace eCAL for (YAML::const_iterator it = otherNode.begin(); it != otherNode.end(); ++it) { - YAML::Node key = it->first; - YAML::Node value = it->second; + const YAML::Node key = it->first; + const YAML::Node value = it->second; - std::string key_as_string = ""; + std::string key_as_string; switch (key.Type()) { @@ -83,7 +83,7 @@ namespace eCAL { if (value.IsMap() && baseNode[key_as_string].IsMap()) { - nodes.push(std::make_pair(baseNode[key_as_string], value)); // Push nested nodes to stack + nodes.emplace(baseNode[key_as_string], value); // Push nested nodes to stack } else { diff --git a/ecal/core/src/config/configuration_to_yaml.cpp b/ecal/core/src/config/configuration_to_yaml.cpp index 2d49236ccb..00adacc7a7 100644 --- a/ecal/core/src/config/configuration_to_yaml.cpp +++ b/ecal/core/src/config/configuration_to_yaml.cpp @@ -22,7 +22,7 @@ namespace YAML { // create excluding filter list char filter_mask = log_level_none; - for (auto& it : filter_) + for (const auto& it : filter_) { if (it == "all") filter_mask |= log_level_all; if (it == "info") filter_mask |= log_level_info; @@ -41,15 +41,15 @@ namespace YAML std::vector LogLevelToVector(eCAL_Logging_Filter filter_mask) { std::vector filter; - if (filter_mask & log_level_all) filter.push_back("all"); - if (filter_mask & log_level_info) filter.push_back("info"); - if (filter_mask & log_level_warning)filter.push_back("warning"); - if (filter_mask & log_level_error) filter.push_back("error"); - if (filter_mask & log_level_fatal) filter.push_back("fatal"); - if (filter_mask & log_level_debug1) filter.push_back("debug1"); - if (filter_mask & log_level_debug2) filter.push_back("debug2"); - if (filter_mask & log_level_debug3) filter.push_back("debug3"); - if (filter_mask & log_level_debug4) filter.push_back("debug4"); + if ((filter_mask & log_level_all) != 0) filter.emplace_back("all"); + if ((filter_mask & log_level_info) != 0) filter.emplace_back("info"); + if ((filter_mask & log_level_warning) != 0) filter.emplace_back("warning"); + if ((filter_mask & log_level_error) != 0) filter.emplace_back("error"); + if ((filter_mask & log_level_fatal) != 0) filter.emplace_back("fatal"); + if ((filter_mask & log_level_debug1) != 0) filter.emplace_back("debug1"); + if ((filter_mask & log_level_debug2) != 0) filter.emplace_back("debug2"); + if ((filter_mask & log_level_debug3) != 0) filter.emplace_back("debug3"); + if ((filter_mask & log_level_debug4) != 0) filter.emplace_back("debug4"); return filter; } @@ -57,11 +57,11 @@ namespace YAML eCAL::Publisher::Configuration::LayerPriorityVector transformLayerStrToEnum(const std::vector& string_vector_) { eCAL::Publisher::Configuration::LayerPriorityVector layer_priority_vector; - for (auto& layer_as_string : string_vector_) + for (const auto& layer_as_string : string_vector_) { - if (layer_as_string == "shm") layer_priority_vector.push_back(eCAL::TLayer::tlayer_shm); - if (layer_as_string == "udp") layer_priority_vector.push_back(eCAL::TLayer::tlayer_udp_mc); - if (layer_as_string == "tcp") layer_priority_vector.push_back(eCAL::TLayer::tlayer_tcp); + if (layer_as_string == "shm") layer_priority_vector.emplace_back(eCAL::TLayer::tlayer_shm); + if (layer_as_string == "udp") layer_priority_vector.emplace_back(eCAL::TLayer::tlayer_udp_mc); + if (layer_as_string == "tcp") layer_priority_vector.emplace_back(eCAL::TLayer::tlayer_tcp); } return layer_priority_vector; @@ -70,18 +70,18 @@ namespace YAML std::vector transformLayerEnumToStr(const eCAL::Publisher::Configuration::LayerPriorityVector& enum_vector_) { std::vector layer_priority_vector; - for (auto& layer_as_enum : enum_vector_) + for (const auto& layer_as_enum : enum_vector_) { switch (layer_as_enum) { case eCAL::TLayer::tlayer_shm: - layer_priority_vector.push_back("shm"); + layer_priority_vector.emplace_back("shm"); break; case eCAL::TLayer::tlayer_udp_mc: - layer_priority_vector.push_back("udp"); + layer_priority_vector.emplace_back("udp"); break; case eCAL::TLayer::tlayer_tcp: - layer_priority_vector.push_back("tcp"); + layer_priority_vector.emplace_back("tcp"); break; default: break; diff --git a/ecal/core/src/config/default_config.h b/ecal/core/src/config/default_config.h index 5620644941..abe4bf28bb 100644 --- a/ecal/core/src/config/default_config.h +++ b/ecal/core/src/config/default_config.h @@ -2,7 +2,7 @@ #include -std::string default_config = R"(# _____ _ _ ____ _ _ +const std::string default_config = R"(# _____ _ _ ____ _ _ # | ____|___| (_)_ __ ___ ___ ___ / ___| / \ | | # | _| / __| | | '_ \/ __|/ _ \ _____ / _ \ | / _ \ | | # | |__| (__| | | |_) \__ \ __/ |_____| | __/ |___ / ___ \| |___ diff --git a/ecal/core/src/config/ecal_config_initializer.cpp b/ecal/core/src/config/ecal_config_initializer.cpp index 2ffdea2bec..7f62c2f936 100644 --- a/ecal/core/src/config/ecal_config_initializer.cpp +++ b/ecal/core/src/config/ecal_config_initializer.cpp @@ -177,12 +177,12 @@ namespace // ----------------------------------------------------------- // precedence 0: relative path to executable // ----------------------------------------------------------- - std::string cwd_directory_path = cwdPath(); + const std::string cwd_directory_path = cwdPath(); std::vector ecal_default_paths = getEcalDefaultPaths(); ecal_default_paths.emplace(ecal_default_paths.begin(), cwd_directory_path); - std::string found_path = findValidConfigPath(ecal_default_paths, config_file_); + const std::string found_path = findValidConfigPath(ecal_default_paths, config_file_); // check in case user provided whole path if (found_path.empty()) @@ -196,9 +196,9 @@ namespace namespace eCAL { - void Configuration::InitConfigFromFile(const std::string yaml_path_) + void Configuration::InitConfigFromFile(const std::string& yaml_path_) { - std::string yaml_path = checkForValidConfigFilePath(yaml_path_); + const std::string yaml_path = checkForValidConfigFilePath(yaml_path_); if (!yaml_path.empty()) { eCAL::Config::YamlFileToConfig(yaml_path, *this); @@ -233,9 +233,9 @@ namespace eCAL Init(args_); } - void Configuration::Init(const std::vector& arguments_) + void Configuration::Init(const std::vector& args_) { - Config::CmdParser parser(arguments_); + Config::CmdParser parser(args_); command_line_arguments.user_yaml = parser.getUserIni(); command_line_arguments.dump_config = parser.getDumpConfig(); @@ -301,7 +301,7 @@ namespace eCAL ECAL_API std::string GeteCALConfigPath() { // Check for first directory which contains the ini file. - std::vector search_directories = getEcalDefaultPaths(); + const std::vector search_directories = getEcalDefaultPaths(); return findValidConfigPath(search_directories, ECAL_DEFAULT_CFG); } @@ -325,8 +325,9 @@ namespace eCAL #endif /* ECAL_OS_WINDOWS */ #ifdef ECAL_OS_LINUX - const char *hdir; - if ((hdir = getenv("HOME")) == NULL) { + const char *hdir = nullptr; + hdir = getenv("HOME"); + if (hdir == nullptr) { hdir = getpwuid(getuid())->pw_dir; } home_path += hdir; diff --git a/ecal/core/src/types/ecal_custom_data_types.cpp b/ecal/core/src/types/ecal_custom_data_types.cpp index 826214d58c..efa3abd252 100644 --- a/ecal/core/src/types/ecal_custom_data_types.cpp +++ b/ecal/core/src/types/ecal_custom_data_types.cpp @@ -86,10 +86,10 @@ namespace eCAL std::ostream& operator<<(std::ostream& os, const IpAddressV4& ipv4) { os << ipv4.Get(); return os; }; - bool IpAddressV4::operator==(const eCAL::Types::IpAddressV4 other) const { return m_ip_address == other.Get(); }; - bool operator==(eCAL::Types::IpAddressV4 lhs, const char* ip_string_) { return lhs.Get() == std::string(ip_string_); }; - bool operator==(const char* ip_string_, eCAL::Types::IpAddressV4 rhs) { return rhs == ip_string_; }; - bool operator==(eCAL::Types::IpAddressV4 lhs, const std::string& ip_string_) { return lhs.Get() == ip_string_; }; - bool operator==(const std::string& ip_string_, eCAL::Types::IpAddressV4 rhs) { return rhs == ip_string_; }; + bool IpAddressV4::operator==(const eCAL::Types::IpAddressV4& rhs) const { return m_ip_address == rhs.Get(); }; + bool operator==(eCAL::Types::IpAddressV4 lhs, const char* ip_string_) { return lhs.Get() == std::string(ip_string_); }; + bool operator==(const char* ip_string_, eCAL::Types::IpAddressV4 rhs) { return rhs == ip_string_; }; + bool operator==(eCAL::Types::IpAddressV4 lhs, const std::string& ip_string_) { return lhs.Get() == ip_string_; }; + bool operator==(const std::string& ip_string_, eCAL::Types::IpAddressV4 rhs) { return rhs == ip_string_; }; } }