2 Copyright (C) 2012 fmaj7b5.info
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef FM7b5_MDVIEW_CUH
19 #define FM7b5_MDVIEW_CUH
22 #include <type_traits>
28 //! copy const qualifier of From to To
29 template <typename From, typename To> struct copy_const { typedef To type; };
30 template <typename From, typename To> struct copy_const<const From, To> { typedef typename std::add_const<To>::type type; };
32 //! array subscript access for multi dimentional data
33 template <typename T, size_t D>
38 typedef typename copy_const<T, uint_fast8_t>::type* byte_pointer_type;
39 typedef typename copy_const<T, void>::type* void_pointer_type;
42 //! temporal class for resolving the operator[]
43 template <typename T, size_t D>
46 byte_pointer_type const m_p;
47 const size_t* const m_extent;
50 typedef ref<T, D-1> result_type;
53 ref(void_pointer_type p, const size_t* extent)
54 : m_p(static_cast<byte_pointer_type>(p)), m_extent(extent)
58 result_type operator[](size_t i) const
60 return result_type(m_p + *m_extent * i, m_extent + 1);
67 byte_pointer_type const m_p;
68 const size_t* const m_extent;
71 typedef typename std::add_reference<T>::type result_type;
74 ref(void_pointer_type p, const size_t* extent)
75 : m_p(static_cast<byte_pointer_type>(p)), m_extent(extent)
79 result_type operator[](size_t i) const
81 return *reinterpret_cast<T*>(m_p + *m_extent * i);
85 byte_pointer_type m_data;
91 MDView(void_pointer_type p, const size_t extent[D])
92 : m_data(static_cast<byte_pointer_type>(p))
94 for (size_t i = 0; i < D; ++i) {
95 m_extent[i] = extent[i];
100 typename ref<T, D>::result_type operator[](size_t i) const
102 ref<T, D> r(m_data, m_extent);
108 using detail::MDView;
111 #endif /* FM7b5_MDVIEW_CUH */