From ad9818a89b22ce6580fc192f3642489b421348b5 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Thu, 4 Apr 2019 23:25:51 +0800 Subject: [PATCH] move concept & pimpl helpers to single header respectively --- include/def.h | 62 -------------------------------------- src/buffer.cpp | 1 + src/concept.h | 12 ++++++++ src/pimpl.h | 66 +++++++++++++++++++++++++++++++++++++++++ src/platform/to_tchar.h | 2 +- src/shm.cpp | 2 +- src/waiter.cpp | 2 +- 7 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 src/concept.h create mode 100644 src/pimpl.h diff --git a/include/def.h b/include/def.h index 47fc9bf..3f438eb 100644 --- a/include/def.h +++ b/include/def.h @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -47,65 +46,4 @@ enum class trans { // transmission template struct wr {}; -// concept helpers - -template -using Requires = std::enable_if_t; - -// pimpl small object optimization helpers - -template -using IsImplComfortable = Requires<(sizeof(T) <= sizeof(T*)), R>; - -template -using IsImplUncomfortable = Requires<(sizeof(T) > sizeof(T*)), R>; - -template -constexpr auto make_impl(P&&... params) -> IsImplComfortable { - T* buf {}; - ::new (&buf) T { std::forward

(params)... }; - return buf; -} - -template -constexpr auto impl(T* const (& p)) -> IsImplComfortable { - return reinterpret_cast(&const_cast(reinterpret_cast(p))); -} - -template -constexpr auto clear_impl(T* p) -> IsImplComfortable { - if (p != nullptr) impl(p)->~T(); -} - -template -constexpr auto make_impl(P&&... params) -> IsImplUncomfortable { - return new T { std::forward

(params)... }; -} - -template -constexpr auto clear_impl(T* p) -> IsImplUncomfortable { - delete p; -} - -template -constexpr auto impl(T* const (& p)) -> IsImplUncomfortable { - return p; -} - -template -struct pimpl { - template - constexpr static T* make(P&&... params) { - return make_impl(std::forward

(params)...); - } - -#if __cplusplus >= 201703L - constexpr void clear() { -#else /*__cplusplus < 201703L*/ - void clear() { -#endif/*__cplusplus < 201703L*/ - clear_impl(static_cast(const_cast(this))); - } -}; - } // namespace ipc diff --git a/src/buffer.cpp b/src/buffer.cpp index 098050c..ad077bf 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -1,4 +1,5 @@ #include "buffer.h" +#include "pimpl.h" #include diff --git a/src/concept.h b/src/concept.h new file mode 100644 index 0000000..da29f09 --- /dev/null +++ b/src/concept.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace ipc { + +// concept helpers + +template +using Requires = std::enable_if_t; + +} // namespace ipc diff --git a/src/pimpl.h b/src/pimpl.h new file mode 100644 index 0000000..9e7d228 --- /dev/null +++ b/src/pimpl.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include + +#include "concept.h" + +namespace ipc { + +// pimpl small object optimization helpers + +template +using IsImplComfortable = Requires<(sizeof(T) <= sizeof(T*)), R>; + +template +using IsImplUncomfortable = Requires<(sizeof(T) > sizeof(T*)), R>; + +template +constexpr auto make_impl(P&&... params) -> IsImplComfortable { + T* buf {}; + ::new (&buf) T { std::forward

(params)... }; + return buf; +} + +template +constexpr auto impl(T* const (& p)) -> IsImplComfortable { + return reinterpret_cast(&const_cast(reinterpret_cast(p))); +} + +template +constexpr auto clear_impl(T* p) -> IsImplComfortable { + if (p != nullptr) impl(p)->~T(); +} + +template +constexpr auto make_impl(P&&... params) -> IsImplUncomfortable { + return new T { std::forward

(params)... }; +} + +template +constexpr auto clear_impl(T* p) -> IsImplUncomfortable { + delete p; +} + +template +constexpr auto impl(T* const (& p)) -> IsImplUncomfortable { + return p; +} + +template +struct pimpl { + template + constexpr static T* make(P&&... params) { + return make_impl(std::forward

(params)...); + } + +#if __cplusplus >= 201703L + constexpr void clear() { +#else /*__cplusplus < 201703L*/ + void clear() { +#endif/*__cplusplus < 201703L*/ + clear_impl(static_cast(const_cast(this))); + } +}; + +} // namespace ipc diff --git a/src/platform/to_tchar.h b/src/platform/to_tchar.h index 72a6068..3903e6f 100644 --- a/src/platform/to_tchar.h +++ b/src/platform/to_tchar.h @@ -9,7 +9,7 @@ #include #include -#include "def.h" +#include "concept.h" namespace ipc::detail { diff --git a/src/shm.cpp b/src/shm.cpp index a34c9cf..f8cd2a1 100644 --- a/src/shm.cpp +++ b/src/shm.cpp @@ -3,7 +3,7 @@ #include #include -#include "def.h" +#include "pimpl.h" namespace ipc { namespace shm { diff --git a/src/waiter.cpp b/src/waiter.cpp index 54e8840..b3925f0 100644 --- a/src/waiter.cpp +++ b/src/waiter.cpp @@ -2,7 +2,7 @@ #include -#include "def.h" +#include "pimpl.h" #include "platform/waiter_wrapper.h" #undef IPC_PP_CAT_