A Tcl like command language made for the Clinl kernel testing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
1.8 KiB

5 years ago
  1. #pragma once
  2. #include <stddef.h>
  3. #include "container.hpp"
  4. namespace __internals{
  5. template<typename T>
  6. T& ref(T& n)
  7. {
  8. return n;
  9. }
  10. template<class T>
  11. struct r_op_iterator
  12. {
  13. r_op_iterator(T* in):it(in){}
  14. T* it;
  15. auto operator++()
  16. {
  17. return it--;
  18. }
  19. auto operator++(int)
  20. {
  21. return --it;
  22. }
  23. auto operator--()
  24. {
  25. return it--;
  26. }
  27. auto operator--(int)
  28. {
  29. return --it;
  30. }
  31. T& operator*()
  32. {
  33. return *it;
  34. }
  35. T& operator[](const size_t idx)
  36. {
  37. return *(it-idx);
  38. }
  39. auto operator==(const r_op_iterator& rhs)
  40. {
  41. return it==rhs.it;
  42. }
  43. auto operator!=(const r_op_iterator& rhs)
  44. {
  45. return it!=rhs.it;
  46. }
  47. auto operator<=(const r_op_iterator& rhs)
  48. {
  49. return (rhs.it<=it);
  50. }
  51. auto operator>=(const r_op_iterator& rhs)
  52. {
  53. return (rhs.it>=it);
  54. }
  55. auto operator<(const r_op_iterator& rhs)
  56. {
  57. return (rhs.it<it);
  58. }
  59. auto operator>(const r_op_iterator& rhs)
  60. {
  61. return (rhs.it>it);
  62. }
  63. };
  64. }
  65. namespace ksdk{
  66. template<typename T>
  67. class buffer : public typed_container<T>
  68. {
  69. protected:
  70. T* _buffer;
  71. size_t _size;
  72. public:
  73. constexpr buffer(T* b, T* e)
  74. :_buffer(b)
  75. ,_size(e-b)
  76. {
  77. }
  78. constexpr buffer()
  79. :_buffer(nullptr)
  80. ,_size(0)
  81. {
  82. }
  83. constexpr buffer(T* b, size_t sz)
  84. :_buffer(b)
  85. ,_size(sz)
  86. {
  87. }
  88. size_t size()
  89. {
  90. return _size;
  91. }
  92. T* begin()
  93. {
  94. return _buffer;
  95. }
  96. T* end()
  97. {
  98. return _buffer+_size;
  99. }
  100. __internals::r_op_iterator<T> rbegin()
  101. {
  102. return __internals::r_op_iterator<T>(_buffer-1+_size);
  103. }
  104. __internals::r_op_iterator<T> rend()
  105. {
  106. return __internals::r_op_iterator<T>(_buffer-1);
  107. }
  108. T& operator[](size_t idx)
  109. {
  110. return _buffer[idx];
  111. }
  112. bool operator==(buffer<T> oth)
  113. {
  114. if(size()!=oth.size())
  115. return false;
  116. for(size_t i=0; i<size(); i++)
  117. if(_buffer[i]!=oth[i])
  118. return false;
  119. return true;
  120. }
  121. };
  122. }