buffer_value.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP
  11. #define BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP
  12. #include <boost/compute/context.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/detail/device_ptr.hpp>
  15. #include <boost/compute/detail/read_write_single_value.hpp>
  16. namespace boost {
  17. namespace compute {
  18. namespace detail {
  19. template<class T>
  20. class buffer_value
  21. {
  22. public:
  23. typedef T value_type;
  24. buffer_value()
  25. {
  26. }
  27. buffer_value(const value_type &value)
  28. : m_value(value)
  29. {
  30. }
  31. // creates a reference for the value in buffer at index (in bytes).
  32. buffer_value(const buffer &buffer, size_t index)
  33. : m_buffer(buffer.get(), false),
  34. m_index(index)
  35. {
  36. }
  37. buffer_value(const buffer_value<T> &other)
  38. : m_buffer(other.m_buffer.get(), false),
  39. m_index(other.m_index)
  40. {
  41. }
  42. ~buffer_value()
  43. {
  44. // set buffer to null so that its reference count will
  45. // not be decremented when its destructor is called
  46. m_buffer.get() = 0;
  47. }
  48. operator value_type() const
  49. {
  50. if(m_buffer.get()){
  51. const context &context = m_buffer.get_context();
  52. const device &device = context.get_device();
  53. command_queue queue(context, device);
  54. return detail::read_single_value<T>(m_buffer, m_index / sizeof(T), queue);
  55. }
  56. else {
  57. return m_value;
  58. }
  59. }
  60. buffer_value<T> operator-() const
  61. {
  62. return -T(*this);
  63. }
  64. bool operator<(const T &value) const
  65. {
  66. return T(*this) < value;
  67. }
  68. bool operator>(const T &value) const
  69. {
  70. return T(*this) > value;
  71. }
  72. bool operator<=(const T &value) const
  73. {
  74. return T(*this) <= value;
  75. }
  76. bool operator>=(const T &value) const
  77. {
  78. return T(*this) <= value;
  79. }
  80. bool operator==(const T &value) const
  81. {
  82. return T(*this) == value;
  83. }
  84. bool operator==(const buffer_value<T> &other) const
  85. {
  86. if(m_buffer.get() != other.m_buffer.get()){
  87. return false;
  88. }
  89. if(m_buffer.get()){
  90. return m_index == other.m_index;
  91. }
  92. else {
  93. return m_value == other.m_value;
  94. }
  95. }
  96. bool operator!=(const T &value) const
  97. {
  98. return T(*this) != value;
  99. }
  100. buffer_value<T>& operator=(const T &value)
  101. {
  102. if(m_buffer.get()){
  103. const context &context = m_buffer.get_context();
  104. command_queue queue(context, context.get_device());
  105. detail::write_single_value<T>(
  106. value, m_buffer, m_index / sizeof(T), queue
  107. ).wait();
  108. return *this;
  109. }
  110. else {
  111. m_value = value;
  112. return *this;
  113. }
  114. }
  115. buffer_value<T>& operator=(const buffer_value<T> &value)
  116. {
  117. return operator=(T(value));
  118. }
  119. detail::device_ptr<T> operator&() const
  120. {
  121. return detail::device_ptr<T>(m_buffer, m_index);
  122. }
  123. buffer_value<T>& operator++()
  124. {
  125. if(m_buffer.get()){
  126. T value = T(*this);
  127. value++;
  128. *this = value;
  129. }
  130. else {
  131. m_value++;
  132. }
  133. return *this;
  134. }
  135. buffer_value<T> operator++(int)
  136. {
  137. buffer_value<T> result(*this);
  138. ++(*this);
  139. return result;
  140. }
  141. private:
  142. const buffer m_buffer;
  143. size_t m_index;
  144. value_type m_value;
  145. };
  146. } // end detail namespace
  147. } // end compute namespace
  148. } // end boost namespace
  149. #endif // BOOST_COMPUTE_DETAIL_BUFFER_VALUE_HPP