summaryrefslogtreecommitdiff
path: root/src/bindings/eo_cxx/eo_cxx_interop.hh
blob: 18af392c837706a29322c38abe7f2e346cb5d2e6 (plain) (blame)
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
306
307
308
309
310
311
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
474
475
476
477
478
479
480
481
482
483
484

#ifndef EFL_EOLIAN_INTEROP_HH
#define EFL_EOLIAN_INTEROP_HH

#include <string>
#include <tuple>
#include <utility>
#include <type_traits>
#include <initializer_list>

#include <Eina.hh>
#include <Eo.hh>

#include "eo_concrete.hh"

namespace efl { namespace eolian {

//// From C++ to C

inline const char*
to_c(std::string const& x)
{
   return x.c_str();
}

inline const char*
to_c(eina::optional<std::string> const& x)
{
   if (!x)
     return nullptr;
   return x->c_str();
}

inline const char*
to_c(eina::string_view const& x)
{
   return x.data();
}

inline const char*
to_c(eina::optional<eina::string_view> const& x)
{
   if (!x)
     return nullptr;
   return x->data();
}

inline const char*
to_c(efl::eina::stringshare const& x)
{
   return x.c_str();
}

inline const char*
to_c(eina::optional<efl::eina::stringshare> const& x)
{
   if (!x)
     return nullptr;
   return x->c_str();
}

inline Eina_Bool
to_c(bool x)
{
   return x ? EINA_TRUE : EINA_FALSE;
}

inline Eina_Bool*
to_c(bool* x)
{
   static_assert(sizeof(bool) == sizeof(Eina_Bool), "");
   return reinterpret_cast<Eina_Bool*>(x);
}

template <typename T>
T to_c(T const& v, typename std::enable_if<!std::is_convertible<T*, ::efl::eo::concrete*>::value>::type* = 0)
{
   return v;
}

template <typename T>
Eo* to_c(T const& v, typename std::enable_if<std::is_convertible<T*, ::efl::eo::concrete*>::value>::type* = 0)
{
   return v._eo_ptr();
}

template <typename T>
Eo* to_c(eina::optional<T> const& v, typename std::enable_if<std::is_convertible<T*, ::efl::eo::concrete*>::value>::type* = 0)
{
   if (!v)
     return nullptr;
   return v->_eo_ptr();
}

template <typename T>
Eo** to_c(T* v, typename std::enable_if<std::is_convertible<T*, ::efl::eo::concrete*>::value>::type* = 0)
{
   static_assert(sizeof(T) == sizeof(Eo*), "");
   return static_cast<Eo**>(static_cast<void*>(v));
}

template <typename R, typename T>
R to_native(T& v)
{
   static_assert(sizeof(T) == sizeof(R), "");
   return v.native_handle();
}

template <typename R, typename T>
R to_native(eina::optional<T>& v)
{
   static_assert(sizeof(T) == sizeof(R), "");
   if (!v)
     return nullptr;
   return v->native_handle();
}

template <typename R, typename T>
R to_native(T* v)
{
  static_assert(sizeof(T) == sizeof(typename std::remove_pointer<R>::type), "");
  return static_cast<R>(static_cast<void*>(v));
}

    
//// From C to C++

template <typename T>
struct tag {};

template <typename T, typename U, typename O>
T to_cxx(U object, O o);

// XXX
inline void*
to_cxx(void *x, std::tuple<std::false_type>, tag<void*>)
{
   return x;
}

// XXX
inline const void*
to_cxx(const void *x, std::tuple<std::false_type>, tag<const void*>)
{
   return x;
}

template <typename T>
inline T
to_cxx(Eo* x, std::tuple<std::true_type>, tag<T>)
{
   return T(x);
}

template <typename T>
inline T
to_cxx(Eo* x, std::tuple<std::false_type>, tag<T>)
{
   return T(::eo_ref(x));
}

template <typename T>
inline eina::optional<T>
to_cxx(Eo* x, std::tuple<std::true_type>, tag<eina::optional<T> >)
{
   if (!x)
     return nullptr;
   return T(x);
}

template <typename T>
inline eina::optional<T>
to_cxx(Eo* x, std::tuple<std::false_type>, tag<eina::optional<T> >)
{
   if (!x)
     return nullptr;
   return T(::eo_ref(x));
}

template <typename T>
inline T
to_cxx(Eo** x, std::tuple<std::false_type>, tag<T>)
{
   static_assert(sizeof(Eo*) == sizeof(typename std::remove_pointer<T>::type), "");
   return static_cast<T>((static_cast<void*>(x)));
}

#ifdef _EVAS_H
template <typename T>
Evas_Object_Textblock_Node_Format *
to_cxx(Evas_Object_Textblock_Node_Format* x, std::tuple<std::false_type>, tag<T>)
{
   return x; // XXX
}
#endif

inline bool
to_cxx(Eina_Bool x, std::tuple<std::false_type>, tag<bool>)
{
   return !!x;
}

inline std::string
to_cxx(const char* x, std::tuple<std::false_type>, tag<std::string>)
{
   return std::string(x);
}

inline eina::optional<std::string>
to_cxx(const char* x, std::tuple<std::false_type>, tag<eina::optional<std::string> >)
{
   if (!x)
     return nullptr;
   return std::string(x);
}

inline eina::string_view
to_cxx(const char* x, std::tuple<std::false_type>, tag<eina::string_view>)
{
   return eina::string_view(x);
}

inline eina::optional<eina::string_view>
to_cxx(const char* x, std::tuple<std::false_type>, tag<eina::optional<eina::string_view> >)
{
   if (!x)
     return nullptr;
   return eina::string_view(x);
}

template <typename T, typename Enable = void>
struct traits
{
   typedef T type;
};

template <typename T>
struct traits
 <T, typename std::enable_if<std::is_base_of<::efl::eo::concrete, T>::value>::type>
{
   typedef Eo* type;
};

template <typename T>
struct traits
 <T, typename std::enable_if<std::is_base_of<std::basic_string<char>, T>::value>::type>
{
   typedef const char* type;
};

template <typename T>
struct traits
 <T, typename std::enable_if<std::is_base_of<::efl::eina::basic_string_view<char>, T>::value>::type>
{
   typedef const char* type;
};

template <typename T, typename ...Args>
inline efl::eina::range_list<T const>
to_cxx(const Eina_List* x, std::tuple<std::false_type, Args...>, tag< efl::eina::range_list<T> >)
{
   return efl::eina::range_list<T const> {x};
}

template <typename T, typename ...Args>
inline eina::optional<efl::eina::range_list<T const> >
to_cxx(const Eina_List* x, std::tuple<std::false_type, Args...>, tag< eina::optional<efl::eina::range_list<T> > >)
{
   if (!x)
     return nullptr;
   return efl::eina::range_list<T const> {x};
}

template <typename T, typename ...Args>
inline efl::eina::range_list<T>
to_cxx(Eina_List* x, std::tuple<std::false_type, Args...>, tag< efl::eina::range_list<T> >)
{
   return efl::eina::range_list<T>{x};
}

template <typename T, typename ...Args>
inline eina::optional<efl::eina::range_list<T> >
to_cxx(Eina_List* x, std::tuple<std::false_type, Args...>, tag< eina::optional<efl::eina::range_list<T> > >)
{
   if (!x)
     return nullptr;
   return efl::eina::range_list<T>{x};
}

template <typename T, typename ...Args>
inline efl::eina::list<T>
to_cxx(Eina_List* x, std::tuple<std::true_type, Args...>, tag< efl::eina::list<T> >)
{
   return efl::eina::list<T> {x};
}

template <typename T, typename ...Args>
inline eina::optional<efl::eina::list<T> >
to_cxx(Eina_List* x, std::tuple<std::true_type, Args...>, tag< eina::optional<efl::eina::list<T> > >)
{
   if (!x)
     return nullptr;
   return efl::eina::list<T> {x};
}

inline eina::stringshare
to_cxx(Eina_Stringshare const* x, const std::false_type, tag<eina::stringshare>)
{
   return ::eina_stringshare_ref(x);
}

inline eina::optional<eina::stringshare>
to_cxx(Eina_Stringshare const* x, const std::false_type, tag<eina::optional<eina::stringshare> >)
{
   if (!x)
     return nullptr;
   return eina::stringshare(::eina_stringshare_ref(x));
}

template <typename T, typename ...Args>
inline efl::eina::accessor<T>
to_cxx(Eina_Accessor* x, std::tuple<std::false_type, Args...>, tag< efl::eina::accessor<T> >)
{
   return efl::eina::accessor<T>(x);
}

template <typename T, typename ...Args>
inline eina::optional<efl::eina::accessor<T> >
to_cxx(Eina_Accessor* x, std::tuple<std::false_type, Args...>, tag< eina::optional<efl::eina::accessor<T> > >)
{
   if (!x)
     return nullptr;
   return efl::eina::accessor<T>(x);
}

template <typename T, typename ...Args>
inline efl::eina::iterator<T>
to_cxx(Eina_Iterator* x, std::tuple<std::false_type, Args...>, tag< efl::eina::iterator<T> >)
{
   return efl::eina::iterator<T>(x);
}

template <typename T, typename ...Args>
inline eina::optional<efl::eina::iterator<T> >
to_cxx(Eina_Iterator* x, std::tuple<std::false_type, Args...>, tag< eina::optional<efl::eina::iterator<T> > >)
{
   if (!x)
     return nullptr;
   return efl::eina::iterator<T>(x);
}

template <typename T, typename ...Args>
T
to_cxx(Eo const* x, std::tuple<std::false_type, Args...>, tag< T >
       , typename std::enable_if<std::is_base_of<::efl::eo::concrete, T>::value>* = 0)
{
   // Workaround for erroneous constness
   return T{ ::eo_ref(const_cast<Eo*>(x))};
}

template <typename T, typename ...Args>
eina::optional<T>
to_cxx(Eo const* x, std::tuple<std::false_type, Args...>, tag< eina::optional<T> >
       , typename std::enable_if<std::is_base_of<::efl::eo::concrete, T>::value>* = 0)
{
   // Workaround for erroneous constness
   if (!x)
     return nullptr;
   return T{ ::eo_ref(const_cast<Eo*>(x))};
}

template <typename T, typename ...Args>
T
to_cxx(Eo const* x, std::tuple<std::true_type, Args...>, tag< T >
       , typename std::enable_if<std::is_base_of<::efl::eo::concrete, T>::value>* = 0)
{
   // Workaround for erroneous constness
   return T{const_cast<Eo*>(x)};
}

template <typename T, typename ...Args>
eina::optional<T>
to_cxx(Eo const* x, std::tuple<std::true_type, Args...>, tag< eina::optional<T> >
       , typename std::enable_if<std::is_base_of<::efl::eo::concrete, T>::value>* = 0)
{
   // Workaround for erroneous constness
   if (!x)
     return nullptr;
   return T{const_cast<Eo*>(x)};
}

template <typename T, typename U, typename O>
T to_cxx(U object, O o)
{
   return to_cxx(object, o, tag<T>());
}

//// Callbacks

template <typename F, typename R, typename V, typename... Args>
R funcall(V* data, Args... args)
{
   F const* f = static_cast<F const*>(data);
   return (*f)(args...);
}

template <typename T>
struct callback_result_type;

template <typename R, typename... Args>
struct callback_result_type<R(*)(Args...)>
{
   typedef R type;
};

template <typename R>
struct callback_args_type;

template <typename R, typename... Args>
struct callback_args_type<R(*)(Args...)>
{
   typedef std::tuple<Args...> type;
};

template <typename C, typename F, typename R, typename V, typename... Args>
C get_callback_impl(tag<std::tuple<V*, Args...> >)
{
   static_assert(std::is_same<void, typename std::remove_cv<V>::type>::value,
                 "First argument of callback should be void* or const void*");
   return static_cast<C>(&eolian::funcall<F, R, V, Args...>);
}

template <typename C, typename F>
C get_callback()
{
   return get_callback_impl<C, F, typename callback_result_type<C>::type>
     (tag<typename callback_args_type<C>::type>());
}

template <typename F>
Eina_Bool free_callback_callback(void* data, ::Eo_Event const*)
{
   delete static_cast<F*>(data);
   return EO_CALLBACK_CONTINUE;
}

template <typename... Fs>
inline
void register_ev_del_free_callback(Eo* eoptr, Fs&&... fs)
{
   std::initializer_list<int const> const v {(fs.register_ev_del_free_callback(eoptr), 0)...};
   (void) v; (void) eoptr;
}

template <typename F>
inline
std::vector<F>& get_static_callback_vector()
{
   static std::vector<F> vec;
   return vec;
}

template <typename F>
inline
F* alloc_static_callback(F&& f)
{
   get_static_callback_vector<F>().push_back(std::forward<F>(f));
   return &(get_static_callback_vector<F>().back());
}

/// Miscellaneous

template <typename... Fs>
inline
void call_ctors(Eo* _obj_eoid, Fs&&... fs)
{
   std::initializer_list<int const> const v {(fs(_obj_eoid), 0)...};
   (void) v;
   (void) _obj_eoid;
}

} } // namespace efl { namespace eolian {

#endif // EFL_EOLIAN_INTEROP_HH