memory_traits.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include <util/generic/array_ref.h>
  3. #include <util/memory/blob.h>
  4. #include <util/memory/tempbuf.h>
  5. #include <util/generic/buffer.h>
  6. #include <util/generic/strbuf.h>
  7. #include <util/generic/string.h>
  8. #include <util/generic/vector.h>
  9. #include <util/generic/typetraits.h>
  10. #include <array>
  11. #include <string>
  12. #include <utility>
  13. template <typename T>
  14. struct TMemoryTraits {
  15. enum {
  16. SimpleMemory = std::is_arithmetic<T>::value,
  17. ContinuousMemory = SimpleMemory,
  18. OwnsMemory = SimpleMemory,
  19. };
  20. using TElementType = T;
  21. };
  22. template <typename T, size_t n>
  23. struct TMemoryTraits<T[n]> {
  24. enum {
  25. SimpleMemory = TMemoryTraits<T>::SimpleMemory,
  26. ContinuousMemory = SimpleMemory,
  27. OwnsMemory = SimpleMemory,
  28. };
  29. using TElementType = T;
  30. };
  31. template <typename T, size_t n>
  32. struct TMemoryTraits<std::array<T, n>> {
  33. enum {
  34. SimpleMemory = TMemoryTraits<T>::SimpleMemory,
  35. ContinuousMemory = SimpleMemory,
  36. OwnsMemory = SimpleMemory,
  37. };
  38. using TElementType = T;
  39. };
  40. template <typename A, typename B>
  41. struct TMemoryTraits<std::pair<A, B>> {
  42. enum {
  43. SimpleMemory = TMemoryTraits<A>::SimpleMemory && TMemoryTraits<B>::SimpleMemory,
  44. ContinuousMemory = SimpleMemory,
  45. OwnsMemory = SimpleMemory,
  46. };
  47. using TElementType = std::pair<A, B>;
  48. };
  49. template <>
  50. struct TMemoryTraits<TBuffer> {
  51. enum {
  52. SimpleMemory = false,
  53. ContinuousMemory = true,
  54. OwnsMemory = true,
  55. };
  56. using TElementType = char;
  57. };
  58. template <>
  59. struct TMemoryTraits<TTempBuf> {
  60. enum {
  61. SimpleMemory = false,
  62. ContinuousMemory = true,
  63. OwnsMemory = true,
  64. };
  65. using TElementType = char;
  66. };
  67. template <>
  68. struct TMemoryTraits< ::TBlob> {
  69. enum {
  70. SimpleMemory = false,
  71. ContinuousMemory = true,
  72. OwnsMemory = true,
  73. };
  74. using TElementType = char;
  75. };
  76. template <typename T>
  77. struct TElementDependentMemoryTraits {
  78. enum {
  79. SimpleMemory = false,
  80. ContinuousMemory = TMemoryTraits<T>::SimpleMemory,
  81. };
  82. using TElementType = T;
  83. };
  84. template <typename T, typename TAlloc>
  85. struct TMemoryTraits<std::vector<T, TAlloc>>: public TElementDependentMemoryTraits<T> {
  86. enum {
  87. OwnsMemory = TMemoryTraits<T>::OwnsMemory
  88. };
  89. };
  90. template <typename T, typename TAlloc>
  91. struct TMemoryTraits<TVector<T, TAlloc>>: public TMemoryTraits<std::vector<T, TAlloc>> {
  92. };
  93. template <typename T>
  94. struct TMemoryTraits<TTempArray<T>>: public TElementDependentMemoryTraits<T> {
  95. enum {
  96. OwnsMemory = TMemoryTraits<T>::OwnsMemory
  97. };
  98. };
  99. template <typename T, typename TCharTraits, typename TAlloc>
  100. struct TMemoryTraits<std::basic_string<T, TCharTraits, TAlloc>>: public TElementDependentMemoryTraits<T> {
  101. enum {
  102. OwnsMemory = TMemoryTraits<T>::OwnsMemory
  103. };
  104. };
  105. template <>
  106. struct TMemoryTraits<TString>: public TElementDependentMemoryTraits<char> {
  107. enum {
  108. OwnsMemory = true
  109. };
  110. };
  111. template <>
  112. struct TMemoryTraits<TUtf16String>: public TElementDependentMemoryTraits<wchar16> {
  113. enum {
  114. OwnsMemory = true
  115. };
  116. };
  117. template <typename T>
  118. struct TMemoryTraits<TArrayRef<T>>: public TElementDependentMemoryTraits<T> {
  119. enum {
  120. OwnsMemory = false
  121. };
  122. };
  123. template <typename TCharType, typename TCharTraits>
  124. struct TMemoryTraits<TBasicStringBuf<TCharType, TCharTraits>>: public TElementDependentMemoryTraits<TCharType> {
  125. enum {
  126. OwnsMemory = false
  127. };
  128. };
  129. template <>
  130. struct TMemoryTraits<TStringBuf>: public TElementDependentMemoryTraits<char> {
  131. enum {
  132. OwnsMemory = false
  133. };
  134. };
  135. template <>
  136. struct TMemoryTraits<TWtringBuf>: public TElementDependentMemoryTraits<wchar16> {
  137. enum {
  138. OwnsMemory = false
  139. };
  140. };