uv.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <vector>
  3. #include <glm/glm.hpp>
  4. namespace gb
  5. {
  6. template <typename vector_type>
  7. class uv;
  8. template <typename vector_type>
  9. class uvm;
  10. // Unified Vector Manager
  11. template <typename vector_type>
  12. class uvm
  13. {
  14. private:
  15. static std::vector<vector_type> m_arr;
  16. public:
  17. using uv_type = uv<vector_type>;
  18. using index_type = typename uv_type::index_type;
  19. static vector_type& query(index_type i)
  20. {
  21. return m_arr[i];
  22. }
  23. static index_type make(const vector_type& val)
  24. {
  25. m_arr.push_back(val);
  26. return m_arr.size()-1;
  27. }
  28. static index_type make(vector_type&& val)
  29. {
  30. m_arr.push_back(std::move(val));
  31. return m_arr.size()-1;
  32. }
  33. static void free(index_type i)
  34. {
  35. // TODO
  36. }
  37. };
  38. template <typename vector_type>
  39. ::std::vector<vector_type> uvm<vector_type>::m_arr {};
  40. template <typename vector_type>
  41. class uv
  42. {
  43. public:
  44. using index_type = size_t;
  45. using uvm_type = uvm<vector_type>;
  46. public:
  47. index_type i;
  48. public:
  49. // copies the index only
  50. explicit uv(const uv& v)
  51. {
  52. i = v.i;
  53. }
  54. explicit uv(uv&& v)
  55. {
  56. i = v.i;
  57. }
  58. // make new vector
  59. static uv make(const vector_type& val)
  60. {
  61. return uv(uvm_type::make(val));
  62. }
  63. static uv make(vector_type&& val)
  64. {
  65. return uv(uvm_type::make(std::move(val)));
  66. }
  67. // copies the index only
  68. uv& operator=(const uv& v)
  69. {
  70. i = v.i;
  71. }
  72. uv& operator=(uv&& v)
  73. {
  74. i = v.i;
  75. }
  76. vector_type& operator*(void)
  77. {
  78. return uvm_type::query(i);
  79. }
  80. const vector_type& operator*(void) const
  81. {
  82. return uvm_type::query(i);
  83. }
  84. vector_type* operator->(void)
  85. {
  86. return &**this;
  87. }
  88. const vector_type* operator->(void) const
  89. {
  90. return &**this;
  91. }
  92. private:
  93. explicit uv(index_type _i)
  94. : i(_i)
  95. {
  96. }
  97. };
  98. using uv4 = uv<glm::vec4>;
  99. using uv2 = uv<glm::vec2>;
  100. } // namespace gb