move concept & pimpl helpers to single header respectively

This commit is contained in:
mutouyun 2019-04-04 23:25:51 +08:00
parent 2079c8eafb
commit ad9818a89b
7 changed files with 82 additions and 65 deletions

View File

@ -3,7 +3,6 @@
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <new>
#include <utility>
@ -47,65 +46,4 @@ enum class trans { // transmission
template <relat Rp, relat Rc, trans Ts>
struct wr {};
// concept helpers
template <bool Cond, typename R>
using Requires = std::enable_if_t<Cond, R>;
// pimpl small object optimization helpers
template <typename T, typename R = T*>
using IsImplComfortable = Requires<(sizeof(T) <= sizeof(T*)), R>;
template <typename T, typename R = T*>
using IsImplUncomfortable = Requires<(sizeof(T) > sizeof(T*)), R>;
template <typename T, typename... P>
constexpr auto make_impl(P&&... params) -> IsImplComfortable<T> {
T* buf {};
::new (&buf) T { std::forward<P>(params)... };
return buf;
}
template <typename T>
constexpr auto impl(T* const (& p)) -> IsImplComfortable<T> {
return reinterpret_cast<T*>(&const_cast<char &>(reinterpret_cast<char const &>(p)));
}
template <typename T>
constexpr auto clear_impl(T* p) -> IsImplComfortable<T, void> {
if (p != nullptr) impl(p)->~T();
}
template <typename T, typename... P>
constexpr auto make_impl(P&&... params) -> IsImplUncomfortable<T> {
return new T { std::forward<P>(params)... };
}
template <typename T>
constexpr auto clear_impl(T* p) -> IsImplUncomfortable<T, void> {
delete p;
}
template <typename T>
constexpr auto impl(T* const (& p)) -> IsImplUncomfortable<T> {
return p;
}
template <typename T>
struct pimpl {
template <typename... P>
constexpr static T* make(P&&... params) {
return make_impl<T>(std::forward<P>(params)...);
}
#if __cplusplus >= 201703L
constexpr void clear() {
#else /*__cplusplus < 201703L*/
void clear() {
#endif/*__cplusplus < 201703L*/
clear_impl(static_cast<T*>(const_cast<pimpl*>(this)));
}
};
} // namespace ipc

View File

@ -1,4 +1,5 @@
#include "buffer.h"
#include "pimpl.h"
#include <cstring>

12
src/concept.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <type_traits>
namespace ipc {
// concept helpers
template <bool Cond, typename R>
using Requires = std::enable_if_t<Cond, R>;
} // namespace ipc

66
src/pimpl.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include <new>
#include <utility>
#include "concept.h"
namespace ipc {
// pimpl small object optimization helpers
template <typename T, typename R = T*>
using IsImplComfortable = Requires<(sizeof(T) <= sizeof(T*)), R>;
template <typename T, typename R = T*>
using IsImplUncomfortable = Requires<(sizeof(T) > sizeof(T*)), R>;
template <typename T, typename... P>
constexpr auto make_impl(P&&... params) -> IsImplComfortable<T> {
T* buf {};
::new (&buf) T { std::forward<P>(params)... };
return buf;
}
template <typename T>
constexpr auto impl(T* const (& p)) -> IsImplComfortable<T> {
return reinterpret_cast<T*>(&const_cast<char &>(reinterpret_cast<char const &>(p)));
}
template <typename T>
constexpr auto clear_impl(T* p) -> IsImplComfortable<T, void> {
if (p != nullptr) impl(p)->~T();
}
template <typename T, typename... P>
constexpr auto make_impl(P&&... params) -> IsImplUncomfortable<T> {
return new T { std::forward<P>(params)... };
}
template <typename T>
constexpr auto clear_impl(T* p) -> IsImplUncomfortable<T, void> {
delete p;
}
template <typename T>
constexpr auto impl(T* const (& p)) -> IsImplUncomfortable<T> {
return p;
}
template <typename T>
struct pimpl {
template <typename... P>
constexpr static T* make(P&&... params) {
return make_impl<T>(std::forward<P>(params)...);
}
#if __cplusplus >= 201703L
constexpr void clear() {
#else /*__cplusplus < 201703L*/
void clear() {
#endif/*__cplusplus < 201703L*/
clear_impl(static_cast<T*>(const_cast<pimpl*>(this)));
}
};
} // namespace ipc

View File

@ -9,7 +9,7 @@
#include <algorithm>
#include <cstring>
#include "def.h"
#include "concept.h"
namespace ipc::detail {

View File

@ -3,7 +3,7 @@
#include <string>
#include <utility>
#include "def.h"
#include "pimpl.h"
namespace ipc {
namespace shm {

View File

@ -2,7 +2,7 @@
#include <string>
#include "def.h"
#include "pimpl.h"
#include "platform/waiter_wrapper.h"
#undef IPC_PP_CAT_