accessors.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "accessors_impl.h"
  3. namespace NAccessors {
  4. /*
  5. * Adds API compatibility between different types representing memory regions.
  6. *
  7. * i.e. this will work:
  8. *
  9. * TString t;
  10. * const char* beg = NAccessors::Begin(t); // t.begin()
  11. * const char* end = NAccessors::End(t); // t.end()
  12. * size_t sz = NAccessors::Size(t); // t.size()
  13. *
  14. * as well as this:
  15. *
  16. * ui64 t;
  17. * const ui64* beg = NAccessors::Begin(t); // &t
  18. * const ui64* end = NAccessors::End(t); // &t + 1
  19. * size_t sz = NAccessors::Size(t); // 1
  20. *
  21. * Both will give you begin, end and size of the underlying memory region.
  22. */
  23. template <typename T>
  24. inline const typename TMemoryTraits<T>::TElementType* Begin(const T& t) {
  25. return NPrivate::TBegin<T>::Get(t);
  26. }
  27. template <typename T>
  28. inline const typename TMemoryTraits<T>::TElementType* End(const T& t) {
  29. return NPrivate::TEnd<T>::Get(t);
  30. }
  31. template <typename T>
  32. inline size_t Size(const T& t) {
  33. return End(t) - Begin(t);
  34. }
  35. /**
  36. * This gives some unification in terms of memory manipulation.
  37. */
  38. template <typename T>
  39. inline void Reserve(T& t, size_t sz) {
  40. NPrivate::TReserve<T>::Do(t, sz);
  41. }
  42. template <typename T>
  43. inline void Resize(T& t, size_t sz) {
  44. NPrivate::TResize<T>::Do(t, sz);
  45. }
  46. template <typename T>
  47. inline void Clear(T& t) {
  48. NPrivate::TClear<T, false>::Do(t);
  49. }
  50. template <typename T>
  51. inline void Init(T& t) {
  52. NPrivate::TClear<T, true>::Do(t);
  53. }
  54. template <typename T>
  55. inline void Append(T& t, const typename TMemoryTraits<T>::TElementType& v) {
  56. NPrivate::TAppend<T>::Do(t, v);
  57. }
  58. template <typename T>
  59. inline void Append(T& t,
  60. const typename TMemoryTraits<T>::TElementType* beg,
  61. const typename TMemoryTraits<T>::TElementType* end) {
  62. NPrivate::TAppendRegion<T>::Do(t, beg, end);
  63. }
  64. template <typename T>
  65. inline void Assign(T& t,
  66. const typename TMemoryTraits<T>::TElementType* beg,
  67. const typename TMemoryTraits<T>::TElementType* end) {
  68. NPrivate::TAssign<T>::Do(t, beg, end);
  69. }
  70. }