mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
move concept & pimpl helpers to single header respectively
This commit is contained in:
parent
2079c8eafb
commit
ad9818a89b
@ -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
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "buffer.h"
|
||||
#include "pimpl.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
|
||||
12
src/concept.h
Normal file
12
src/concept.h
Normal 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
66
src/pimpl.h
Normal 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
|
||||
@ -9,7 +9,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include "def.h"
|
||||
#include "concept.h"
|
||||
|
||||
namespace ipc::detail {
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "def.h"
|
||||
#include "pimpl.h"
|
||||
|
||||
namespace ipc {
|
||||
namespace shm {
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "def.h"
|
||||
#include "pimpl.h"
|
||||
#include "platform/waiter_wrapper.h"
|
||||
|
||||
#undef IPC_PP_CAT_
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user