summaryrefslogtreecommitdiff
path: root/include/gudhi_patches/CGAL/NewKernel_d/Wrapper/Cartesian_wrap.h
blob: 44e9aa9621b398370071c1febd3f83ed4624b5f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2014
// INRIA Saclay-Ile de France (France)
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
// Author(s)     : Marc Glisse

#ifndef CGAL_KERNEL_D_CARTESIAN_WRAP_H
#define CGAL_KERNEL_D_CARTESIAN_WRAP_H

#include <CGAL/basic.h>
#include <CGAL/is_iterator.h>

#if defined(BOOST_MSVC)
#  pragma warning(push)
#  pragma warning(disable:4003) // not enough actual parameters for macro 'BOOST_PP_EXPAND_I'
                                // http://lists.boost.org/boost-users/2014/11/83291.php
#endif 
#include <CGAL/NewKernel_d/Wrapper/Point_d.h>
#include <CGAL/NewKernel_d/Wrapper/Vector_d.h>
#include <CGAL/NewKernel_d/Wrapper/Segment_d.h>
#include <CGAL/NewKernel_d/Wrapper/Sphere_d.h>
#include <CGAL/NewKernel_d/Wrapper/Hyperplane_d.h>
#include <CGAL/NewKernel_d/Wrapper/Weighted_point_d.h>

#include <CGAL/NewKernel_d/Wrapper/Ref_count_obj.h>

#include <boost/mpl/or.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/mpl/vector.hpp>

//TODO: do we want to store the kernel ref in the Object wrappers? It would allow for additions and operator[] and things like that to work, but objects would still need to be created by functors.

namespace CGAL {
namespace internal {
BOOST_MPL_HAS_XXX_TRAIT_DEF(Is_wrapper)
template<class T,bool=has_Is_wrapper<T>::value> struct Is_wrapper {
	enum { value=false };
	typedef Tag_false type;
};
template<class T> struct Is_wrapper<T,true> {
	typedef typename T::Is_wrapper type;
	enum { value=type::value };
};

template<class T,bool=is_iterator_type<T,std::input_iterator_tag>::value> struct Is_wrapper_iterator {
	enum { value=false };
	typedef Tag_false type;
};
template<class T> struct Is_wrapper_iterator<T,true> :
	Is_wrapper<typename std::iterator_traits<typename CGAL::decay<T>::type>::value_type>
{ };

struct Forward_rep {
//TODO: make a good C++0X version with perfect forwarding
//#ifdef CGAL_CXX11
//template <class T,class=typename std::enable_if<!Is_wrapper<typename std::decay<T>::type>::value&&!Is_wrapper_iterator<typename std::decay<T>::type>::value>::type>
//T&& operator()(typename std::remove_reference<T>::type&& t) const {return static_cast<T&&>(t);};
//template <class T,class=typename std::enable_if<!Is_wrapper<typename std::decay<T>::type>::value&&!Is_wrapper_iterator<typename std::decay<T>::type>::value>::type>
//T&& operator()(typename std::remove_reference<T>::type& t) const {return static_cast<T&&>(t);};
//
//template <class T,class=typename std::enable_if<Is_wrapper<typename std::decay<T>::type>::value>::type>
//typename Type_copy_cvref<T,typename std::decay<T>::type::Rep>::type&&
//operator()(T&& t) const {
//	return static_cast<typename Type_copy_cvref<T,typename std::decay<T>::type::Rep>::type&&>(t.rep());
//};
//
//template <class T,class=typename std::enable_if<Is_wrapper_iterator<typename std::decay<T>::type>::value>::type>
//transforming_iterator<Forward_rep,typename std::decay<T>::type>
//operator()(T&& t) const {
//	return make_transforming_iterator(std::forward<T>(t),Forward_rep());
//};
//#else
template <class T,bool=Is_wrapper<T>::value,bool=Is_wrapper_iterator<T>::value> struct result_;
template <class T> struct result_<T,false,false>{typedef T const& type;};
template <class T> struct result_<T,true,false>{typedef typename decay<T>::type::Rep const& type;};
template <class T> struct result_<T,false,true>{typedef transforming_iterator<Forward_rep,typename decay<T>::type> type;};
template<class> struct result;
template<class T> struct result<Forward_rep(T)> : result_<T> {};

template <class T> typename boost::disable_if<boost::mpl::or_<Is_wrapper<T>,Is_wrapper_iterator<T> >,T>::type const& operator()(T const& t) const {return t;}
template <class T> typename boost::disable_if<boost::mpl::or_<Is_wrapper<T>,Is_wrapper_iterator<T> >,T>::type& operator()(T& t) const {return t;}

template <class T> typename T::Rep const& operator()(T const& t, typename boost::enable_if<Is_wrapper<T> >::type* = 0) const {return t.rep();}

template <class T> transforming_iterator<Forward_rep,typename boost::enable_if<Is_wrapper_iterator<T>,T>::type> operator()(T const& t) const {return make_transforming_iterator(t,Forward_rep());}
//#endif
};
}

template <class B, class K, class T, bool = Provides_type<B, T>::value>
struct Map_wrapping_type : Get_type<B, T> {};
#define CGAL_REGISTER_OBJECT_WRAPPER(X) \
  template <class B, class K> \
  struct Map_wrapping_type <B, K, X##_tag, true> { \
    typedef Wrap::X##_d<K> type; \
  }
CGAL_REGISTER_OBJECT_WRAPPER(Point);
CGAL_REGISTER_OBJECT_WRAPPER(Vector);
CGAL_REGISTER_OBJECT_WRAPPER(Segment);
CGAL_REGISTER_OBJECT_WRAPPER(Sphere);
CGAL_REGISTER_OBJECT_WRAPPER(Hyperplane);
CGAL_REGISTER_OBJECT_WRAPPER(Weighted_point);
#undef CGAL_REGISTER_OBJECT_WRAPPER

// Note: this tends to be an all or nothing thing currently, wrapping
// only some types breaks, probably because we don't check whether the
// return type is indeed wrapped.
template < typename Base_ , typename Derived_ = Default >
struct Cartesian_wrap : public Base_
{
    CGAL_CONSTEXPR Cartesian_wrap(){}
    CGAL_CONSTEXPR Cartesian_wrap(int d):Base_(d){}
    typedef Base_ Kernel_base;
    typedef Cartesian_wrap Self;
    // TODO: pass the 2 types Self and Derived to the wrappers, they can use Self for most purposes and Derived only for Kernel_traits' typedef R.
    typedef typename Default::Get<Derived_, Self>::type Derived;
    // FIXME: The list doesn't belong here.
    typedef boost::mpl::vector<Point_tag,Segment_tag,Sphere_tag,Vector_tag,Hyperplane_tag> Wrapped_list;

    template <class T>
    struct Type : Map_wrapping_type<Base_, Derived, T> {};

    //Translate the arguments
    template <class T, class D = void,
      class=typename Get_functor_category<Derived,T>::type,
      bool=Provides_functor<Kernel_base, T>::value,
      bool=boost::mpl::contains<Wrapped_list,typename map_result_tag<T>::type>::type::value>
    struct Functor {
	    typedef typename Get_functor<Kernel_base, T>::type B;
	    struct type {
		    B b;
		    type(){}
		    type(Self const&k):b(k){}
		    typedef typename B::result_type result_type;
#ifdef CGAL_CXX11
		    template<class...U> result_type operator()(U&&...u)const{
			    return b(internal::Forward_rep()(u)...);
		    }
#else
#define CGAL_VAR(Z,N,_) internal::Forward_rep()(u##N)
#define CGAL_CODE(Z,N,_) template<BOOST_PP_ENUM_PARAMS(N,class U)> result_type \
		    operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,U,const&u))const{ \
			    return b(BOOST_PP_ENUM(N,CGAL_VAR,)); \
		    }
		    BOOST_PP_REPEAT_FROM_TO(1,11,CGAL_CODE,_)
#undef CGAL_CODE
#undef CGAL_VAR
// In case the last argument needs to be non-const. Fragile...
#define CGAL_VAR(Z,N,_) internal::Forward_rep()(u##N)
#define CGAL_CODE(Z,N,_) template<BOOST_PP_ENUM_PARAMS(N,class U),class V> result_type \
		    operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,U,const&u),V&v)const{ \
			    return b(BOOST_PP_ENUM(N,CGAL_VAR,),internal::Forward_rep()(v)); \
		    }
		    BOOST_PP_REPEAT_FROM_TO(1,8,CGAL_CODE,_)
#undef CGAL_CODE
#undef CGAL_VAR
#endif
	    };
    };

    // Preserve the difference between Null_functor and nothing.
    template <class T, class D, class C, bool b>
    struct Functor <T, D, C, false, b>
      : Get_functor <Kernel_base, T> {};

    //Translate both the arguments and the result
    //TODO: Check Is_wrapper instead of relying on map_result_tag?
    template<class T,class D> struct Functor<T,D,Construct_tag,true,true> {
	    typedef typename Get_functor<Kernel_base, T>::type B;
	    struct type {
		    B b;
		    type(){}
		    type(Self const&k):b(k){}
		    typedef typename map_result_tag<T>::type result_tag;
		    // FIXME: Self or Derived?
		    typedef typename Get_type<Self,result_tag>::type result_type;
#ifdef CGAL_CXX11
		    template<class...U> result_type operator()(U&&...u)const{
			    return result_type(Eval_functor(),b,internal::Forward_rep()(u)...);
		    }
#else
#define CGAL_VAR(Z,N,_) internal::Forward_rep()(u##N)
#define CGAL_CODE(Z,N,_) template<BOOST_PP_ENUM_PARAMS(N,class U)> result_type \
		    operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,U,const&u))const{ \
			    return result_type(Eval_functor(),b,BOOST_PP_ENUM(N,CGAL_VAR,)); \
		    }
		    BOOST_PP_REPEAT_FROM_TO(1,11,CGAL_CODE,_)
#undef CGAL_CODE
#undef CGAL_VAR
#endif
	    };
    };

};

template < typename Base_ >
struct Cartesian_refcount : public Base_
{
    CGAL_CONSTEXPR Cartesian_refcount(){}
    CGAL_CONSTEXPR Cartesian_refcount(int d):Base_(d){}
    typedef Base_ Kernel_base;
    typedef Cartesian_refcount Self;

    // FIXME: Use object_list, or a list passed as argument, or anything
    // automatic.
    template <class T, class=void> struct Type : Get_type<Base_, T> {};
#define CGAL_Kernel_obj(X,Y) \
    template <class D> struct Type<X##_tag, D> { typedef Ref_count_obj<Cartesian_refcount, X##_tag> type; };

    CGAL_Kernel_obj(Point,point)
    CGAL_Kernel_obj(Vector,vector)
#undef CGAL_Kernel_obj

    template<class T> struct Dispatch {
	    //typedef typename map_functor_type<T>::type f_t;
	    typedef typename map_result_tag<T>::type r_t;
	    enum {
		    is_nul = boost::is_same<typename Get_functor<Kernel_base, T>::type,Null_functor>::value,
		    ret_rcobj = boost::is_same<r_t,Point_tag>::value || boost::is_same<r_t,Vector_tag>::value
	    };
    };

    //Translate the arguments
    template<class T,class D=void,bool=Dispatch<T>::is_nul,bool=Dispatch<T>::ret_rcobj> struct Functor {
	    typedef typename Get_functor<Kernel_base, T>::type B;
	    struct type {
		    B b;
		    type(){}
		    type(Self const&k):b(k){}
		    typedef typename B::result_type result_type;
#ifdef CGAL_CXX11
		    template<class...U> result_type operator()(U&&...u)const{
			    return b(internal::Forward_rep()(u)...);
		    }
#else
		    result_type operator()()const{
			    return b();
		    }
#define CGAL_VAR(Z,N,_) internal::Forward_rep()(u##N)
#define CGAL_CODE(Z,N,_) template<BOOST_PP_ENUM_PARAMS(N,class U)> result_type \
		    operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,U,const&u))const{ \
			    return b(BOOST_PP_ENUM(N,CGAL_VAR,)); \
		    }
		    BOOST_PP_REPEAT_FROM_TO(1,11,CGAL_CODE,_)
#undef CGAL_CODE
#undef CGAL_VAR
#endif
	    };
    };

    //Translate both the arguments and the result
    template<class T,class D,bool b> struct Functor<T,D,true,b> {
	    typedef Null_functor type;
    };

    template<class T,class D> struct Functor<T,D,false,true> {
	    typedef typename Get_functor<Kernel_base, T>::type B;
	    struct type {
		    B b;
		    type(){}
		    type(Self const&k):b(k){}
		    typedef typename map_result_tag<T>::type result_tag;
		    typedef typename Get_type<Self,result_tag>::type result_type;
#ifdef CGAL_CXX11
		    template<class...U> result_type operator()(U&&...u)const{
			    return result_type(Eval_functor(),b,internal::Forward_rep()(u)...);
		    }
#else
		    result_type operator()()const{
			     return result_type(Eval_functor(),b);
		    }
#define CGAL_VAR(Z,N,_) internal::Forward_rep()(u##N)
#define CGAL_CODE(Z,N,_) template<BOOST_PP_ENUM_PARAMS(N,class U)> result_type \
		    operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,U,const&u))const{ \
			    return result_type(Eval_functor(),b,BOOST_PP_ENUM(N,CGAL_VAR,)); \
		    }
		    BOOST_PP_REPEAT_FROM_TO(1,11,CGAL_CODE,_)
#undef CGAL_CODE
#undef CGAL_VAR
#endif
	    };
    };

};

} //namespace CGAL

#if defined(BOOST_MSVC)
#  pragma warning(pop)
#endif

#endif // CGAL_KERNEL_D_CARTESIAN_WRAP_H