mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-23 23:10:02 +02:00
constexpr
This commit is contained in:
parent
6fc388bf78
commit
d6c0f7be05
@ -2,5 +2,5 @@ BasedOnStyle: LLVM
|
|||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
|
|
||||||
UseTab: Never
|
UseTab: Never
|
||||||
ColumnLimit: 100
|
ColumnLimit: 80
|
||||||
AlignConsecutiveAssignments: false
|
AlignConsecutiveAssignments: false
|
@ -11,15 +11,15 @@ class IpAddr {
|
|||||||
uint32_t addr_{0};
|
uint32_t addr_{0};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr IpAddr(uint32_t address) noexcept: addr_{address} {}
|
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
|
||||||
IpAddr(const std::string &address);
|
IpAddr(const std::string &address);
|
||||||
IpAddr(const char *address);
|
IpAddr(const char *address);
|
||||||
std::string str() const;
|
std::string str() const;
|
||||||
std::string hex() const;
|
std::string hex() const;
|
||||||
bool operator==(const IpAddr &other) const { return addr_ == other.addr_; }
|
constexpr bool operator==(const IpAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||||
bool operator!=(const IpAddr &other) const { return addr_ != other.addr_; }
|
constexpr bool operator!=(const IpAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||||
bool operator==(const uint32_t other) const { return addr_ == other; }
|
constexpr bool operator==(const uint32_t other) const noexcept { return addr_ == other; }
|
||||||
bool operator!=(const uint32_t other) const { return addr_ != other; }
|
constexpr bool operator!=(const uint32_t other) const noexcept { return addr_ != other; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class MacAddr {
|
class MacAddr {
|
||||||
@ -33,10 +33,10 @@ class MacAddr {
|
|||||||
MacAddr(const char *address);
|
MacAddr(const char *address);
|
||||||
std::string str() const { return to_hex(':'); }
|
std::string str() const { return to_hex(':'); }
|
||||||
std::string hex() const { return to_hex(); }
|
std::string hex() const { return to_hex(); }
|
||||||
bool operator==(const MacAddr &other) const { return addr_ == other.addr_; }
|
constexpr bool operator==(const MacAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||||
bool operator!=(const MacAddr &other) const { return addr_ != other.addr_; }
|
constexpr bool operator!=(const MacAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||||
bool operator==(const uint64_t other) const { return addr_ == other; }
|
constexpr bool operator==(const uint64_t other) const noexcept { return addr_ == other; }
|
||||||
bool operator!=(const uint64_t other) const { return addr_ != other; }
|
constexpr bool operator!=(const uint64_t other) const noexcept { return addr_ != other; }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
|
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
|
||||||
|
@ -16,8 +16,9 @@
|
|||||||
|
|
||||||
namespace sls {
|
namespace sls {
|
||||||
|
|
||||||
|
IpAddr::IpAddr(const std::string &address) {
|
||||||
IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &addr_); }
|
inet_pton(AF_INET, address.c_str(), &addr_);
|
||||||
|
}
|
||||||
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
|
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
|
||||||
std::string IpAddr::str() const {
|
std::string IpAddr::str() const {
|
||||||
char ipstring[INET_ADDRSTRLEN]{};
|
char ipstring[INET_ADDRSTRLEN]{};
|
||||||
@ -34,8 +35,8 @@ std::string IpAddr::hex() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MacAddr::MacAddr(std::string mac) {
|
MacAddr::MacAddr(std::string mac) {
|
||||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') ||
|
||||||
(mac[11] != ':') || (mac[14] != ':')) {
|
(mac[8] != ':') || (mac[11] != ':') || (mac[14] != ':')) {
|
||||||
addr_ = 0;
|
addr_ = 0;
|
||||||
} else {
|
} else {
|
||||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
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();
|
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) {
|
uint32_t HostnameToIp(const char *hostname) {
|
||||||
struct addrinfo hints, *result;
|
struct addrinfo hints, *result;
|
||||||
|
@ -2,14 +2,12 @@
|
|||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
#include "container_utils.h"
|
#include "container_utils.h"
|
||||||
#include "network_utils.h"
|
#include "network_utils.h"
|
||||||
#include <sstream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <algorithm>
|
#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::stringstream ss(strToSplit);
|
||||||
std::string item;
|
std::string item;
|
||||||
std::vector<std::string> splittedStrings;
|
std::vector<std::string> splittedStrings;
|
||||||
@ -19,55 +17,47 @@ std::vector<std::string> split(const std::string& strToSplit, char delimeter)
|
|||||||
return splittedStrings;
|
return splittedStrings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string concatenateNonEmptyStrings(const std::vector<std::string> &vec) {
|
||||||
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
|
|
||||||
std::string ret;
|
std::string ret;
|
||||||
for (const auto& s : vec)
|
for (const auto &s : vec)
|
||||||
if (!s.empty())
|
if (!s.empty())
|
||||||
ret += s + '+';
|
ret += s + '+';
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string concatenateIfDifferent(const std::vector<std::string>& container)
|
std::string concatenateIfDifferent(const std::vector<std::string> &container) {
|
||||||
{
|
|
||||||
if (allEqual(container)) {
|
if (allEqual(container)) {
|
||||||
return container.front();
|
return container.front();
|
||||||
} else {
|
} else {
|
||||||
std::string result;
|
std::string result;
|
||||||
for (const auto& s : container)
|
for (const auto &s : container)
|
||||||
result += s + '+';
|
result += s + '+';
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<typename T>
|
template <typename T>
|
||||||
std::string concatenateIfDifferent(const std::vector<T>& container)
|
std::string concatenateIfDifferent(const std::vector<T> &container) {
|
||||||
{
|
|
||||||
if (allEqual(container)) {
|
if (allEqual(container)) {
|
||||||
return container.front().str();
|
return container.front().str();
|
||||||
} else {
|
} else {
|
||||||
std::string result;
|
std::string result;
|
||||||
for (const auto& s : container)
|
for (const auto &s : container)
|
||||||
result += s.str() + '+';
|
result += s.str() + '+';
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template std::string concatenateIfDifferent(const std::vector<IpAddr>&);
|
template std::string concatenateIfDifferent(const std::vector<IpAddr> &);
|
||||||
template std::string concatenateIfDifferent(const std::vector<MacAddr>&);
|
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::istringstream iss(ip);
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
std::string item;
|
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);
|
oss << std::setw(2) << std::setfill('0') << std::hex << std::stoi(item);
|
||||||
}
|
}
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}; // namespace sls
|
}; // namespace sls
|
Loading…
x
Reference in New Issue
Block a user