11USERVER_NAMESPACE_BEGIN
45template <
class T, std::size_t Size, std::size_t Alignment,
bool Strict =
false>
46class FastPimpl final {
49 FastPimpl(FastPimpl&& v)
noexcept(
noexcept(T(std::declval<T>()))) : FastPimpl(std::move(*v)) {}
52 FastPimpl(
const FastPimpl& v)
noexcept(
noexcept(T(std::declval<
const T&>()))) : FastPimpl(*v) {}
55 FastPimpl& operator=(
const FastPimpl& rhs)
noexcept(
noexcept(std::declval<T&>() = std::declval<
const T&>())) {
60 FastPimpl& operator=(FastPimpl&& rhs)
noexcept(
62 noexcept(std::declval<T&>() = std::declval<T>())
64 *AsHeld() = std::move(*rhs);
68 template <
typename... Args>
70 explicit FastPimpl(Args&&... args)
noexcept(
noexcept(T(std::declval<Args>()...))) {
71 ::
new (AsHeld()) T(std::forward<Args>(args)...);
74 T* operator->()
noexcept {
return AsHeld(); }
76 const T* operator->()
const noexcept {
return AsHeld(); }
78 T& operator*()
noexcept {
return *AsHeld(); }
80 const T& operator*()
const noexcept {
return *AsHeld(); }
82 ~FastPimpl()
noexcept {
83 Validate<
sizeof(T),
alignof(T)>();
89 template <std::size_t ActualSize, std::size_t ActualAlignment>
90 static void Validate()
noexcept {
91 static_assert(Size >= ActualSize,
"invalid Size: Size >= sizeof(T) failed");
92 static_assert(!Strict || Size == ActualSize,
"invalid Size: Size == sizeof(T) failed");
94 static_assert(Alignment % ActualAlignment == 0,
"invalid Alignment: Alignment % alignof(T) == 0 failed");
95 static_assert(!Strict || Alignment == ActualAlignment,
"invalid Alignment: Alignment == alignof(T) failed");
98 alignas(Alignment) std::byte storage_[Size];
100 T* AsHeld()
noexcept {
return reinterpret_cast<T*>(&storage_); }
102 const T* AsHeld()
const noexcept {
return reinterpret_cast<
const T*>(&storage_); }