aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/erased_storage-inl.h
blob: 8b6a303dffdf54d85c330b0fa0f7522f514ba64d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef ERASED_STORAGE_INL_H_
#error "Direct inclusion of this file is not allowed, include erased_storage.h"
// For the sake of sane code completion.
#include "erased_storage.h"
#endif

#include <algorithm>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <size_t MaxByteSize>
template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
TErasedStorage<MaxByteSize>::TErasedStorage(TDecayedConcrete concrete) noexcept
{
    // NB(arkady-e1ppa): We want to be able to compare
    // erased objects as if they are not erased.
    // Assuming erased type's operator ==
    // is equivalent to bitwise comparison.
    std::ranges::fill(Bytes_, std::byte(0));
    std::construct_at(
        &AsConcrete<TDecayedConcrete>(),
        concrete);
}

template <size_t MaxByteSize>
template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
TDecayedConcrete& TErasedStorage<MaxByteSize>::AsConcrete() & noexcept
{
    using TPtr = TDecayedConcrete*;
    return *std::launder(reinterpret_cast<TPtr>(&Bytes_));
}

template <size_t MaxByteSize>
template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
const TDecayedConcrete& TErasedStorage<MaxByteSize>::AsConcrete() const & noexcept
{
    using TPtr = const TDecayedConcrete*;
    return *std::launder(reinterpret_cast<TPtr>(&Bytes_));
}

template <size_t MaxByteSize>
template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
TDecayedConcrete&& TErasedStorage<MaxByteSize>::AsConcrete() && noexcept
{
    using TPtr = TDecayedConcrete*;
    return std::move(*std::launder(reinterpret_cast<TPtr>(&Bytes_)));
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT