General Purpose library for Freestanding C++ and POSIX systems
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.

93 lines
1.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <initializer_list>
  3. #include <gp/buffer.hpp>
  4. namespace gp{
  5. template<typename T, std::size_t sz>
  6. class array{
  7. T ary[sz];
  8. public:
  9. using associated_iterator = pointer_iterator<T>;
  10. using associated_const_iterator = pointer_iterator<T>;
  11. array()
  12. : ary()
  13. {}
  14. template<typename ...U>
  15. array(U&& ...v)
  16. : ary{gp::forward(v...)}
  17. {}
  18. template<>
  19. array<T[sz]>(T (&& oth)[sz]) {
  20. gp::move_uninitialized(
  21. gp::nameless_range(oth, oth+sz),
  22. gp::nameless_range(*this)
  23. );
  24. }
  25. constexpr T& operator[] (size_t off)
  26. {
  27. if constexpr (gp_config::has_buffer_bounds)
  28. {
  29. gp_config::assertion(
  30. off < sz,
  31. "Array bounds infringed"
  32. );
  33. }
  34. return ary[off];
  35. }
  36. constexpr const T& operator[] (size_t off) const
  37. {
  38. return ary[off];
  39. }
  40. constexpr size_t size() const
  41. {
  42. return sz;
  43. }
  44. constexpr associated_iterator begin()
  45. {
  46. return associated_iterator(&ary[0]);
  47. }
  48. constexpr associated_iterator end()
  49. {
  50. return associated_iterator(&ary[sz]);
  51. }
  52. constexpr associated_const_iterator cbegin() const
  53. {
  54. return ary;
  55. }
  56. constexpr associated_const_iterator cend() const
  57. {
  58. return ary+sz;
  59. }
  60. constexpr bool operator==(const array& oth) const
  61. {
  62. for(size_t idx = 0; idx<sz; idx++)
  63. {
  64. if(ary[idx] != oth.ary[idx])
  65. {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. constexpr bool operator!=(const array& oth) const
  72. {
  73. return !(*this == oth);
  74. }
  75. gp::buffer<T> as_buffer()
  76. {
  77. return gp::buffer<T>{(T*)ary, (T*)ary+sz};
  78. }
  79. };
  80. }