range_ops.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <util/generic/typetraits.h>
  3. #include <new>
  4. namespace NRangeOps {
  5. template <class T, bool isTrivial>
  6. struct TRangeOpsBase {
  7. static inline void DestroyRange(T* b, T* e) noexcept {
  8. while (e > b) {
  9. (--e)->~T();
  10. }
  11. }
  12. static inline void InitializeRange(T* b, T* e) {
  13. T* c = b;
  14. try {
  15. for (; c < e; ++c) {
  16. new (c) T();
  17. }
  18. } catch (...) {
  19. DestroyRange(b, c);
  20. throw;
  21. }
  22. }
  23. };
  24. template <class T>
  25. struct TRangeOpsBase<T, true> {
  26. static inline void DestroyRange(T*, T*) noexcept {
  27. }
  28. static inline void InitializeRange(T*, T*) noexcept {
  29. }
  30. };
  31. template <class T>
  32. using TRangeOps = TRangeOpsBase<T, TTypeTraits<T>::IsPod>;
  33. template <class T>
  34. static inline void DestroyRange(T* b, T* e) noexcept {
  35. TRangeOps<T>::DestroyRange(b, e);
  36. }
  37. template <class T>
  38. static inline void InitializeRange(T* b, T* e) {
  39. TRangeOps<T>::InitializeRange(b, e);
  40. }
  41. }