From b34805de73968d1d25eeef98362c71cecc8e1615 Mon Sep 17 00:00:00 2001 From: chenjingyu <2458006366@qq.com> Date: Tue, 11 Oct 2022 02:01:45 +0800 Subject: [PATCH] =?UTF-8?q?Update=EF=BC=9A=E9=99=8D=E7=BA=A7=E5=88=B0C++11?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + CMakeLists.txt | 2 +- .../ThreadPool/Queue/UAtomicPriorityQueue.h | 3 +- src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h | 3 +- src/UtilsCtrl/UAllocator.h | 8 +-- src/UtilsCtrl/UMemory.h | 73 ++++++++++++++++++++++ 6 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 src/UtilsCtrl/UMemory.h diff --git a/.gitignore b/.gitignore index 143ceb6..c9df15f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /cmake-build-release/ /.idea/ .DS_Store +build*/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 11d742d..ccabf6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ cmake_minimum_required(VERSION 3.2.5) project(CThreadPool VERSION 1.0.1) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 11) # add CThreadPool environment info include(cmake/CThreadPool-env-include.cmake) diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h index f9a328b..7dfaa82 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h @@ -12,6 +12,7 @@ #include #include "UQueueObject.h" +#include "../../UMemory.h" CGRAPH_NAMESPACE_BEGIN @@ -64,7 +65,7 @@ public: * @return */ CVoid push(T&& value, int priority) { - std::unique_ptr task(std::make_unique(std::move(value), priority)); + std::unique_ptr task(make_unique(std::move(value), priority)); CGRAPH_LOCK_GUARD lk(mutex_); priority_queue_.push(std::move(task)); } diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h index c5f696f..598ec70 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h @@ -15,6 +15,7 @@ #include #include "../UThreadPoolDefine.h" +#include "../../UMemory.h" #include "UQueueObject.h" CGRAPH_NAMESPACE_BEGIN @@ -104,7 +105,7 @@ public: * @param value */ CVoid push(T&& value) { - std::unique_ptr task(std::make_unique(std::move(value))); + std::unique_ptr task(make_unique(std::move(value))); CGRAPH_LOCK_GUARD lk(mutex_); queue_.push(std::move(task)); cv_.notify_one(); diff --git a/src/UtilsCtrl/UAllocator.h b/src/UtilsCtrl/UAllocator.h index 3ca20cf..0eaa99d 100644 --- a/src/UtilsCtrl/UAllocator.h +++ b/src/UtilsCtrl/UAllocator.h @@ -14,7 +14,7 @@ #endif #include -#include +#include "UMemory.h" CGRAPH_NAMESPACE_BEGIN @@ -31,7 +31,7 @@ public: * @return */ template::value, int> = 0> + enable_if_t::value, int> = 0> static T* safeMallocCObject() { T* ptr = nullptr; while (!ptr) { @@ -47,9 +47,9 @@ public: * @return */ template::value, int> = 0> + enable_if_t::value, int> = 0> static std::unique_ptr makeUniqueCObject() { - return std::make_unique(); + return make_unique(); } diff --git a/src/UtilsCtrl/UMemory.h b/src/UtilsCtrl/UMemory.h new file mode 100644 index 0000000..95c94ef --- /dev/null +++ b/src/UtilsCtrl/UMemory.h @@ -0,0 +1,73 @@ +/* + * @Author: chenjingyu + * @Date: 2022-10-11 01:43:58 + * @LastEditTime: 2022-10-11 01:59:50 + * @Description: Memory operator + * @FilePath: \CThreadPool\src\UtilsCtrl\UMemory.h + */ +#ifndef CGRAPH_UMEMORY_H +#define CGRAPH_UMEMORY_H + +#include +#include +#include "../CBasic/CBasicInclude.h" + +CGRAPH_NAMESPACE_BEGIN + +template +using enable_if_t = typename std::enable_if::type; + +template +std::unique_ptr WrapUnique(T* ptr) { + static_assert(!std::is_array::value, "array types are unsupported"); + static_assert(std::is_object::value, "non-object types are unsupported"); + return std::unique_ptr(ptr); +} + +namespace memory_internal { + +// Traits to select proper overload and return type for `absl::make_unique<>`. +template +struct MakeUniqueResult { + using scalar = std::unique_ptr; +}; +template +struct MakeUniqueResult { + using array = std::unique_ptr; +}; +template +struct MakeUniqueResult { + using invalid = void; +}; + +} // namespace memory_internal + +// gcc 4.8 has __cplusplus at 201301 but the libstdc++ shipped with it doesn't +// define make_unique. Other supported compilers either just define __cplusplus +// as 201103 but have make_unique (msvc), or have make_unique whenever +// __cplusplus > 201103 (clang). +#if (__cplusplus > 201103L || defined(_MSC_VER)) && \ + !(defined(__GLIBCXX__) && !defined(__cpp_lib_make_unique)) +using std::make_unique; +#else + +template +typename memory_internal::MakeUniqueResult::scalar make_unique( + Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename memory_internal::MakeUniqueResult::array make_unique(size_t n) { + return std::unique_ptr(new typename absl::remove_extent_t[n]()); +} + +template +typename memory_internal::MakeUniqueResult::invalid make_unique( + Args&&... /* args */) = delete; +#endif + +CGRAPH_NAMESPACE_END + + +#endif // CGRAPH_UMEMORY_H