|
| 1 | +#ifndef MIR_RCARRAY |
| 2 | + |
| 3 | +#define MIR_RCARRAY |
| 4 | + |
| 5 | +#include <cassert> |
| 6 | +#include <stdexcept> |
| 7 | +#include "mir/ndslice.h" |
| 8 | + |
| 9 | +template <typename T> |
| 10 | +struct mir_rci; |
| 11 | + |
| 12 | +template <typename T> |
| 13 | +struct mir_rcarray |
| 14 | +{ |
| 15 | +private: |
| 16 | + |
| 17 | + void* _context; |
| 18 | + |
| 19 | +public: |
| 20 | + |
| 21 | + ~mir_rcarray(); |
| 22 | + mir_rcarray(mir_rcarray& rhs); |
| 23 | + bool initialize(size_t length, unsigned int alignment, bool deallocate, bool initialize); |
| 24 | + |
| 25 | + mir_slice<mir_rci<T>> asSlice(); |
| 26 | + |
| 27 | + inline mir_rcarray(size_t length, unsigned int alignment = alignof(T), bool deallocate = true, bool initialize = true) |
| 28 | + { |
| 29 | + if (!this->initialize(length, alignment, deallocate, initialize)) |
| 30 | + { |
| 31 | + throw std::runtime_error("mir_rcarray: out of memory arror."); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + inline size_t size() noexcept |
| 36 | + { |
| 37 | + return _context ? *(size_t*)((char*)_context + sizeof(void*)) : 0; |
| 38 | + } |
| 39 | + |
| 40 | + inline T& at(size_t index) |
| 41 | + { |
| 42 | + assert(index < this->size()); |
| 43 | + return ((T*)((char*)_context + sizeof(void*) * 4))[index]; |
| 44 | + } |
| 45 | + |
| 46 | + inline const T& at(size_t index) const |
| 47 | + { |
| 48 | + assert(index < this->size()); |
| 49 | + return ((const T*)((char*)_context + sizeof(void*) * 4))[index]; |
| 50 | + } |
| 51 | + |
| 52 | + inline T& operator[](size_t index) |
| 53 | + { |
| 54 | + assert(index < this->size()); |
| 55 | + return ((T*)((char*)_context + sizeof(void*) * 4))[index]; |
| 56 | + } |
| 57 | + |
| 58 | + inline const T& operator[](size_t index) const |
| 59 | + { |
| 60 | + assert(index < this->size()); |
| 61 | + return ((const T*)((char*)_context + sizeof(void*) * 4))[index]; |
| 62 | + } |
| 63 | + |
| 64 | + inline T* data() noexcept |
| 65 | + { |
| 66 | + return _context ? (T*)((char*)_context + sizeof(void*) * 4) : NULL; |
| 67 | + } |
| 68 | + |
| 69 | + inline T* begin() noexcept |
| 70 | + { |
| 71 | + return _context ? (T*)((char*)_context + sizeof(void*) * 4) : NULL; |
| 72 | + } |
| 73 | + |
| 74 | + inline const T* cbegin() const noexcept |
| 75 | + { |
| 76 | + return _context ? (const T*)((char*)_context + sizeof(void*) * 4) : NULL; |
| 77 | + } |
| 78 | + |
| 79 | + inline T* end() noexcept |
| 80 | + { |
| 81 | + return this->begin() + this->size(); |
| 82 | + } |
| 83 | + |
| 84 | + inline const T* cend() const noexcept |
| 85 | + { |
| 86 | + return this->cbegin() + this->size(); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | + |
| 91 | +template <typename T> |
| 92 | +struct mir_rci |
| 93 | +{ |
| 94 | +public: |
| 95 | + |
| 96 | + T* _iterator; |
| 97 | + mir_rcarray<T> _array; |
| 98 | + |
| 99 | + ~mir_rci() = default; |
| 100 | + mir_rci(mir_rci& rhs) = default; |
| 101 | +}; |
| 102 | + |
| 103 | +#endif |
0 commit comments