From 8a8c534f531efd3a3ac3e8da77f5f702c0dbdd08 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Fri, 5 Apr 2019 18:24:16 +0800 Subject: [PATCH] use ipc::detail::max/min instead of std::max/min --- src/ipc.cpp | 10 +++------- src/memory/alloc.h | 8 +++----- src/platform/detail.h | 13 +++++++++++++ src/platform/to_tchar.h | 5 +++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/ipc.cpp b/src/ipc.cpp index 45e22bc..b561729 100755 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -29,11 +29,7 @@ using namespace ipc; using msg_id_t = std::size_t; template = 201703L - std::size_t AlignSize = (std::min)(DataSize, alignof(std::max_align_t))> -#else /*__cplusplus < 201703L*/ - std::size_t AlignSize = (alignof(std::max_align_t) < DataSize) ? alignof(std::max_align_t) : DataSize> -#endif/*__cplusplus < 201703L*/ + std::size_t AlignSize = (ipc::detail::min)(DataSize, alignof(std::max_align_t))> struct msg_t; template @@ -60,7 +56,7 @@ struct msg_t { template buff_t make_cache(T& data, std::size_t size) { auto ptr = mem::alloc(size); - std::memcpy(ptr, &data, (std::min)(sizeof(data), size)); + std::memcpy(ptr, &data, (ipc::detail::min)(sizeof(data), size)); return { ptr, size, mem::free }; } @@ -74,7 +70,7 @@ struct cache_t { void append(void const * data, std::size_t size) { if (fill_ >= buff_.size() || data == nullptr || size == 0) return; - auto new_fill = (std::min)(fill_ + size, buff_.size()); + auto new_fill = (ipc::detail::min)(fill_ + size, buff_.size()); std::memcpy(static_cast(buff_.data()) + fill_, data, new_fill - fill_); fill_ = new_fill; } diff --git a/src/memory/alloc.h b/src/memory/alloc.h index 9a5627e..ace13a5 100644 --- a/src/memory/alloc.h +++ b/src/memory/alloc.h @@ -10,6 +10,8 @@ #include "def.h" #include "rw_lock.h" +#include "platform/detail.h" + namespace ipc { namespace mem { @@ -148,11 +150,7 @@ public: using alloc_policy = AllocP; enum : std::size_t { -#if __cplusplus >= 201703L - block_size = (std::max)(BlockSize, sizeof(void*)) -#else /*__cplusplus < 201703L*/ - block_size = (BlockSize < sizeof(void*)) ? sizeof(void*) : BlockSize -#endif/*__cplusplus < 201703L*/ + block_size = (ipc::detail::max)(BlockSize, sizeof(void*)) }; private: diff --git a/src/platform/detail.h b/src/platform/detail.h index 85be6a3..23f1b93 100644 --- a/src/platform/detail.h +++ b/src/platform/detail.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "def.h" #include "export.h" @@ -57,6 +58,8 @@ namespace detail { using std::unique_ptr; using std::unique_lock; using std::shared_lock; +using std::max; +using std::min; #else /*__cplusplus < 201703L*/ @@ -81,6 +84,16 @@ constexpr auto shared_lock(T&& lc) { return std::shared_lock> { std::forward(lc) }; } +template +constexpr const T& (max)(const T& a, const T& b) { + return (a < b) ? b : a; +} + +template +constexpr const T& (min)(const T& a, const T& b) { + return (b < a) ? b : a; +} + #endif/*__cplusplus < 201703L*/ template diff --git a/src/platform/to_tchar.h b/src/platform/to_tchar.h index 3903e6f..9e4b49c 100644 --- a/src/platform/to_tchar.h +++ b/src/platform/to_tchar.h @@ -6,11 +6,12 @@ #include #include #include -#include #include #include "concept.h" +#include "platform/detail.h" + namespace ipc::detail { struct has_value_type_ { @@ -49,7 +50,7 @@ inline auto to_tchar(T* dst, char const * src, std::size_t size) -> IsSameChar inline auto to_tchar(T* dst, char const * src, std::size_t size) -> IsSameChar { auto wstr = std::wstring_convert>{}.from_bytes(src, src + size); - std::memcpy(dst, wstr.data(), (std::min)(wstr.size(), size)); + std::memcpy(dst, wstr.data(), (ipc::detail::min)(wstr.size(), size)); } } // namespace ipc::detail