-
Notifications
You must be signed in to change notification settings - Fork 334
/
buffer_value.hpp
178 lines (147 loc) · 3.93 KB
/
buffer_value.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP
#define BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP
#include <boost/compute/context.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/detail/device_ptr.hpp>
#include <boost/compute/detail/read_write_single_value.hpp>
namespace boost {
namespace compute {
namespace detail {
template<class T>
class buffer_value
{
public:
typedef T value_type;
buffer_value()
{
}
buffer_value(const value_type &value)
: m_value(value)
{
}
// creates a reference for the value in buffer at index (in bytes).
buffer_value(const buffer &buffer, size_t index)
: m_buffer(buffer.get(), false),
m_index(index)
{
}
buffer_value(const buffer_value<T> &other)
: m_buffer(other.m_buffer.get(), false),
m_index(other.m_index)
{
}
~buffer_value()
{
// set buffer to null so that its reference count will
// not be decremented when its destructor is called
m_buffer.get() = 0;
}
operator value_type() const
{
if(m_buffer.get()){
const context &context = m_buffer.get_context();
const device &device = context.get_device();
command_queue queue(context, device);
return detail::read_single_value<T>(m_buffer, m_index / sizeof(T), queue);
}
else {
return m_value;
}
}
buffer_value<T> operator-() const
{
return -T(*this);
}
bool operator<(const T &value) const
{
return T(*this) < value;
}
bool operator>(const T &value) const
{
return T(*this) > value;
}
bool operator<=(const T &value) const
{
return T(*this) <= value;
}
bool operator>=(const T &value) const
{
return T(*this) <= value;
}
bool operator==(const T &value) const
{
return T(*this) == value;
}
bool operator==(const buffer_value<T> &other) const
{
if(m_buffer.get() != other.m_buffer.get()){
return false;
}
if(m_buffer.get()){
return m_index == other.m_index;
}
else {
return m_value == other.m_value;
}
}
bool operator!=(const T &value) const
{
return T(*this) != value;
}
buffer_value<T>& operator=(const T &value)
{
if(m_buffer.get()){
const context &context = m_buffer.get_context();
command_queue queue(context, context.get_device());
detail::write_single_value<T>(value, m_buffer, m_index / sizeof(T), queue);
return *this;
}
else {
m_value = value;
return *this;
}
}
buffer_value<T>& operator=(const buffer_value<T> &value)
{
return operator=(T(value));
}
detail::device_ptr<T> operator&() const
{
return detail::device_ptr<T>(m_buffer, m_index);
}
buffer_value<T>& operator++()
{
if(m_buffer.get()){
T value = T(*this);
value++;
*this = value;
}
else {
m_value++;
}
return *this;
}
buffer_value<T> operator++(int)
{
buffer_value<T> result(*this);
++(*this);
return result;
}
private:
const buffer m_buffer;
size_t m_index;
value_type m_value;
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP