aimstil  5.0.5
index_collection.h
Go to the documentation of this file.
1 #ifndef INDEX_COLLECTION_H_
2 #define INDEX_COLLECTION_H_
3 
4 // includes from STL
5 #include <cassert>
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/templateTools.h"
13 
14 // includes from TIL
15 #include "declarations_external.h"
16 #include "globalTraits.h"
17 
18 namespace til
19 {
20 
22  // I highly suspect it fails because of stupid GCC 3.3
23 
24  // NB: we template on TPointer since it can be an iterator
25  template < typename TIndex, typename T, typename TAlloc, typename TPointer >
26  inline
27  typename boost::enable_if_c<
28  boost::is_same<TIndex, std::size_t>::value &&
29  !boost::is_same<TPointer, std::size_t>::value
30  ,
31  TIndex
32  >::type
33  getIndex(const std::vector<T, TAlloc> & c, TPointer pElem)
34  {
35  return std::distance(&*(c.begin()), &*pElem);
36  }
37 
38  template < typename TIndex, typename TCollection >
39  inline
40  typename boost::enable_if<
41  boost::is_same<TIndex, std::size_t>
42  ,
43  TIndex
44  >::type
45  getIndex(const TCollection &, std::size_t i)
46  {
47  return i;
48  }
49 
50  template < typename TIndex, typename T, typename TAlloc, typename TPointer >
51  inline
52  typename boost::enable_if_c<
53  boost::is_same<TIndex, T*>::value &&
54  !boost::is_same<TPointer, std::size_t>::value
55  ,
56  TIndex
57  >::type
58  getIndex(const std::vector<T, TAlloc> & c, TPointer pElem)
59  {
60  return &*pElem;
61  }
62 
63  template < typename TIndex, typename TContainer >
64  inline
65  typename boost::enable_if<
66  boost::is_same<TIndex, typename value_type_of<TContainer>::type*>
67  ,
68  TIndex
69  >::type
70  getIndex(const TContainer & c, std::size_t i)
71  {
72  return &(c[i]);
73  }
74 
75 
76  /*
77  namespace detail
78  {
79  template < typename TContainer, typename TPointer, typename TReturn >
80  TReturn
81  indexee_pointer(TContainer &, TPointer p)
82  {
83  return *p;
84  }
85 
86  template < typename TContainer, typename TScalarIndex, typename TReturn >
87  TReturn
88  indexee_scalar(const TContainer & c, TScalarIndex i)
89  {
90  return c[i];
91  }
92  }
93  */
94 
95  /*
96  template < typename TContainer >
97  inline
98  typename type_if< boost::is_const<TContainer>::value,
99  typename boost::call_traits<typename value_type<TContainer>::type>::const_reference,
100  typename boost::call_traits<typename value_type<TContainer>::type>::reference
101  >::type
102  indexee(TContainer & c, typename value_type<TContainer>::type * p)
103  {
104  return *p;
105  }
106 
107 
108  template < typename TContainer >
109  inline
110  typename type_if< boost::is_const<TContainer>::value,
111  typename boost::call_traits<typename value_type<TContainer>::type>::const_reference,
112  typename boost::call_traits<typename value_type<TContainer>::type>::reference
113  >::type
114  indexee(TContainer & c, std::size_t i)
115  {
116  return c[i];
117  }
118  */
119 
120  namespace detail
121  {
122 
123  template < typename TContainer, typename TPointer >
125  {
126  public: // typedefs
127 
128  typedef TPointer index_type;
129  typedef typename TContainer::value_type indexed_type;
130 
131  public: // constructors & destructor
132 
134  index_collection_pointer(TContainer &) {}
135 
136  public: // functions
137 
138  typename boost::call_traits<indexed_type>::const_reference
139  get(typename boost::call_traits<TPointer>::param_type p) const
140  {
141  return *p;
142  }
143 
144  typename boost::call_traits<indexed_type>::reference
145  get(typename boost::call_traits<TPointer>::param_type p)
146  {
147  return *p;
148  }
149  };
150 
151  template < typename TContainer, typename TScalarIndex >
153  {
154  public: // typedefs
155 
156  typedef TScalarIndex index_type;
157  typedef typename TContainer::value_type indexed_type;
158  typedef typename TContainer::reference reference;
159  typedef typename TContainer::const_reference const_reference;
160 
161  public: // constructors & destructor
162 
163  index_collection_scalar() : m_pContainer() {}
164  index_collection_scalar(TContainer & c) : m_pContainer(&c) {}
165 
166  public: // set & get
167 
168  void setContainer(TContainer & c) { m_pContainer = &c; }
169  TContainer & getContainer() { assert(m_pContainer != 0); return *m_pContainer; }
170  const TContainer & getContainer() const { assert(m_pContainer != 0); return *m_pContainer; }
171 
172  public: // functions
173 
174  const_reference get(TScalarIndex i) const
175  {
176  assert(m_pContainer != 0);
177  return (*m_pContainer)[i];
178  }
179 
180  reference get(TScalarIndex i)
181  {
182  assert(m_pContainer != 0);
183  return (*m_pContainer)[i];
184  }
185 
186  private: // data
187 
188  // TODO: why using a shared_ptr? It's a pain because it requires the user to use it as well.
189  // OK, but we need to use a pointer, because we need to be able to default-initialize the class
190  TContainer * m_pContainer;
191  //shared_ptr<TContainer> m_pContainer;
192  };
193  } // namespace detail
194 
195 
196  template < typename TContainer, typename TIndex >
198 
199 
200  template < typename TContainer, typename T >
201  class index_collection< TContainer, T* >
203  {
204  public: // typedefs
205 
207 
208  public: // constructors & destructor
209 
210  index_collection() : Base() {}
211  index_collection(TContainer & c) : Base(c) {}
212  };
213 
214 
215  template < typename TContainer >
216  class index_collection< TContainer, std::size_t >
217  : public detail::index_collection_scalar<TContainer, std::size_t >
218  {
219  public: // typedefs
220 
222 
223  public: // constructors & destructor
224 
225  index_collection() : Base() {}
226  index_collection(TContainer & c) : Base(c) {}
227  };
228 
229 
230 
231  // NB: TIndexCollection could actually be a reference
232  template < typename TIndexCollection >
234  {
235  public: // typedefs
236  typedef typename TIndexCollection::reference reference;
237 
238  public: //
239  // NB: we have to use call_traits as TIndexCollection might be a reference
241  : m_indices(indices)
242  {}
243  public: //
244  reference operator[](std::size_t i) { return this->get(i); }
245  private: // data
246  TIndexCollection m_indices;
247  };
248 
249 
250 } // namespace til
251 
252 
253 #endif /*INDEX_COLLECTION_H_*/
ImageParameter param(const TImage &im)
Create an ImageParameter structure out of an Image.
Definition: image_common.h:34
STL namespace.
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
TContainer::const_reference const_reference
reference operator[](std::size_t i)
detail::index_collection_pointer< TContainer, T * > Base
TIndexCollection::reference reference
detail::index_collection_scalar< TContainer, std::size_t > Base
Collects template tools used for library implementation.
boost::enable_if_c< boost::is_same< TIndex, std::size_t >::value &&!boost::is_same< TPointer, std::size_t >::value, TIndex >::type getIndex(const std::vector< T, TAlloc > &c, TPointer pElem)
const TContainer & getContainer() const
virtual_collection(typename boost::call_traits< TIndexCollection >::param indices)