aimstil  5.0.5
value_proxy.h
Go to the documentation of this file.
1 #ifndef TIL_VALUE_PROXY_H_
2 #define TIL_VALUE_PROXY_H_
3 
4 // includes from STL
5 #include <cstdlib>
6 
7 // includes from BOOST
8 #include "boost/call_traits.hpp"
9 #include "boost/type_traits.hpp"
10 
11 // includes from TIL
12 #include "til/traits.h"
14 
15 namespace til
16 {
17 
18  //------------------------------------------------------------------------------------------------
19 
20  //---------------------//
21  // const_value_proxy //
22  //---------------------//
23 
31 
32  template < typename TContainer, typename TIndex = std::size_t, typename AccessPolicy = policy::VPAccess_Default<TContainer,TIndex> > //, typename T = typename value_type<TContainer>::type >
34  {
35  public: // typedefs
36 
38  //typedef TContainer container;
40  typedef TIndex index_type;
41  typedef typename AccessPolicy::const_reference const_reference;
42  typedef typename AccessPolicy::const_pointer const_pointer;
43 
44  public: // constructors & destructor
45 
46  //const_value_proxy(TIndex i, TContainer * pContainer) : m_i(i), m_pContainer(pContainer) {}
47  const_value_proxy(TIndex i, TContainer & container) : m_i(i), m_pContainer(&container) {}
48  template < typename XContainer, typename XIndex, typename XAccessPolicy>
50  : m_i(other.index())
51  , m_pContainer(&other.container())
52  {}
53 
54  public: // operators
55 
56  const_pointer operator->() const
57  {
58  return AccessPolicy::getPointer(*m_pContainer, m_i);
59  }
60 
61  //operator const value_type () const
62  operator const_reference ()
63  {
64  //return m_pContainer->get(m_i);
65  return AccessPolicy::get(*m_pContainer, m_i);
66  }
67 
68  public: // set & get
69 
70  // TIndex might be a reference. Therefore, manipulation of
71  // TIndex has to go through such objects.
72  typename boost::call_traits<TIndex>::const_reference index() const { return m_i; }
73  typename boost::call_traits<TIndex>::reference index() { return m_i; }
74  //TContainer * pContainer() { return m_pContainer; }
75  TContainer & container() { return *m_pContainer; }
76  const TContainer & container() const { return *m_pContainer; }
77 
78  /*
79  void operator=(const Self & other)
80  {
81  m_i = other.m_i;
82  m_container = other.m_container;
83  }
84  */
85 
86  private: // data
87 
88  TIndex m_i;
89  TContainer * m_pContainer;
90  //TContainer & m_container;
91  };
92 
93 
94  //------------------------------------------------------------------------------------------------
95 
96  //---------------//
97  // value_proxy //
98  //---------------//
99 
101  template < typename TContainer, typename TIndex = std::size_t, typename AccessPolicy = policy::VPAccess_Default<TContainer,TIndex> > //, typename T = typename value_type<TContainer>::type >
102  class value_proxy : public const_value_proxy<TContainer, TIndex, AccessPolicy>
103  {
104  public: // typedefs
105 
108  typedef typename Base::value_type value_type;
109 
110  public: // constructors & destructor
111 
112  //value_proxy(TIndex i, TContainer * pContainer) : Base(i, pContainer) {}
113  value_proxy(TIndex i, TContainer & container) : Base(i, container) {}
114  template < typename XContainer, typename XIndex, typename XAccessPolicy >
116  : Base(other)
117  {}
118 
119  public: // operators
120 
121  // NB: actually, I guess all these operations could be done using a value_type& conversion
122  // operator. The problem doing so is that when getting a reference on a value that was not
123  // initialized before, the container has to initialize it to the default value. Which might
124  // be bad, because we might be breaking the rule that no value should be initialized to
125  // the default value.
126 
127  void operator=(typename boost::call_traits<value_type>::param_type value)
128  {
129  // NB: we have to use set, and not getref, because we may be inserting a new value.
130  //m_pContainer->set(m_i, value);
131  //AccessPolicy::set(this->pContainer(), this->index(), value);
132  AccessPolicy::set(this->container(), this->index(), value);
133  }
134 
135  void operator+=(typename boost::call_traits<value_type>::param_type value)
136  {
137  this->assignOp(value, std::plus<value_type>());
138  }
139 
140  void operator-=(typename boost::call_traits<value_type>::param_type value)
141  {
142  this->assignOp(value, std::minus<value_type>());
143  }
144 
145  void operator*=(typename boost::call_traits<value_type>::param_type value)
146  {
147  this->assignOp(value, std::multiplies<value_type>());
148  }
149 
150  void operator/=(typename boost::call_traits<value_type>::param_type value)
151  {
152  this->assignOp(value, std::divides<value_type>());
153  }
154 
155  /*
156  operator value_type & ()
157  {
158  //return m_pContainer->getref(m_i);
159  return AccessPolicy::getref(this->pContainer(), this->index());
160  }
161  */
162 
163 
164  /*
165  void operator=(const Self & other)
166  {
167  this->Base::operator=(other);
168  }
169  */
170 
171  private: // functions
172 
173  template < typename TFunctor >
174  void assignOp(typename boost::call_traits<value_type>::param_type value, TFunctor f)
175  {
176  //AccessPolicy::set(this->pContainer(), this->index(),
177  // f(AccessPolicy::get(this->pContainer(), this->index()), value));
178  AccessPolicy::set(this->container(), this->index(),
179  f(AccessPolicy::get(this->container(), this->index()), value));
180  }
181  };
182 
183 } // namespace til
184 
185 
186 #endif /*VALUE_PROXY_H_*/
const_value_proxy< TContainer, TIndex, AccessPolicy > Self
Definition: value_proxy.h:37
void operator/=(typename boost::call_traits< value_type >::param_type value)
Definition: value_proxy.h:150
Adds some left-value operations to const_value_proxy.
Definition: value_proxy.h:102
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
const_pointer operator->() const
Definition: value_proxy.h:56
void operator*=(typename boost::call_traits< value_type >::param_type value)
Definition: value_proxy.h:145
boost::call_traits< TIndex >::reference index()
Definition: value_proxy.h:73
const_value_proxy(TIndex i, TContainer &container)
Definition: value_proxy.h:47
AccessPolicy::const_reference const_reference
Definition: value_proxy.h:41
void operator-=(typename boost::call_traits< value_type >::param_type value)
Definition: value_proxy.h:140
void operator+=(typename boost::call_traits< value_type >::param_type value)
Definition: value_proxy.h:135
value_proxy< TContainer, TIndex, AccessPolicy > Self
Definition: value_proxy.h:106
value_proxy(const value_proxy< XContainer, XIndex, XAccessPolicy > &other)
Definition: value_proxy.h:115
void set(TImage &im, typename TImage::value_type value)
Definition: imageArith.h:22
Base::value_type value_type
Definition: value_proxy.h:108
TContainer & container()
Definition: value_proxy.h:75
The value_proxy is designed to provide a simple access to container values that are in fact complicat...
Definition: value_proxy.h:33
value_type_of< TContainer >::type value_type
Definition: value_proxy.h:39
const_value_proxy< TContainer, TIndex, AccessPolicy > Base
Definition: value_proxy.h:107
void operator=(typename boost::call_traits< value_type >::param_type value)
Definition: value_proxy.h:127
AccessPolicy::const_pointer const_pointer
Definition: value_proxy.h:42
const TContainer & container() const
Definition: value_proxy.h:76
value_proxy(TIndex i, TContainer &container)
Definition: value_proxy.h:113
const_value_proxy(const const_value_proxy< XContainer, XIndex, XAccessPolicy > &other)
Definition: value_proxy.h:49
boost::call_traits< TIndex >::const_reference index() const
Definition: value_proxy.h:72
T::value_type type
Definition: traits.h:200