|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) 2016, Johan Mabille and Sylvain Corlay * |
| 3 | +* * |
| 4 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 5 | +* * |
| 6 | +* The full license is in the file LICENSE, distributed with this software. * |
| 7 | +****************************************************************************/ |
| 8 | + |
| 9 | +#ifndef PYBUFFER_ADAPTOR_HPP |
| 10 | +#define PYBUFFER_ADAPTOR_HPP |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | + |
| 14 | +namespace xt |
| 15 | +{ |
| 16 | + |
| 17 | + template <class T> |
| 18 | + class pybuffer_adaptor |
| 19 | + { |
| 20 | + |
| 21 | + public: |
| 22 | + |
| 23 | + using value_type = T; |
| 24 | + using reference = T&; |
| 25 | + using const_reference = const T&; |
| 26 | + using pointer = T*; |
| 27 | + using const_pointer = const T*; |
| 28 | + using size_type = std::size_t; |
| 29 | + using difference_type = std::ptrdiff_t; |
| 30 | + |
| 31 | + using iterator = pointer; |
| 32 | + using const_iterator = const_pointer; |
| 33 | + |
| 34 | + pybuffer_adaptor(pointer data, size_type size); |
| 35 | + |
| 36 | + bool empty() const noexcept; |
| 37 | + size_type size() const noexcept; |
| 38 | + |
| 39 | + reference operator[](size_type i); |
| 40 | + const_reference operator[](size_type i) const; |
| 41 | + |
| 42 | + reference front(); |
| 43 | + const_reference front() const; |
| 44 | + |
| 45 | + reference back(); |
| 46 | + const_reference back() const; |
| 47 | + |
| 48 | + iterator begin(); |
| 49 | + iterator end(); |
| 50 | + |
| 51 | + const_iterator begin() const; |
| 52 | + const_iterator end() const; |
| 53 | + const_iterator cbegin() const; |
| 54 | + const_iterator cend() const; |
| 55 | + |
| 56 | + private: |
| 57 | + |
| 58 | + pointer p_data; |
| 59 | + size_type m_size; |
| 60 | + }; |
| 61 | + |
| 62 | + /*********************************** |
| 63 | + * pybuffer_adaptor implementation * |
| 64 | + ***********************************/ |
| 65 | + |
| 66 | + template <class T> |
| 67 | + inline pybuffer_adaptor<T>::pybuffer_adaptor(pointer data, size_type size) |
| 68 | + : p_data(data), m_size(size) |
| 69 | + { |
| 70 | + } |
| 71 | + |
| 72 | + template <class T> |
| 73 | + inline bool pybuffer_adapgtor<T>::empty() const noexcept |
| 74 | + { |
| 75 | + return m_size == 0; |
| 76 | + } |
| 77 | + |
| 78 | + template <class T> |
| 79 | + inline auto pybuffer_adaptor<T>::size() const noexcept |
| 80 | + { |
| 81 | + return m_size; |
| 82 | + } |
| 83 | + |
| 84 | + template <class T> |
| 85 | + inline auto pybuffer_adaptor<T>::operator[](size_type i) -> reference |
| 86 | + { |
| 87 | + return p_data[i]; |
| 88 | + } |
| 89 | + |
| 90 | + template <class T> |
| 91 | + inline auto pybuffer_adaptor<T>::operator[](size_type i) -> const_reference |
| 92 | + { |
| 93 | + return p_data[i]; |
| 94 | + } |
| 95 | + |
| 96 | + template <class T> |
| 97 | + inline auto pybuffer_adaptor<T>::front() -> reference |
| 98 | + { |
| 99 | + return p_data[0]; |
| 100 | + } |
| 101 | + |
| 102 | + template <class T> |
| 103 | + inline auto pybuffer_adaptor<T>::front() const -> const_reference |
| 104 | + { |
| 105 | + return p_data[0]; |
| 106 | + } |
| 107 | + |
| 108 | + template <class T> |
| 109 | + inline auto pybuffer_adaptor<T>::back() -> reference |
| 110 | + { |
| 111 | + return p_data[m_size - 1]; |
| 112 | + } |
| 113 | + |
| 114 | + template <class T> |
| 115 | + inline auto pybuffer_adaptor<T>::back() const -> const_reference |
| 116 | + { |
| 117 | + return p_data[m_size - 1]; |
| 118 | + } |
| 119 | + |
| 120 | + template <class T> |
| 121 | + inline auto pybuffer_adaptor<T>::begin() -> iterator |
| 122 | + { |
| 123 | + return p_data; |
| 124 | + } |
| 125 | + |
| 126 | + template <class T> |
| 127 | + inline auto pybuffer_adaptor<T>::end() -> iterator |
| 128 | + { |
| 129 | + return p_data + m_size; |
| 130 | + } |
| 131 | + |
| 132 | + template <class T> |
| 133 | + inline auto pybuffer_adaptor<T>::begin() const -> const_iterator |
| 134 | + { |
| 135 | + return const_iterator(p_data); |
| 136 | + } |
| 137 | + |
| 138 | + template <class T> |
| 139 | + inline auto pybuffer_adaptor<T>::end() const -> const_iterator |
| 140 | + { |
| 141 | + return const_iterator(p_data + m_size); |
| 142 | + } |
| 143 | + |
| 144 | + template <class T> |
| 145 | + inline auto pybuffer_adaptor<T>::cbegin() const -> const_iterator |
| 146 | + { |
| 147 | + return begin(); |
| 148 | + } |
| 149 | + |
| 150 | + template <class T> |
| 151 | + inline auto pybuffer_adaptor<T>::cend() const -> const_iterator |
| 152 | + { |
| 153 | + return end(); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#endif |
| 158 | + |
0 commit comments