curfil  ..
 All Classes Functions Variables Typedefs Friends Groups Pages
reference.hpp
1 #ifndef __CUV_REFERENCE_HPP__
2 #define __CUV_REFERENCE_HPP__
3 
4 #include <boost/type_traits/is_convertible.hpp>
5 #include <boost/utility/enable_if.hpp>
6 #include <iostream>
7 
8 #include "meta_programming.hpp"
9 #include "tags.hpp"
10 
11 namespace cuv {
12 
13 namespace detail {
14 
23 template<class value_type>
24 void entry_set(value_type* ptr, size_t idx, value_type val, host_memory_space);
25 
34 template<class value_type>
35 value_type entry_get(const value_type* ptr, size_t idx, host_memory_space);
36 
37 template<class value_type>
38 void entry_set(value_type* ptr, size_t idx, value_type val, dev_memory_space);
39 
43 template<class value_type>
44 value_type entry_get(const value_type* ptr, size_t idx, dev_memory_space);
45 
46 }
47 
52 template<class T, class M>
53 class reference
54 {
55 
56 public:
57 
58  typedef typename unconst<T>::type value_type;
59  typedef M memory_space_type;
61 
63 
65  operator value_type() const {
66  return detail::entry_get(ptr, 0, memory_space_type());
67  }
68 
70  void operator=(const value_type& v) {
71  detail::entry_set(ptr, 0, v, memory_space_type());
72  }
73 
75  template<class _T>
76  typename boost::enable_if_c<boost::is_convertible<_T, value_type>::value>::type operator=(const _T& v) {
77  detail::entry_set(ptr, 0, (value_type) v, memory_space_type());
78  }
79 
82  if (&o == &(*this)) // operator & is overloaded and returns value_type*
83  return *this;
84  (*this) = (value_type) o;
85  return *this;
86  }
87 
89  template<class OM>
91  (*this) = static_cast<T>(o);
92  return *this;
93  }
94 
96  const value_type* operator&() const {
97  return ptr;
98  }
99 
102  return ptr;
103  }
104 
106  reference(const T* p) :
107  ptr(p) {
108  }
109 
111  reference(T* p) :
112  ptr(p) {
113  }
114 
117  ptr(&p) {
118  }
119 
121  reference(const value_type& p) :
122  ptr(&p) {
123  }
124 
127  *this = (value_type) (*this) + v;
128  return *this;
129  }
130 
133  *this = (value_type) (*this) - v;
134  return *this;
135  }
136 
139  *this = (value_type) (*this) * v;
140  return *this;
141  }
142 
145  *this = (value_type) (*this) / v;
146  return *this;
147  }
148 
151  value_type v = *this;
152  *this = v + 1;
153  return v;
154  }
155 
158  value_type v = *this;
159  *this = v - 1;
160  return v;
161  }
162 
165  value_type v = *this;
166  *this = v + 1;
167  return v + 1;
168  }
169 
172  value_type v = *this;
173  *this = v - 1;
174  return v - 1;
175  }
176 
178  bool operator==(const value_type& v) {
179  return ((value_type) *this) == v;
180  }
181 
183  bool operator<=(const value_type& v) {
184  return ((value_type) *this) <= v;
185  }
186 
188  bool operator<(const value_type& v) {
189  return ((value_type) *this) < v;
190  }
191 
193  bool operator>=(const value_type& v) {
194  return ((value_type) *this) >= v;
195  }
196 
198  bool operator>(const value_type& v) {
199  return ((value_type) *this) > v;
200  }
201 };
202 
203 }
204 
205 template<class T, class M>
206 std::ostream& operator<<(std::ostream& os, const cuv::reference<T, M>& reference);
207 
208 #endif