constexpr

This commit is contained in:
Erik Frojdh 2019-04-02 17:59:59 +02:00
parent 6fc388bf78
commit d6c0f7be05
4 changed files with 36 additions and 41 deletions

View File

@ -2,5 +2,5 @@ BasedOnStyle: LLVM
IndentWidth: 4
UseTab: Never
ColumnLimit: 100
ColumnLimit: 80
AlignConsecutiveAssignments: false

View File

@ -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);

View File

@ -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;

View File

@ -2,14 +2,12 @@
#include "string_utils.h"
#include "container_utils.h"
#include "network_utils.h"
#include <sstream>
#include <iomanip>
#include <algorithm>
namespace sls{
#include <iomanip>
#include <sstream>
namespace sls {
std::vector<std::string> split(const std::string& strToSplit, char delimeter)
{
std::vector<std::string> split(const std::string &strToSplit, char delimeter) {
std::stringstream ss(strToSplit);
std::string item;
std::vector<std::string> splittedStrings;
@ -19,55 +17,47 @@ std::vector<std::string> split(const std::string& strToSplit, char delimeter)
return splittedStrings;
}
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
std::string concatenateNonEmptyStrings(const std::vector<std::string> &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<std::string>& container)
{
std::string concatenateIfDifferent(const std::vector<std::string> &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<typename T>
std::string concatenateIfDifferent(const std::vector<T>& container)
{
template <typename T>
std::string concatenateIfDifferent(const std::vector<T> &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<IpAddr>&);
template std::string concatenateIfDifferent(const std::vector<MacAddr>&);
template std::string concatenateIfDifferent(const std::vector<IpAddr> &);
template std::string concatenateIfDifferent(const std::vector<MacAddr> &);
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