proxy.hxx 12.7 KB
Newer Older
1
// Copyright (C) 2008, 2009, 2012, 2013 EPITA Research and Development
2
// Laboratory (LRDE)
Thierry Geraud's avatar
Thierry Geraud committed
3
//
4
// This file is part of Olena.
Thierry Geraud's avatar
Thierry Geraud committed
5
//
6
7
8
9
10
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
Thierry Geraud's avatar
Thierry Geraud committed
11
12
13
14
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
Thierry Geraud's avatar
Thierry Geraud committed
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
Thierry Geraud's avatar
Thierry Geraud committed
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
Thierry Geraud's avatar
Thierry Geraud committed
26
27
28
29
30
31
32
33
34
35
36
37
38

#ifndef MLN_CORE_CONCEPT_PROXY_HXX
# define MLN_CORE_CONCEPT_PROXY_HXX

# include <mln/core/internal/force_exact.hh>
# include <mln/metal/unqualif.hh>
# include <mln/metal/is_a.hh>
# include <mln/metal/if.hh>
# include <mln/metal/is_const.hh>
# include <mln/metal/const.hh>
# include <mln/metal/unconst.hh>
# include <mln/metal/is_not_ref.hh>
# include <mln/metal/ref.hh>
39
# include <mln/metal/fix_return.hh>
Thierry Geraud's avatar
Thierry Geraud committed
40
41
42
43
44


namespace mln
{

45
46
  /// Fwd decls.
  /// \{
Thierry Geraud's avatar
Thierry Geraud committed
47
48
  template <typename E> struct Proxy;

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  namespace internal
  {
    template <typename T>
    struct unproxy_rec_;
  }

  template <typename T>
  typename mln::internal::unproxy_rec_<T>::ret
  unproxy_rec(T& t);

  template <typename T>
  typename mln::internal::unproxy_rec_<const T>::ret
  unproxy_rec(const T& t);
  /// \}

Thierry Geraud's avatar
Thierry Geraud committed
64
65
66
67
68
69
70
71
72
73
74
75
76

  namespace internal
  {


    // helper_unprox_binop

    template <bool b, typename P> struct helper_unprox_if;

    template <typename P>
    struct helper_unprox_if< true, P >
    {
      typedef mlc_unqualif(mln_q_subject(P)) ret;
77
      static mlc_const_return(mln_q_subject(P)) on(const Proxy<P>& p);
Thierry Geraud's avatar
Thierry Geraud committed
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
    };

    template <typename P>
    struct helper_unprox_if< false, P >
    {
      typedef P ret;
      static const P& on(const Proxy<P>& p);
    };

    template <typename L, typename R>
    struct helper_unprox_binop
    {
      enum { L_level = L::proxy_level,
	     R_level = R::proxy_level }; // To prevent a weird g++ warning.
      typedef internal::helper_unprox_if<(L_level >= R_level), L> L_helper;
      typedef internal::helper_unprox_if<(R_level >= L_level), R> R_helper;
      typedef typename L_helper::ret L_ret;
      typedef typename R_helper::ret R_ret;
    };



    template <typename O, bool is_proxy>
    struct helper_unproxy_rec;


    // P is a proxy type.

    template <typename P>
    struct helper_unproxy_rec< P, true >
    {
109
      typedef mln_exact(P)::HOT_actual_subject ret;
Thierry Geraud's avatar
Thierry Geraud committed
110
111
112
113
114
115
      static ret on(P& p);
    };

    template <typename P>
    struct helper_unproxy_rec< const P, true >
    {
116
      typedef mlc_const(mln_exact(P)::HOT_actual_subject) ret;
Thierry Geraud's avatar
Thierry Geraud committed
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
      static ret on(const P& p);
    };

    // O is *not* a proxy type.

    template <typename O>
    struct helper_unproxy_rec< const O, false >
    {
      typedef const mln_exact(mlc_unqualif(O))& ret;
      static ret on(const O& obj);
    };

    template <typename O>
    struct helper_unproxy_rec< O, false >
    {
      typedef mln_exact(mlc_unqualif(O))& ret;
      static ret on(O& obj);
    };


    template <typename T>
    struct unproxy_rec_
    {
      enum { is_proxy = mlc_is_a(T, Proxy)::value };
      typedef helper_unproxy_rec<T, is_proxy> helper;
      typedef typename helper::ret ret;
      static ret on(T& t);
    };



    
    // External way of getting an address of an object from/through a
    // proxy.  This is a recursive implementation since we can have a
    // proxy of proxy, etc.

    // Case 1: Not found so unproxy_ (once).

    template <typename T, typename P>
    void get_adr(const T *& ptr, const Proxy<P>& p);

    template <typename T, typename P>
    void get_adr(      T *& ptr,       Proxy<P>& p);

    // Case 2: Found.  (Note that T can also be a Proxy.)

    template <typename T>
    void get_adr(const T *& ptr, const Object<T>& obj);

    template <typename T>
    void get_adr(      T *& ptr,       Object<T>& obj);

    template <typename T>
    void get_adr(      T *& ptr,       T& t);

    // Case 3: Fail to found!

    template <typename T, typename O>
    void get_adr(const T *& ptr, const Object<O>&);

    template <typename T, typename O>
    void get_adr(      T *& ptr,       Object<O>&);



    // Every proxied class should derive from subject_impl.  The class
    // below is provided to be specialized so that an effective
    // implementation (with the interface of the proxied class) can
    // equip proxy classes.

    template <typename Subject, typename E>
    struct subject_impl
    {
    };

    template <typename S, typename E>
    struct subject_impl< S&, E >; // Safety.




    template <typename prox>
    struct helper_subject_of
    {
      typedef mlc_unqualif(prox)::q_subject subj;
      // We have the proxy type (prox) and its subject type (subj).
      // Both can be qualified with 'const' and/or '&'.  We have to
      // propagate when possible the proper qualifications to the
      // resulting subject type.

      // The rules are:
      // (1) the subject is constant if either prox or subj is const
      //     ---so we have to propagate the possible constness of
      //        prox;
      typedef mlc_if(mlc_is_const(prox), mlc_const(subj), subj) temp;

      // (2) the subject is a reference if both prox and subj are
      //     references---so we have to remove the possible & if prox
      //     is not a reference.
      typedef mlc_if(mlc_is_not_ref(prox), mlc_unref(temp), temp) ret;

      // +-------------+---------+-------------+-------------+----------------+
      // | prox / subj | const S |      S      |  const S&   |       S&       |
      // +-------------+---------+-------------+-------------+----------------+
      // | const P     | const S | const S (1) | const S (2) | const S  (1,2) |
      // | P           | const S | S           | const S (2) | S          (2) |
      // | const P&    | const S | const S (1) | const S&    | const S&   (1) |
      // | P&          | const S | S           | const S&    | S&         (*) |
      // +-------------+---------+-------------+-------------+----------------+
    };


    template <typename S> struct helper_subject_impl           { typedef const S ret; };
    template <typename S> struct helper_subject_impl<S&>       { typedef       S ret; }; // (*)
    template <typename S> struct helper_subject_impl<const S&> { typedef const S ret; };
    template <typename S> struct helper_subject_impl<const S>  { typedef const S ret; };

    // FIXME: I don't know yet how to deal with these cases:
    template <typename S> struct helper_subject_impl<S*>;
    template <typename S> struct helper_subject_impl<S*const>;


    // A proxy should convert towards its subject.  And, if we have a
    // proxy of proxy, it should also convert towards its subject of
    // subject, and so on.  It leads to a recursive implementation
    // where conversions are automatically obtained through
    // inheritance.
    //
    // E is a Proxy type; Subject is its subject type.

    template <typename Subject, typename E> struct get_proxy_impl;

    template <typename Subject, typename E, bool rec = true>
    struct helper_get_proxy_impl : get_proxy_impl< typename helper_subject_of<Subject>::ret,
						   E > // Rec.
    {
    private:
      typedef mlc_unqualif(Subject) unq_subject_;
    public:
      enum { proxy_level = unq_subject_::proxy_level + 1};
    };

    template <typename Subject, typename E>
    struct helper_get_proxy_impl< Subject, E, false > // Stop rec.
      :
      subject_impl< typename helper_subject_impl<Subject>::ret,
		    E >
    {
      enum { proxy_level = 1 };

      // FIXME: HOT: test code below

      typedef Subject HOT_actual_subject;

      HOT_actual_subject            get_subject();
272
      mlc_const_return(HOT_actual_subject) get_subject() const;
Thierry Geraud's avatar
Thierry Geraud committed
273
274
275
276
277
278
    };

    template <typename Subject, typename E>
    struct get_proxy_impl : helper_get_proxy_impl< Subject, E,
						   mlc_is_a(Subject, Proxy)::value >
    {
279
280
    public:
      operator mlc_fix_return(mlc_const_return(Subject)) () const;
281
      operator mlc_fix_return(Subject) ();
Thierry Geraud's avatar
Thierry Geraud committed
282
283
284
285
286
287
288
289
290
291
292
293
    };

    template <typename Subject, typename E>
    struct proxy_impl : get_proxy_impl< Subject, E>
    {
      typedef Subject q_subject;

      // HERE: NEW
      q_subject unproxy_()
      {
	return mln::internal::force_exact<E>(*this).subj_();
      }
294
      mlc_const_return(q_subject) unproxy_() const
Thierry Geraud's avatar
Thierry Geraud committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
      {
	E& self_ = mln::internal::force_exact<E>(*this);
	return self_.subj_();
      }
    };




# ifndef MLN_INCLUDE_ONLY


    // helper_unprox_if

    template <typename P>
    inline
311
    mlc_const_return(mln_q_subject(P))
Thierry Geraud's avatar
Thierry Geraud committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
    helper_unprox_if< true, P >::on(const Proxy<P>& p)
    {
      return exact(p).unproxy_();
    }

    template <typename P>
    inline
    const P&
    helper_unprox_if< false, P >::on(const Proxy<P>& p)
    {
      return exact(p);
    }

    template <typename P>
    inline
    mln_exact(P)::HOT_actual_subject
    helper_unproxy_rec< P, true >::on(P& p)
    {
      return unproxy_rec(exact(p).unproxy_());
    }

    // helper_unprox_rec

    template <typename P>
    inline
    mlc_const(mln_exact(P)::HOT_actual_subject)
    helper_unproxy_rec< const P, true >::on(const P& p)
    {
      return unproxy_rec(exact(p).unproxy_());
    }

    template <typename O>
    inline
    mln_exact(mlc_unqualif(O))&
    helper_unproxy_rec< O, false >::on(O& obj)
    {
      return exact(obj);
    }

    template <typename O>
    inline
    const mln_exact(mlc_unqualif(O))&
    helper_unproxy_rec< const O, false >::on(const O& obj)
    {
      return exact(obj);
    }

    // unproxy_rec_

    template <typename T>
    inline
    typename unproxy_rec_<T>::ret
    unproxy_rec_<T>::on(T& t)
    {
      typedef typename unproxy_rec_<T>::helper helper;
      return helper::on(t);
    }


    // helper_get_proxy_impl<..>::get_subject()

    template <typename Subject, typename E>
    inline
    typename helper_get_proxy_impl< Subject, E, false >::HOT_actual_subject
    helper_get_proxy_impl< Subject, E, false >::get_subject()
    {
      return unproxy_rec(mln::internal::force_exact<E>(*this));
    }

    template <typename Subject, typename E>
    inline
383
    typename mln::metal::const_return_< typename helper_get_proxy_impl< Subject, E, false >::HOT_actual_subject >::ret
Thierry Geraud's avatar
Thierry Geraud committed
384
385
386
387
388
389
390
391
392
393
    helper_get_proxy_impl< Subject, E, false >::get_subject() const
    {
      return unproxy_rec(mln::internal::force_exact<const E>(*this));
    }


    // get_proxy_impl<..>::operator Subject()

    template <typename Subject, typename E>
    inline
394
    get_proxy_impl<Subject, E>::operator mlc_fix_return(mlc_const_return(Subject)) () const
Thierry Geraud's avatar
Thierry Geraud committed
395
396
397
398
399
400
401
402
403
404
405
406
    {
      return mln::internal::force_exact<const E>(*this).unproxy_();
      
      // Technical note:
      //
      // The code above seems more effective than the one below:
      // 	const Subject* adr;
      // 	get_adr(adr, mln::internal::force_exact<const E>(*this));
      // 	mln_postcondition(adr != 0);
      // 	return *adr;
    }

407
408
409
410
411
412
413
    template <typename Subject, typename E>
    inline
    get_proxy_impl<Subject, E>::operator mlc_fix_return(Subject) ()
    {
      return mln::internal::force_exact<E>(*this).unproxy_();
    }

Thierry Geraud's avatar
Thierry Geraud committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

    // get_adr

    template <typename T, typename P>
    inline
    void get_adr(const T *& ptr, const Proxy<P>& p)
    {
      get_adr(ptr, exact(p).unproxy_());
    }

    template <typename T, typename P>
    inline
    void get_adr(      T *& ptr,       Proxy<P>& p)
    {
      get_adr(ptr, exact(p).unproxy_());
    }

    template <typename T>
    inline
    void get_adr(const T *& ptr, const Object<T>& obj)
    {
      ptr = & exact(obj);
    }

    template <typename T>
    inline
    void get_adr(      T *& ptr,       Object<T>& obj)
    {
      ptr = & exact(obj);
    }

    template <typename T>
    inline
    void get_adr(      T *& ptr,       T& t)
    {
      ptr = & t;
    }

    template <typename T, typename O>
    inline
    void get_adr(const T *& ptr, const Object<O>&)
    {
      ptr = 0;
    }

    template <typename T, typename O>
    inline
    void get_adr(      T *& ptr,       Object<O>&)
    {
      ptr = 0;
    }

# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::internal

} // end of namespace mln


#endif // ! MLN_CORE_CONCEPT_PROXY_HXX