aimstil  5.0.5
convert.h
Go to the documentation of this file.
1 #ifndef _CONVERT_H_
2 #define _CONVERT_H_
3 
4 // includes from BOOST
5 //#include <boost/numeric/conversion/converter.hpp>
6 #include <boost/type_traits.hpp>
7 #include <boost/utility/enable_if.hpp>
8 
9 // includes from TIL
10 //#include "til/is_traits.h"
11 #include "til/templateTools.h"
12 #include "til/traits.h"
13 
14 // includes from TIL
15 #include "convert.h"
16 #include "globalTraits.h"
17 
18 namespace til
19 {
21 
22  /*
24  template < typename TTo, typename TFrom >
25  inline
26  void
27  convert(const TFrom &x, TTo &y)
28  {
30  typename ConvertTraits<TFrom, TTo>::Tag Tag;
31  convert(x, y, Tag());
32  }
33 
34 
36  template < typename TTo, typename TFrom >
37  inline
38  void
39  convert(const TFrom & x, TTo & y, convertTag::Default)
40  {
41  y = static_cast<TTo>(x);
42  }
43 
44 
46  template < typename TTo, typename TFrom >
47  inline
48  void
49  convert(const TFrom & x, TTo & y, convertTag::Numeric)
50  {
51  y = boost::numeric::converter<TTo,TFrom>::convert(x);
52  }
53 
54 
55 
56  template < typename TPoint3DTo, typename TPoint3DFrom >
57  inline
58  typename boost::enable_if_c<Is_3DPoint<TPoint3DFrom>::value && Is_3DPoint<TPoint3DTo>::value>::type
59  convert(const TPoint3DFrom & x, TPoint3DTo & y)
60  {
61  y[0] = x[0];
62  y[1] = x[1];
63  y[2] = x[2];
64  }
65  */
66 
68  template < typename TTo, typename TFrom >
69  inline
70  typename boost::enable_if_c<
71  // static_cast must work: even the default conversion excludes some cases!
72  boost::is_convertible<TFrom,TTo>::value
73  // numerical types
74  && !(std::numeric_limits<TFrom>::is_specialized || std::numeric_limits<TTo>::is_specialized)
75  && !is_same_naked<TFrom, TTo>::value
76  >::type
77  convert(const TFrom & x, TTo &y)
78  {
79  y = static_cast<TTo>(x);
80  }
81 
84  {
85  template < typename TTo, typename TFrom >
86  inline void operator()(const TFrom & x, TTo & y) const
87  {
88  // TODO: this is too slow because it also does range checking.
89  //y = boost::numeric::converter<TTo, TFrom>::convert(x);
90  y = x;
91  }
92  };
93 
94  template < typename TTo, typename TFrom >
95  inline
96  typename boost::enable_if_c<
97  std::numeric_limits<TFrom>::is_specialized
98  && std::numeric_limits<TTo>::is_specialized
99  // test for gcc3.3
101  >::type
102  convert(const TFrom & x, TTo &y)
103  {
104  Convert_numerical()(x,y);
105  }
106 
107  namespace detail
108  {
109  template < typename T >
110  inline void
111  convert_operatorEqual(const T & x, T & y)
112  {
113  y = x;
114  }
115  }
116 
119  template < typename T1, typename T2 >
120  inline
121  typename boost::enable_if<is_same_naked<T1,T2> >::type
122  convert(const T1 & x, T2 & y)
123  {
125  }
126 
127 
128  namespace detail
129  {
130  template < int D, typename T1, typename T2 >
131  inline
132  void convert_fixedLoop(const T1 & x, T2 & y)
133  {
134  for (int i=0; i<D; ++i) convert(x[i], y[i]);
135  }
136  }
137 
138 
139  inline void convert(const Point3df & x, Point3df & y)
140  {
141  detail::convert_fixedLoop<3>(x,y);
142  }
143 
145  struct Convert
146  {
147  template < typename T1, typename T2 >
148  void
149  operator()(const T1 & x, T2 & y) const
150  {
151  convert(x,y);
152  }
153  };
154 
155  template < typename T1, typename T2 >
156  struct Convert2
157  {
158  void
159  operator()(const T1 & x, T2 & y) const
160  {
161  convert(x,y);
162  }
163  };
164 
165 
166  template < typename TTo, typename TFrom >
167  TTo convertTo(const TFrom & x)
168  {
169  TTo y;
170  convert(x,y);
171  return y;
172  }
173 
174  //---------------------------------------------------------------------------
175 
176 
177  // This is just plain crap, it should at least be a recursive stuff...
178  // obviously a quick fix...
179  template < typename T, typename B >
180  void convert_allocate
181  (
182  std::vector<til::sparse_vector<T,B> > const & m
183  , std::vector<std::vector<std::pair<std::size_t, T> > > & v)
184  {
185  typedef std::vector<til::sparse_vector<T,B> > T1;
186  typedef std::vector<std::vector<std::pair<std::size_t, T> > > T2;
187 
188  v.resize(m.size());
189  typename T1::const_iterator im = m.begin();
190  typename T2::iterator iv = v.begin();
191  for (; iv != v.end(); ++im, ++iv)
192  {
193  iv->resize(im->getMap().size());
194  typename T1::value_type::Map::const_iterator iim = im->getMap().begin();
195  typename T2::value_type::iterator iiv = iv->begin();
196  for (; iiv != iv->end(); ++iim, ++iiv)
197  {
198  *iiv = *iim;
199  }
200  }
201  }
202 
203 
204  //---------------------------------------------------------------------------
205 
206  /*
211  // TODO: shouldn't this go into a Convert/Cast class?
212  template < typename TKey, typename TMap, typename TComp, typename TAlloc1, typename TAlloc2 >
213  void convert(const std::map<TKey, TMap, TComp, TAlloc1> & m, std::vector<std::pair<TKey, TMap>, TAlloc2> & v)
214  {
215  detail::loop_cc(m, v, Convert());
216  }
217  */
218 
219  //---------------------------------------------------------------------------
220 
221 } // namespace til
222 
223 #endif //_CONVERT_H_
224 
void convert(TTo &x, const TFrom &y)
Definition: functors.h:410
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
void operator()(const TFrom &x, TTo &y) const
Definition: convert.h:86
TODO: The idea here is to have no convert with a loop; use for_each(..., Convert()) instead...
Definition: convert.h:145
A class that mimic the behavior of std::vector but with a storage policy focused on sparse data...
void convert_allocate(std::vector< til::sparse_vector< T, B > > const &m, std::vector< std::vector< std::pair< std::size_t, T > > > &v)
Definition: convert.h:181
void convert_fixedLoop(const T1 &x, T2 &y)
Definition: convert.h:132
void operator()(const T1 &x, T2 &y) const
Definition: convert.h:159
conversion between numerical types
Definition: convert.h:83
Collects template tools used for library implementation.
TTo convertTo(const TFrom &x)
Definition: convert.h:167
void convert_operatorEqual(const T &x, T &y)
Definition: convert.h:111
void operator()(const T1 &x, T2 &y) const
Definition: convert.h:149