From d6c0f7be057aaff0c91c13d32998b6bb5b502100 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 17:59:59 +0200 Subject: [PATCH] constexpr --- .clang-format | 2 +- slsSupportLib/include/network_utils.h | 18 ++++++------ slsSupportLib/src/network_utils.cpp | 17 ++++++++---- slsSupportLib/src/string_utils.cpp | 40 ++++++++++----------------- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/.clang-format b/.clang-format index 1032b76aa..dae9439c4 100644 --- a/.clang-format +++ b/.clang-format @@ -2,5 +2,5 @@ BasedOnStyle: LLVM IndentWidth: 4 UseTab: Never -ColumnLimit: 100 +ColumnLimit: 80 AlignConsecutiveAssignments: false \ No newline at end of file diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index d546486f4..5a759d765 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -11,15 +11,15 @@ class IpAddr { uint32_t addr_{0}; public: - constexpr IpAddr(uint32_t address) noexcept: addr_{address} {} + constexpr IpAddr(uint32_t address) noexcept : addr_{address} {} IpAddr(const std::string &address); IpAddr(const char *address); std::string str() const; std::string hex() const; - bool operator==(const IpAddr &other) const { return addr_ == other.addr_; } - bool operator!=(const IpAddr &other) const { return addr_ != other.addr_; } - bool operator==(const uint32_t other) const { return addr_ == other; } - bool operator!=(const uint32_t other) const { return addr_ != other; } + constexpr bool operator==(const IpAddr &other) const noexcept { return addr_ == other.addr_; } + constexpr bool operator!=(const IpAddr &other) const noexcept { return addr_ != other.addr_; } + constexpr bool operator==(const uint32_t other) const noexcept { return addr_ == other; } + constexpr bool operator!=(const uint32_t other) const noexcept { return addr_ != other; } }; class MacAddr { @@ -33,10 +33,10 @@ class MacAddr { MacAddr(const char *address); std::string str() const { return to_hex(':'); } std::string hex() const { return to_hex(); } - bool operator==(const MacAddr &other) const { return addr_ == other.addr_; } - bool operator!=(const MacAddr &other) const { return addr_ != other.addr_; } - bool operator==(const uint64_t other) const { return addr_ == other; } - bool operator!=(const uint64_t other) const { return addr_ != other; } + constexpr bool operator==(const MacAddr &other) const noexcept { return addr_ == other.addr_; } + constexpr bool operator!=(const MacAddr &other) const noexcept { return addr_ != other.addr_; } + constexpr bool operator==(const uint64_t other) const noexcept { return addr_ == other; } + constexpr bool operator!=(const uint64_t other) const noexcept { return addr_ != other; } }; std::ostream &operator<<(std::ostream &out, const IpAddr &addr); diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index 4eb785f8c..061837f21 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -16,8 +16,9 @@ namespace sls { - -IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &addr_); } +IpAddr::IpAddr(const std::string &address) { + inet_pton(AF_INET, address.c_str(), &addr_); +} IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); } std::string IpAddr::str() const { char ipstring[INET_ADDRSTRLEN]{}; @@ -34,8 +35,8 @@ std::string IpAddr::hex() const { } MacAddr::MacAddr(std::string mac) { - if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') || - (mac[11] != ':') || (mac[14] != ':')) { + if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || + (mac[8] != ':') || (mac[11] != ':') || (mac[14] != ':')) { addr_ = 0; } else { mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); @@ -56,9 +57,13 @@ std::string MacAddr::to_hex(const char delimiter) const { return ss.str(); } -std::ostream &operator<<(std::ostream &out, const IpAddr &addr) { return out << addr.str(); } +std::ostream &operator<<(std::ostream &out, const IpAddr &addr) { + return out << addr.str(); +} -std::ostream &operator<<(std::ostream &out, const MacAddr &addr) { return out << addr.str(); } +std::ostream &operator<<(std::ostream &out, const MacAddr &addr) { + return out << addr.str(); +} uint32_t HostnameToIp(const char *hostname) { struct addrinfo hints, *result; diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 3bf090577..7c90eab25 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -2,14 +2,12 @@ #include "string_utils.h" #include "container_utils.h" #include "network_utils.h" -#include -#include #include -namespace sls{ +#include +#include +namespace sls { - -std::vector split(const std::string& strToSplit, char delimeter) -{ +std::vector split(const std::string &strToSplit, char delimeter) { std::stringstream ss(strToSplit); std::string item; std::vector splittedStrings; @@ -19,55 +17,47 @@ std::vector split(const std::string& strToSplit, char delimeter) return splittedStrings; } - -std::string concatenateNonEmptyStrings(const std::vector& vec){ +std::string concatenateNonEmptyStrings(const std::vector &vec) { std::string ret; - for (const auto& s : vec) + for (const auto &s : vec) if (!s.empty()) ret += s + '+'; return ret; } -std::string concatenateIfDifferent(const std::vector& container) -{ +std::string concatenateIfDifferent(const std::vector &container) { if (allEqual(container)) { return container.front(); } else { std::string result; - for (const auto& s : container) + for (const auto &s : container) result += s + '+'; return result; } } -template -std::string concatenateIfDifferent(const std::vector& container) -{ +template +std::string concatenateIfDifferent(const std::vector &container) { if (allEqual(container)) { return container.front().str(); } else { std::string result; - for (const auto& s : container) + for (const auto &s : container) result += s.str() + '+'; return result; } } -template std::string concatenateIfDifferent(const std::vector&); -template std::string concatenateIfDifferent(const std::vector&); +template std::string concatenateIfDifferent(const std::vector &); +template std::string concatenateIfDifferent(const std::vector &); - -std::string stringIpToHex(const std::string& ip) -{ +std::string stringIpToHex(const std::string &ip) { std::istringstream iss(ip); std::ostringstream oss; std::string item; - while (std::getline(iss, item, '.')) - { + while (std::getline(iss, item, '.')) { oss << std::setw(2) << std::setfill('0') << std::hex << std::stoi(item); } return oss.str(); } - - }; // namespace sls \ No newline at end of file