accessors_impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #pragma once
  2. #include "memory_traits.h"
  3. namespace NAccessors {
  4. namespace NPrivate {
  5. template <typename Ta>
  6. struct TMemoryAccessorBase {
  7. enum {
  8. SimpleMemory = TMemoryTraits<Ta>::SimpleMemory,
  9. ContinuousMemory = TMemoryTraits<Ta>::ContinuousMemory,
  10. };
  11. struct TBadAccessor;
  12. };
  13. template <typename Ta>
  14. struct TBegin: public TMemoryAccessorBase<Ta> {
  15. using TElementType = typename TMemoryTraits<Ta>::TElementType;
  16. template <typename Tb>
  17. struct TNoMemoryIndirectionBegin {
  18. static const TElementType* Get(const Tb& b) {
  19. return (const TElementType*)&b;
  20. }
  21. };
  22. template <typename Tb>
  23. struct TIndirectMemoryRegionBegin {
  24. Y_HAS_MEMBER(Begin);
  25. Y_HAS_MEMBER(begin);
  26. template <typename Tc>
  27. struct TByBegin {
  28. static const TElementType* Get(const Tc& b) {
  29. return (const TElementType*)b.Begin();
  30. }
  31. };
  32. template <typename Tc>
  33. struct TBybegin {
  34. static const TElementType* Get(const Tc& b) {
  35. return (const TElementType*)b.begin();
  36. }
  37. };
  38. using TGet = std::conditional_t<THasBegin<Tb>::value, TByBegin<Tb>, TBybegin<Tb>>;
  39. static const TElementType* Get(const Tb& b) {
  40. return TGet::Get(b);
  41. }
  42. };
  43. using TGet = std::conditional_t<
  44. TMemoryAccessorBase<Ta>::SimpleMemory,
  45. TNoMemoryIndirectionBegin<Ta>,
  46. std::conditional_t<
  47. TMemoryAccessorBase<Ta>::ContinuousMemory,
  48. TIndirectMemoryRegionBegin<Ta>,
  49. typename TMemoryAccessorBase<Ta>::TBadAccessor>>;
  50. static const TElementType* Get(const Ta& b) {
  51. return TGet::Get(b);
  52. }
  53. };
  54. template <typename Ta>
  55. struct TEnd: public TMemoryAccessorBase<Ta> {
  56. using TElementType = typename TMemoryTraits<Ta>::TElementType;
  57. template <typename Tb>
  58. struct TNoMemoryIndirectionEnd {
  59. static const TElementType* Get(const Tb& b) {
  60. return (const TElementType*)(&b + 1);
  61. }
  62. };
  63. template <typename Tb>
  64. struct TIndirectMemoryRegionEnd {
  65. Y_HAS_MEMBER(End);
  66. Y_HAS_MEMBER(end);
  67. template <typename Tc>
  68. struct TByEnd {
  69. static const TElementType* Get(const Tc& b) {
  70. return (const TElementType*)b.End();
  71. }
  72. };
  73. template <typename Tc>
  74. struct TByend {
  75. static const TElementType* Get(const Tc& b) {
  76. return (const TElementType*)b.end();
  77. }
  78. };
  79. using TGet = std::conditional_t<THasEnd<Tb>::value, TByEnd<Tb>, TByend<Tb>>;
  80. static const TElementType* Get(const Tb& b) {
  81. return TGet::Get(b);
  82. }
  83. };
  84. using TGet = std::conditional_t<
  85. TMemoryAccessorBase<Ta>::SimpleMemory,
  86. TNoMemoryIndirectionEnd<Ta>,
  87. std::conditional_t<
  88. TMemoryAccessorBase<Ta>::ContinuousMemory,
  89. TIndirectMemoryRegionEnd<Ta>,
  90. typename TMemoryAccessorBase<Ta>::TBadAccessor>>;
  91. static const TElementType* Get(const Ta& b) {
  92. return TGet::Get(b);
  93. }
  94. };
  95. template <typename Ta, bool Init>
  96. struct TClear: public TMemoryAccessorBase<Ta> {
  97. template <typename Tb>
  98. struct TNoMemoryIndirectionClear {
  99. static void Do(Tb& b) {
  100. Zero(b);
  101. }
  102. };
  103. template <typename Tb>
  104. struct TIndirectMemoryRegionClear {
  105. Y_HAS_MEMBER(Clear);
  106. Y_HAS_MEMBER(clear);
  107. template <typename Tc>
  108. struct TByClear {
  109. static void Do(Tc& b) {
  110. b.Clear();
  111. }
  112. };
  113. template <typename Tc>
  114. struct TByclear {
  115. static void Do(Tc& b) {
  116. b.clear();
  117. }
  118. };
  119. template <typename Tc>
  120. struct TByNone {
  121. static void Do(Tc& b) {
  122. if (!Init)
  123. b = Tc();
  124. }
  125. };
  126. using TDo = std::conditional_t<
  127. THasClear<Tb>::value,
  128. TByClear<Tb>,
  129. std::conditional_t<
  130. THasclear<Tb>::value,
  131. TByclear<Tb>,
  132. TByNone<Tb>>>;
  133. static void Do(Tb& b) {
  134. TDo::Do(b);
  135. }
  136. };
  137. using TDo = std::conditional_t<TMemoryAccessorBase<Ta>::SimpleMemory, TNoMemoryIndirectionClear<Ta>, TIndirectMemoryRegionClear<Ta>>;
  138. static void Do(Ta& b) {
  139. TDo::Do(b);
  140. }
  141. };
  142. template <typename Tb>
  143. struct TReserve {
  144. Y_HAS_MEMBER(Reserve);
  145. Y_HAS_MEMBER(reserve);
  146. template <typename Tc>
  147. struct TByReserve {
  148. static void Do(Tc& b, size_t sz) {
  149. b.Reserve(sz);
  150. }
  151. };
  152. template <typename Tc>
  153. struct TByreserve {
  154. static void Do(Tc& b, size_t sz) {
  155. b.reserve(sz);
  156. }
  157. };
  158. template <typename Tc>
  159. struct TByNone {
  160. static void Do(Tc&, size_t) {
  161. }
  162. };
  163. using TDo = std::conditional_t<
  164. THasReserve<Tb>::value,
  165. TByReserve<Tb>,
  166. std::conditional_t<
  167. THasreserve<Tb>::value,
  168. TByreserve<Tb>,
  169. TByNone<Tb>>>;
  170. static void Do(Tb& b, size_t sz) {
  171. TDo::Do(b, sz);
  172. }
  173. };
  174. template <typename Tb>
  175. struct TResize {
  176. Y_HAS_MEMBER(Resize);
  177. Y_HAS_MEMBER(resize);
  178. template <typename Tc>
  179. struct TByResize {
  180. static void Do(Tc& b, size_t sz) {
  181. b.Resize(sz);
  182. }
  183. };
  184. template <typename Tc>
  185. struct TByresize {
  186. static void Do(Tc& b, size_t sz) {
  187. b.resize(sz);
  188. }
  189. };
  190. using TDo = std::conditional_t<THasResize<Tb>::value, TByResize<Tb>, TByresize<Tb>>;
  191. static void Do(Tb& b, size_t sz) {
  192. TDo::Do(b, sz);
  193. }
  194. };
  195. template <typename Tb>
  196. struct TAppend {
  197. Y_HAS_MEMBER(Append);
  198. Y_HAS_MEMBER(append);
  199. Y_HAS_MEMBER(push_back);
  200. template <typename Tc>
  201. struct TByAppend {
  202. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  203. static void Do(Tc& b, const TElementType& val) {
  204. b.Append(val);
  205. }
  206. };
  207. template <typename Tc>
  208. struct TByappend {
  209. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  210. static void Do(Tc& b, const TElementType& val) {
  211. b.append(val);
  212. }
  213. };
  214. template <typename Tc>
  215. struct TBypush_back {
  216. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  217. static void Do(Tc& b, const TElementType& val) {
  218. b.push_back(val);
  219. }
  220. };
  221. using TDo = std::conditional_t<
  222. THasAppend<Tb>::value,
  223. TByAppend<Tb>,
  224. std::conditional_t<
  225. THasappend<Tb>::value,
  226. TByappend<Tb>,
  227. TBypush_back<Tb>>>;
  228. using TElementType = typename TMemoryTraits<Tb>::TElementType;
  229. static void Do(Tb& b, const TElementType& val) {
  230. TDo::Do(b, val);
  231. }
  232. };
  233. template <typename Tb>
  234. struct TAppendRegion {
  235. Y_HAS_MEMBER(Append);
  236. Y_HAS_MEMBER(append);
  237. Y_HAS_MEMBER(insert);
  238. template <typename Tc>
  239. struct TByAppend {
  240. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  241. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  242. b.Append(beg, end);
  243. }
  244. };
  245. template <typename Tc>
  246. struct TByappend {
  247. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  248. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  249. b.append(beg, end);
  250. }
  251. };
  252. template <typename Tc>
  253. struct TByinsert {
  254. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  255. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  256. b.insert(b.end(), beg, end);
  257. }
  258. };
  259. template <typename Tc>
  260. struct TByNone {
  261. using TElementType = typename TMemoryTraits<Tc>::TElementType;
  262. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  263. for (const TElementType* it = beg; it != end; ++it)
  264. TAppend<Tc>::Do(b, *it);
  265. }
  266. };
  267. using TDo = std::conditional_t<
  268. THasAppend<Tb>::value,
  269. TByAppend<Tb>,
  270. std::conditional_t<
  271. THasappend<Tb>::value,
  272. TByappend<Tb>,
  273. std::conditional_t<
  274. THasinsert<Tb>::value,
  275. TByinsert<Tb>,
  276. TByNone<Tb>>>>;
  277. using TElementType = typename TMemoryTraits<Tb>::TElementType;
  278. static void Do(Tb& b, const TElementType* beg, const TElementType* end) {
  279. TDo::Do(b, beg, end);
  280. }
  281. };
  282. template <typename Ta>
  283. struct TAssign: public TMemoryAccessorBase<Ta> {
  284. using TElementType = typename TMemoryTraits<Ta>::TElementType;
  285. template <typename Tb>
  286. struct TNoMemoryIndirectionAssign {
  287. static void Do(Tb& b, const TElementType* beg, const TElementType* end) {
  288. if (sizeof(Tb) == sizeof(TElementType) && end - beg > 0) {
  289. memcpy(&b, beg, sizeof(Tb));
  290. } else if (end - beg > 0) {
  291. memcpy(&b, beg, Min<size_t>((end - beg) * sizeof(TElementType), sizeof(Tb)));
  292. } else {
  293. Zero(b);
  294. }
  295. }
  296. };
  297. template <typename Tb>
  298. struct TIndirectMemoryRegionAssign {
  299. Y_HAS_MEMBER(Assign);
  300. Y_HAS_MEMBER(assign);
  301. template <typename Tc>
  302. struct TByAssign {
  303. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  304. b.Assign(beg, end);
  305. }
  306. };
  307. template <typename Tc>
  308. struct TByassign {
  309. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  310. b.assign(beg, end);
  311. }
  312. };
  313. template <typename Tc>
  314. struct TByClearAppend {
  315. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  316. TClear<Tc, false>::Do(b);
  317. TAppendRegion<Tc>::Do(b, beg, end);
  318. }
  319. };
  320. template <typename Tc>
  321. struct TByConstruction {
  322. static void Do(Tc& b, const TElementType* beg, const TElementType* end) {
  323. b = Tc(beg, end);
  324. }
  325. };
  326. using TDo = std::conditional_t<
  327. THasAssign<Tb>::value,
  328. TByAssign<Tb>,
  329. std::conditional_t<
  330. THasassign<Tb>::value,
  331. TByassign<Tb>,
  332. std::conditional_t<
  333. TMemoryTraits<Tb>::OwnsMemory,
  334. TByClearAppend<Tb>,
  335. TByConstruction<Tb>>>>;
  336. static void Do(Tb& b, const TElementType* beg, const TElementType* end) {
  337. TDo::Do(b, beg, end);
  338. }
  339. };
  340. using TDo = std::conditional_t<TMemoryAccessorBase<Ta>::SimpleMemory, TNoMemoryIndirectionAssign<Ta>, TIndirectMemoryRegionAssign<Ta>>;
  341. static void Do(Ta& b, const TElementType* beg, const TElementType* end) {
  342. TDo::Do(b, beg, end);
  343. }
  344. };
  345. }
  346. }