From e5a5b9f680a10931b10d2cb8603418a1032476c0 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Fri, 18 Jan 2019 12:47:41 +0800 Subject: [PATCH] compatible with old compilers --- build/ipc.pro | 5 ++--- build/test.pro | 7 ++++--- include/buffer.h | 18 ++++++++++++++++++ include/elem_circ.h | 18 ++++++++++++++++++ include/rw_lock.h | 2 +- src/memory/alloc.hpp | 8 ++++++-- src/memory/resource.hpp | 9 +++++++++ src/memory/wrapper.hpp | 20 +++++++++++++++++--- src/platform/shm_linux.cpp | 4 ++-- src/platform/shm_win.cpp | 4 ++-- src/platform/waiter_linux.h | 22 ++++++++++++++++++---- test/test_circ.cpp | 8 ++++---- 12 files changed, 101 insertions(+), 24 deletions(-) diff --git a/build/ipc.pro b/build/ipc.pro index 3240709..6ed7355 100644 --- a/build/ipc.pro +++ b/build/ipc.pro @@ -2,10 +2,9 @@ TEMPLATE = lib TARGET = ipc CONFIG -= qt -CONFIG += c++1z # may be useless +CONFIG += c++14 c++1z -msvc:QMAKE_CXXFLAGS += /std:c++17 -else:QMAKE_CXXFLAGS += -std=gnu++1z +!msvc:QMAKE_CXXFLAGS += -Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable DEFINES += __IPC_LIBRARY__ DESTDIR = ../output diff --git a/build/test.pro b/build/test.pro index 9a1b632..c1b2635 100644 --- a/build/test.pro +++ b/build/test.pro @@ -2,13 +2,14 @@ TEMPLATE = app QT += core testlib QT -= gui -CONFIG += console c++1z + +CONFIG += console +CONFIG += c++14 c++1z CONFIG -= app_bundle DESTDIR = ../output -msvc:QMAKE_CXXFLAGS += /std:c++17 -else:QMAKE_CXXFLAGS += -std=gnu++1z -Wno-unused-function -Wno-attributes +!msvc:QMAKE_CXXFLAGS += -Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-function INCLUDEPATH += \ ../test \ diff --git a/include/buffer.h b/include/buffer.h index 27fcd4c..0f93cd7 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -36,6 +36,7 @@ public: void * data() noexcept; void const * data() const noexcept; +# if __cplusplus >= 201703L template auto data() noexcept -> std::enable_if_t, T*> { return static_cast(data()); @@ -45,6 +46,17 @@ public: auto data() const noexcept -> std::enable_if_t, T*> { return static_cast(data()); } +# else /*__cplusplus < 201703L*/ + template + auto data() noexcept -> std::enable_if_t::value, T*> { + return static_cast(data()); + } + + template + auto data() const noexcept -> std::enable_if_t::value, T*> { + return static_cast(data()); + } +# endif/*__cplusplus < 201703L*/ std::size_t size() const noexcept; @@ -57,7 +69,13 @@ public: } std::vector to_vector() const { +# if __cplusplus >= 201703L auto [d, s] = to_tuple(); +# else /*__cplusplus < 201703L*/ + auto tp = to_tuple(); + auto d = std::get<0>(tp); + auto s = std::get<1>(tp); +# endif/*__cplusplus < 201703L*/ return { static_cast(d), static_cast(d) + s diff --git a/include/elem_circ.h b/include/elem_circ.h index 369e191..659a323 100644 --- a/include/elem_circ.h +++ b/include/elem_circ.h @@ -55,8 +55,17 @@ struct prod_cons { std::atomic rd_ { 0 }; // read index std::atomic wt_ { 0 }; // write index +#if __cplusplus >= 201703L template constexpr static std::size_t elem_param = DataSize - sizeof(circ::detail::elem_head); +#else /*__cplusplus < 201703L*/ + template + struct elem_param { + enum : std::size_t { + value = DataSize - sizeof(circ::detail::elem_head) + }; + }; +#endif/*__cplusplus < 201703L*/ constexpr circ::detail::u2_t cursor() const noexcept { return 0; @@ -144,8 +153,13 @@ template <> struct prod_cons { std::atomic wt_ { 0 }; // write index +#if __cplusplus >= 201703L template constexpr static std::size_t elem_param = DataSize; +#else /*__cplusplus < 201703L*/ + template + struct elem_param { enum : std::size_t { value = DataSize }; }; +#endif/*__cplusplus < 201703L*/ /* std::atomic may not have value_type. @@ -248,7 +262,11 @@ public: using policy_t = Policy; using base_t = Policy; using head_t = ipc::conn_head; +#if __cplusplus >= 201703L using elem_t = detail::elem_t>; +#else /*__cplusplus < 201703L*/ + using elem_t = detail::elem_t::value>; +#endif/*__cplusplus < 201703L*/ enum : std::size_t { head_size = sizeof(policy_t) + sizeof(head_t), diff --git a/include/rw_lock.h b/include/rw_lock.h index 3ab483c..945fa5d 100644 --- a/include/rw_lock.h +++ b/include/rw_lock.h @@ -87,7 +87,7 @@ inline void sleep(K& k, F&& f) noexcept { template inline void sleep(K& k) noexcept { - sleep(k, []() constexpr { return false; }); + sleep(k, [] { return false; }); } } // namespace ipc diff --git a/src/memory/alloc.hpp b/src/memory/alloc.hpp index bc071a3..154e61d 100644 --- a/src/memory/alloc.hpp +++ b/src/memory/alloc.hpp @@ -11,8 +11,8 @@ namespace mem { class static_alloc { public: - static constexpr void clear() {} - static constexpr void swap(static_alloc&) {} + static void clear() {} + static void swap(static_alloc&) {} static void* alloc(std::size_t size) { return size ? std::malloc(size) : nullptr; @@ -131,7 +131,11 @@ 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*/ }; private: diff --git a/src/memory/resource.hpp b/src/memory/resource.hpp index 78016cd..548dab0 100644 --- a/src/memory/resource.hpp +++ b/src/memory/resource.hpp @@ -41,8 +41,13 @@ enum : std::size_t { base_size = sizeof(void*) }; +#if __cplusplus >= 201703L constexpr std::size_t classify(std::size_t size) { constexpr std::size_t mapping[] = { +#else /*__cplusplus < 201703L*/ +inline std::size_t classify(std::size_t size) { + static const std::size_t mapping[] = { +#endif/*__cplusplus < 201703L*/ /* 1 */ 0 , 1 , 2 , 3 , /* 2 */ @@ -56,7 +61,11 @@ constexpr std::size_t classify(std::size_t size) { 31, 31, 31, 31 }; size = (size - 1) / base_size; +#if __cplusplus >= 201703L return (size < std::size(mapping)) ? mapping[size] : 32; +#else /*__cplusplus < 201703L*/ + return (size < (sizeof(mapping) / sizeof(mapping[0]))) ? mapping[size] : 32; +#endif/*__cplusplus < 201703L*/ } template diff --git a/src/memory/wrapper.hpp b/src/memory/wrapper.hpp index 8ee6492..b96d15d 100644 --- a/src/memory/wrapper.hpp +++ b/src/memory/wrapper.hpp @@ -10,11 +10,25 @@ #include #include #include +#include #include "rw_lock.h" #include "tls_pointer.h" namespace ipc { + +namespace detail { +#if __cplusplus >= 201703L +using std::unique_lock; +#else /*__cplusplus < 201703L*/ +// deduction guides for std::unique_lock +template +constexpr auto unique_lock(T&& lc) { + return std::unique_lock> { std::forward(lc) }; +} +#endif/*__cplusplus < 201703L*/ +} // namespace detail + namespace mem { //////////////////////////////////////////////////////////////// @@ -38,7 +52,7 @@ private: alloc_t(synchronized* t) : t_ { t } { { - [[maybe_unused]] auto guard = std::unique_lock { t_->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(t_->lc_); auto it = t_->allocs_.begin(); if (it != t_->allocs_.end()) { std::tie(s_, a_) = *it; @@ -51,7 +65,7 @@ private: } ~alloc_t() { - [[maybe_unused]] auto guard = std::unique_lock { t_->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(t_->lc_); t_->allocs_.emplace(s_, a_); } @@ -82,7 +96,7 @@ public: } void clear() { - auto guard = std::unique_lock { lc_ }; + auto guard = ipc::detail::unique_lock(lc_); std::vector vec(allocs_.size()); std::size_t i = 0; for (auto& pair : allocs_) { diff --git a/src/platform/shm_linux.cpp b/src/platform/shm_linux.cpp index 9ab4763..a8f50f2 100644 --- a/src/platform/shm_linux.cpp +++ b/src/platform/shm_linux.cpp @@ -65,7 +65,7 @@ void* acquire(char const * name, std::size_t size) { auto acc = acc_of(mem); acc->fetch_add(1, std::memory_order_release); { - [[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_); m2h()->cache_.emplace(++acc, std::move(op_name)); } return acc; @@ -75,7 +75,7 @@ void release(void* mem, std::size_t size) { if (mem == nullptr) { return; } - [[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_); auto& cc = m2h()->cache_; auto it = cc.find(mem); if (it == cc.end()) { diff --git a/src/platform/shm_win.cpp b/src/platform/shm_win.cpp index 86fe7b3..6136414 100644 --- a/src/platform/shm_win.cpp +++ b/src/platform/shm_win.cpp @@ -43,7 +43,7 @@ void* acquire(char const * name, std::size_t size) { return nullptr; } { - [[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_); m2h()->cache_.emplace(mem, h); } return mem; @@ -53,7 +53,7 @@ void release(void* mem, std::size_t /*size*/) { if (mem == nullptr) { return; } - [[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ }; + [[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_); auto& cc = m2h()->cache_; auto it = cc.find(mem); if (it == cc.end()) { diff --git a/src/platform/waiter_linux.h b/src/platform/waiter_linux.h index 2808ced..a0f704f 100644 --- a/src/platform/waiter_linux.h +++ b/src/platform/waiter_linux.h @@ -7,6 +7,7 @@ #include #include +#if __cplusplus >= 201703L namespace std { // deduction guides for std::unique_ptr @@ -18,6 +19,19 @@ unique_ptr(T* p, D&& d) -> unique_ptr>; namespace ipc { namespace detail { +using std::unique_ptr; + +#else /*__cplusplus < 201703L*/ +namespace ipc { +namespace detail { + +// deduction guides for std::unique_ptr +template +constexpr auto unique_ptr(T* p, D&& d) { + return std::unique_ptr> { p, std::forward(d) }; +} +#endif/*__cplusplus < 201703L*/ + class waiter { pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond_ = PTHREAD_COND_INITIALIZER; @@ -44,20 +58,20 @@ public: if (::pthread_mutexattr_init(&mutex_attr) != 0) { return invalid(); } - [[maybe_unused]] auto guard_mutex_attr = std::unique_ptr { &mutex_attr, ::pthread_mutexattr_destroy }; + [[maybe_unused]] auto guard_mutex_attr = unique_ptr(&mutex_attr, ::pthread_mutexattr_destroy); if (::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED) != 0) { return invalid(); } if (::pthread_mutex_init(&mutex_, &mutex_attr) != 0) { return invalid(); } - auto guard_mutex = std::unique_ptr { &mutex_, ::pthread_mutex_destroy }; + auto guard_mutex = unique_ptr(&mutex_, ::pthread_mutex_destroy); // init condition pthread_condattr_t cond_attr; if (::pthread_condattr_init(&cond_attr) != 0) { return invalid(); } - [[maybe_unused]] auto guard_cond_attr = std::unique_ptr { &cond_attr, ::pthread_condattr_destroy }; + [[maybe_unused]] auto guard_cond_attr = unique_ptr(&cond_attr, ::pthread_condattr_destroy); if (::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0) { return invalid(); } @@ -86,7 +100,7 @@ public: if (::pthread_mutex_lock(&(w->mutex_)) != 0) { return false; } - [[maybe_unused]] auto guard = std::unique_ptr { &(w->mutex_), ::pthread_mutex_unlock }; + [[maybe_unused]] auto guard = unique_ptr(&(w->mutex_), ::pthread_mutex_unlock); if (::pthread_cond_wait(&(w->cond_), &(w->mutex_)) != 0) { return false; } diff --git a/test/test_circ.cpp b/test/test_circ.cpp index c9c0bf1..373461c 100644 --- a/test/test_circ.cpp +++ b/test/test_circ.cpp @@ -201,12 +201,12 @@ struct test_cq> { } } - cn_t connect_send() { - return cn_t{ ca_ }; + cn_t* connect_send() { + return nullptr; } - void send(cn_t& cn, msg_t const & msg) { - cn.push(msg); + void send(cn_t* /*cn*/, msg_t const & msg) { + cn_t{ ca_ }.push(msg); } };