CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
reference.hpp
1 //*LB*
2 // Copyright (c) 2010, University of Bonn, Institute for Computer Science VI
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the University of Bonn
14 // nor the names of its contributors may be used to endorse or promote
15 // products derived from this software without specific prior written
16 // permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //*LE*
29 
30 #ifndef __REFERENCE_HPP__
31 #define __REFERENCE_HPP__
32 
33 #include<boost/type_traits/is_convertible.hpp>
34 #include<boost/utility/enable_if.hpp>
35 #include<cuv/tools/cuv_general.hpp>
36 #include<cuv/tools/meta_programming.hpp>
37 #include<cuv/basics/accessors.hpp>
38 
39 namespace cuv
40 {
45  template<class T, class M, class I>
46  struct reference
47  {
48  typedef T* pointer_type;
49  typedef typename unconst<T>::type value_type;
50  typedef M memory_space_type;
52  typedef I index_type;
53 
54  const pointer_type ptr;
55 
57  operator value_type ()const{
58  return detail::entry_get(ptr,(index_type)0,memory_space_type());
59  }
60 
62  void operator=(const value_type& v){
63  detail::entry_set(ptr,(index_type)0,v,memory_space_type());
64  }
65 
67  template<class _T>
68  typename
69  boost::enable_if_c<boost::is_convertible<_T,value_type>::value >::type
70  operator=(const _T& v){
71  detail::entry_set(ptr,(index_type)0,(value_type)v,memory_space_type());
72  }
73 
76  {
77  if(&o == &(*this)) // operator & is overloaded and returns value_type*
78  return *this;
79  (*this) = (value_type)o;
80  return *this;
81  }
82 
84  template<class O>
86  {
87  (*this) = (T)o;
88  return *this;
89  }
90 
91 
93  pointer_type operator&(void)const{
94  return ptr;
95  }
96 
99  :ptr(p)
100  {
101  }
102 
104  reference(T & p)
105  :ptr(&p)
106  {
107  }
108 
110  reference(const T & p)
111  :ptr(&p)
112  {
113  }
114 
116  my_type& operator+=(const value_type& v){ *this = (value_type)(*this)+v; return *this; }
118  my_type& operator-=(const value_type& v){ *this = (value_type)(*this)-v; return *this; }
120  my_type& operator*=(const value_type& v){ *this = (value_type)(*this)*v; return *this; }
122  my_type& operator/=(const value_type& v){ *this = (value_type)(*this)/v; return *this; }
124  value_type operator++(int){value_type v=*this; *this=v+1; return v;}
126  value_type operator--(int){value_type v=*this; *this=v-1; return v;}
128  value_type operator++(){value_type v=*this; *this=v+1; return v+1;}
130  value_type operator--(){value_type v=*this; *this=v-1; return v-1;}
131 
133  bool operator==(const value_type& v){ return ((value_type)*this)==v;}
135  bool operator<=(const value_type& v){ return ((value_type)*this)<=v;}
137  bool operator< (const value_type& v){ return ((value_type)*this)< v;}
139  bool operator>=(const value_type& v){ return ((value_type)*this)>=v;}
141  bool operator> (const value_type& v){ return ((value_type)*this)> v;}
142  };
143 
144 }
145 
146 
147 #endif /* __REFERENCE_HPP__ */