erased_storage-inl.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef ERASED_STORAGE_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include erased_storage.h"
  3. // For the sake of sane code completion.
  4. #include "erased_storage.h"
  5. #endif
  6. #include <algorithm>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <size_t MaxByteSize>
  10. template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
  11. TErasedStorage<MaxByteSize>::TErasedStorage(TDecayedConcrete concrete) noexcept
  12. {
  13. // NB(arkady-e1ppa): We want to be able to compare
  14. // erased objects as if they are not erased.
  15. // Assuming erased type's operator ==
  16. // is equivalent to bitwise comparison.
  17. std::ranges::fill(Bytes_, std::byte(0));
  18. std::construct_at(
  19. &AsConcrete<TDecayedConcrete>(),
  20. concrete);
  21. }
  22. template <size_t MaxByteSize>
  23. template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
  24. TDecayedConcrete& TErasedStorage<MaxByteSize>::AsConcrete() & noexcept
  25. {
  26. using TPtr = TDecayedConcrete*;
  27. return *std::launder(reinterpret_cast<TPtr>(&Bytes_));
  28. }
  29. template <size_t MaxByteSize>
  30. template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
  31. const TDecayedConcrete& TErasedStorage<MaxByteSize>::AsConcrete() const & noexcept
  32. {
  33. using TPtr = const TDecayedConcrete*;
  34. return *std::launder(reinterpret_cast<TPtr>(&Bytes_));
  35. }
  36. template <size_t MaxByteSize>
  37. template <CTriviallyErasable<MaxByteSize> TDecayedConcrete>
  38. TDecayedConcrete&& TErasedStorage<MaxByteSize>::AsConcrete() && noexcept
  39. {
  40. using TPtr = TDecayedConcrete*;
  41. return std::move(*std::launder(reinterpret_cast<TPtr>(&Bytes_)));
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////
  44. } // namespace NYT