From e34b6500d90d39f2efef6a00890621cfbce7cd8e Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sat, 22 May 2021 15:54:59 +0200 Subject: [PATCH] Utils: Add implementations for std::data and std::size These can be removed once C++17 is supported. --- src/zm_utils.h | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/zm_utils.h b/src/zm_utils.h index 13bbc2dcf..ea56f0b0b 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -35,7 +35,7 @@ typedef std::vector StringVector; std::string Trim(const std::string &str, const std::string &char_set); inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } -std::string ReplaceAll(std::string str, const std::string& old_value, const std::string& new_value); +std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value); inline void StringToUpper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), ::toupper); } StringVector Split(const std::string &str, char delim); @@ -72,32 +72,53 @@ void *sse2_aligned_memcpy(void *dest, const void *src, size_t bytes); void touch(const char *pathname); namespace ZM { -//! std::make_unique implementation (TODO: remove this once C++14 is supported) +// C++14 std::make_unique (TODO: remove this once C++14 is supported) template inline auto make_unique(Args &&...args) -> typename std::enable_if::value, std::unique_ptr>::type { return std::unique_ptr(new T(std::forward(args)...)); } - template inline auto make_unique(std::size_t size) -> typename std::enable_if::value && std::extent::value == 0, std::unique_ptr>::type { return std::unique_ptr(new typename std::remove_extent::type[size]()); } - template inline auto make_unique(Args &&...) -> typename std::enable_if::value != 0, void>::type = delete; +// C++17 std::clamp (TODO: remove this once C++17 is supported) template constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) { return comp(v, lo) ? lo : comp(hi, v) ? hi : v; } - template constexpr const T &clamp(const T &v, const T &lo, const T &hi) { return ZM::clamp(v, lo, hi, std::less{}); } + +// C++17 std::data (TODO: remove this once C++17 is supported) +template +constexpr auto data(C &c) -> decltype(c.data()) { return c.data(); } + +template +constexpr auto data(C const &c) -> decltype(c.data()) { return c.data(); } + +template +constexpr T *data(T(&a)[N]) noexcept { return a; } + +template +constexpr T const *data(const T(&a)[N]) noexcept { return a; } + +template +constexpr T const *data(std::initializer_list l) noexcept { return l.begin(); } + +// C++17 std::size (TODO: remove this once C++17 is supported) +template +constexpr auto size(const C &c) -> decltype(c.size()) { return c.size(); } + +template +constexpr std::size_t size(const T(&)[N]) noexcept { return N; } } typedef std::chrono::microseconds Microseconds; @@ -146,4 +167,5 @@ class QueryString { std::map> parameters_; }; + #endif // ZM_UTILS_H