1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP
17 #include <boost/iterator/iterator_facade.hpp>
19 #include <boost/compute/buffer.hpp>
20 #include <boost/compute/iterator/buffer_iterator.hpp>
21 #include <boost/compute/type_traits/is_device_iterator.hpp>
26 // forward declaration for constant_buffer_iterator<T>
27 template<class T> class constant_buffer_iterator;
31 // helper class which defines the iterator_facade super-class
32 // type for constant_buffer_iterator<T>
34 class constant_buffer_iterator_base
37 typedef ::boost::iterator_facade<
38 ::boost::compute::constant_buffer_iterator<T>,
40 ::std::random_access_iterator_tag,
41 ::boost::compute::detail::buffer_value<T>
45 } // end detail namespace
47 /// \class constant_buffer_iterator
48 /// \brief An iterator for a buffer in the \c constant memory space.
50 /// The constant_buffer_iterator class provides an iterator for values in a
51 /// buffer in the \c constant memory space.
53 /// For iterating over values in the \c global memory space (the most common
54 /// case), use the buffer_iterator class.
56 /// \see buffer_iterator
58 class constant_buffer_iterator :
59 public detail::constant_buffer_iterator_base<T>::type
62 typedef typename detail::constant_buffer_iterator_base<T>::type super_type;
63 typedef typename super_type::reference reference;
64 typedef typename super_type::difference_type difference_type;
66 constant_buffer_iterator()
72 constant_buffer_iterator(const buffer &buffer, size_t index)
78 constant_buffer_iterator(const constant_buffer_iterator<T> &other)
79 : m_buffer(other.m_buffer),
80 m_index(other.m_index)
84 constant_buffer_iterator<T>& operator=(const constant_buffer_iterator<T> &other)
87 m_buffer = other.m_buffer;
88 m_index = other.m_index;
94 ~constant_buffer_iterator()
98 const buffer& get_buffer() const
103 size_t get_index() const
108 T read(command_queue &queue) const
110 BOOST_ASSERT(m_buffer && m_buffer->get());
111 BOOST_ASSERT(m_index < m_buffer->size() / sizeof(T));
113 return detail::read_single_value<T>(m_buffer, m_index, queue);
116 void write(const T &value, command_queue &queue)
118 BOOST_ASSERT(m_buffer && m_buffer->get());
119 BOOST_ASSERT(m_index < m_buffer->size() / sizeof(T));
121 detail::write_single_value<T>(m_buffer, m_index, queue);
125 detail::buffer_iterator_index_expr<T, Expr>
126 operator[](const Expr &expr) const
128 BOOST_ASSERT(m_buffer);
129 BOOST_ASSERT(m_buffer->get());
131 return detail::buffer_iterator_index_expr<T, Expr>(
132 *m_buffer, m_index, memory_object::constant_memory, expr
137 friend class ::boost::iterator_core_access;
139 reference dereference() const
141 return detail::buffer_value<T>(*m_buffer, m_index);
144 bool equal(const constant_buffer_iterator<T> &other) const
146 return m_buffer == other.m_buffer && m_index == other.m_index;
159 void advance(difference_type n)
161 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
164 difference_type distance_to(const constant_buffer_iterator<T> &other) const
166 return static_cast<difference_type>(other.m_index - m_index);
170 const buffer *m_buffer;
174 /// Creates a new constant_buffer_iterator for \p buffer at \p index.
176 /// \param buffer the \ref buffer object
177 /// \param index the index in the buffer
179 /// \return a \c constant_buffer_iterator for \p buffer at \p index
181 inline constant_buffer_iterator<T>
182 make_constant_buffer_iterator(const buffer &buffer, size_t index = 0)
184 return constant_buffer_iterator<T>(buffer, index);
187 /// \internal_ (is_device_iterator specialization for constant_buffer_iterator)
189 struct is_device_iterator<constant_buffer_iterator<T> > : boost::true_type {};
193 // is_buffer_iterator specialization for constant_buffer_iterator
194 template<class Iterator>
195 struct is_buffer_iterator<
197 typename boost::enable_if<
199 constant_buffer_iterator<typename Iterator::value_type>,
200 typename boost::remove_const<Iterator>::type
203 > : public boost::true_type {};
205 } // end detail namespace
206 } // end compute namespace
207 } // end boost namespace
209 #endif // BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP