• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP
13 
14 #include <cstddef>
15 #include <iterator>
16 
17 #include <boost/iterator/iterator_facade.hpp>
18 
19 #include <boost/compute/buffer.hpp>
20 #include <boost/compute/iterator/buffer_iterator.hpp>
21 #include <boost/compute/type_traits/is_device_iterator.hpp>
22 
23 namespace boost {
24 namespace compute {
25 
26 // forward declaration for constant_buffer_iterator<T>
27 template<class T> class constant_buffer_iterator;
28 
29 namespace detail {
30 
31 // helper class which defines the iterator_facade super-class
32 // type for constant_buffer_iterator<T>
33 template<class T>
34 class constant_buffer_iterator_base
35 {
36 public:
37     typedef ::boost::iterator_facade<
38         ::boost::compute::constant_buffer_iterator<T>,
39         T,
40         ::std::random_access_iterator_tag,
41         ::boost::compute::detail::buffer_value<T>
42     > type;
43 };
44 
45 } // end detail namespace
46 
47 /// \class constant_buffer_iterator
48 /// \brief An iterator for a buffer in the \c constant memory space.
49 ///
50 /// The constant_buffer_iterator class provides an iterator for values in a
51 /// buffer in the \c constant memory space.
52 ///
53 /// For iterating over values in the \c global memory space (the most common
54 /// case), use the buffer_iterator class.
55 ///
56 /// \see buffer_iterator
57 template<class T>
58 class constant_buffer_iterator :
59     public detail::constant_buffer_iterator_base<T>::type
60 {
61 public:
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;
65 
constant_buffer_iterator()66     constant_buffer_iterator()
67         : m_buffer(0),
68           m_index(0)
69     {
70     }
71 
constant_buffer_iterator(const buffer & buffer,size_t index)72     constant_buffer_iterator(const buffer &buffer, size_t index)
73         : m_buffer(&buffer),
74           m_index(index)
75     {
76     }
77 
constant_buffer_iterator(const constant_buffer_iterator<T> & other)78     constant_buffer_iterator(const constant_buffer_iterator<T> &other)
79         : m_buffer(other.m_buffer),
80           m_index(other.m_index)
81     {
82     }
83 
operator =(const constant_buffer_iterator<T> & other)84     constant_buffer_iterator<T>& operator=(const constant_buffer_iterator<T> &other)
85     {
86         if(this != &other){
87             m_buffer = other.m_buffer;
88             m_index = other.m_index;
89         }
90 
91         return *this;
92     }
93 
~constant_buffer_iterator()94     ~constant_buffer_iterator()
95     {
96     }
97 
get_buffer() const98     const buffer& get_buffer() const
99     {
100         return *m_buffer;
101     }
102 
get_index() const103     size_t get_index() const
104     {
105         return m_index;
106     }
107 
read(command_queue & queue) const108     T read(command_queue &queue) const
109     {
110         BOOST_ASSERT(m_buffer && m_buffer->get());
111         BOOST_ASSERT(m_index < m_buffer->size() / sizeof(T));
112 
113         return detail::read_single_value<T>(m_buffer, m_index, queue);
114     }
115 
write(const T & value,command_queue & queue)116     void write(const T &value, command_queue &queue)
117     {
118         BOOST_ASSERT(m_buffer && m_buffer->get());
119         BOOST_ASSERT(m_index < m_buffer->size() / sizeof(T));
120 
121         detail::write_single_value<T>(m_buffer, m_index, queue);
122     }
123 
124     template<class Expr>
125     detail::buffer_iterator_index_expr<T, Expr>
operator [](const Expr & expr) const126     operator[](const Expr &expr) const
127     {
128         BOOST_ASSERT(m_buffer);
129         BOOST_ASSERT(m_buffer->get());
130 
131         return detail::buffer_iterator_index_expr<T, Expr>(
132             *m_buffer, m_index, memory_object::constant_memory, expr
133         );
134     }
135 
136 private:
137     friend class ::boost::iterator_core_access;
138 
dereference() const139     reference dereference() const
140     {
141         return detail::buffer_value<T>(*m_buffer, m_index);
142     }
143 
equal(const constant_buffer_iterator<T> & other) const144     bool equal(const constant_buffer_iterator<T> &other) const
145     {
146         return m_buffer == other.m_buffer && m_index == other.m_index;
147     }
148 
increment()149     void increment()
150     {
151         m_index++;
152     }
153 
decrement()154     void decrement()
155     {
156         m_index--;
157     }
158 
advance(difference_type n)159     void advance(difference_type n)
160     {
161         m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
162     }
163 
distance_to(const constant_buffer_iterator<T> & other) const164     difference_type distance_to(const constant_buffer_iterator<T> &other) const
165     {
166         return static_cast<difference_type>(other.m_index - m_index);
167     }
168 
169 private:
170     const buffer *m_buffer;
171     size_t m_index;
172 };
173 
174 /// Creates a new constant_buffer_iterator for \p buffer at \p index.
175 ///
176 /// \param buffer the \ref buffer object
177 /// \param index the index in the buffer
178 ///
179 /// \return a \c constant_buffer_iterator for \p buffer at \p index
180 template<class T>
181 inline constant_buffer_iterator<T>
make_constant_buffer_iterator(const buffer & buffer,size_t index=0)182 make_constant_buffer_iterator(const buffer &buffer, size_t index = 0)
183 {
184     return constant_buffer_iterator<T>(buffer, index);
185 }
186 
187 /// \internal_ (is_device_iterator specialization for constant_buffer_iterator)
188 template<class T>
189 struct is_device_iterator<constant_buffer_iterator<T> > : boost::true_type {};
190 
191 namespace detail {
192 
193 // is_buffer_iterator specialization for constant_buffer_iterator
194 template<class Iterator>
195 struct is_buffer_iterator<
196     Iterator,
197     typename boost::enable_if<
198         boost::is_same<
199             constant_buffer_iterator<typename Iterator::value_type>,
200             typename boost::remove_const<Iterator>::type
201         >
202     >::type
203 > : public boost::true_type {};
204 
205 } // end detail namespace
206 } // end compute namespace
207 } // end boost namespace
208 
209 #endif // BOOST_COMPUTE_ITERATOR_CONSTANT_BUFFER_ITERATOR_HPP
210