summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eo_cxx/eo_promise.hh
blob: 74d662b6153221fbf08488021540bd84fc218b7b (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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
///
/// @file eo_concrete.hh
///

#ifndef EFL_CXX_EO_PROMISE_HH
#define EFL_CXX_EO_PROMISE_HH

#include <Efl.h>

#include <Eina.hh>
#include <Ecore_Manual.hh>

#include <mutex>
#include <condition_variable>

#include <eina_tuple.hh>

namespace efl {

template <typename...Args>
struct shared_future;
  
namespace _impl {

template <typename...Futures>
struct all_result_type;

template <typename...Args>
struct all_result_type<shared_future<Args...>>
{
  typedef shared_future<Args...> type;
};

template <typename...Args1, typename...Args2>
struct all_result_type<shared_future<Args1...>, shared_future<Args2...>>
{
  typedef shared_future<Args1..., Args2...> type;
};

template <typename...Args1, typename...Args2, typename...OtherFutures>
struct all_result_type<shared_future<Args1...>, shared_future<Args2...>, OtherFutures...>
{
  typedef typename all_result_type<shared_future<Args1..., Args2...>, OtherFutures...>::type type;
};

template <typename...Futures>
typename all_result_type<Futures...>::type
all_impl(Futures const& ... futures)
{
  Efl_Future* future = ::efl_future_all_internal(futures.native_handle()..., NULL);
  return typename all_result_type<Futures...>::type{ ::eo_ref(future)};
}

template <typename...Futures>
struct race_result_type;

template <typename...Args>
struct race_result_type<shared_future<Args...>>
{
  typedef shared_future<Args...> type;
};

template <typename T, typename...Args>
struct race_compose_impl;

template <typename T, typename A0, typename...Args>
struct race_compose_impl<T, A0, Args...>
{
  typedef typename std::conditional<eina::_mpl::tuple_contains<A0, T>::value
                                    , typename race_compose_impl<T, Args...>::type
                                    , typename race_compose_impl<typename eina::_mpl::push_back<T, A0>::type, Args...>::type
                                    >::type type;
};

template <typename T>
struct race_compose_impl<T>
{
   typedef T type;
};

template <typename T>
struct variant_from_tuple;

template <typename...Args>
struct variant_from_tuple<std::tuple<Args...>>
{
  typedef eina::variant<Args...> type;
};
  
template <typename...Args>
struct race_variant
{
  typedef typename variant_from_tuple<typename race_compose_impl<std::tuple<>, Args...>::type>::type type;
};
  
template <typename A0>
struct race_result_type<shared_future<A0>>
{
  typedef shared_future<A0> type;
};

template <typename A0>
struct race_result_type<shared_future<eina::variant<A0>>>
{
  typedef shared_future<A0> type;
};
  
template <typename...Args1, typename...Args2>
struct race_result_type<shared_future<Args1...>, shared_future<Args2...>>
{
  typedef typename race_result_type<shared_future<typename race_variant<Args1..., Args2...>::type>>::type type;
};

template <typename...Args1, typename...Args2, typename...OtherFutures>
struct race_result_type<shared_future<Args1...>, shared_future<Args2...>, OtherFutures...>
{
  typedef typename race_result_type<shared_future<typename race_variant<Args1..., Args2...>::type>
                                    , OtherFutures...>::type type;
};

template <typename...Futures>
typename race_result_type<Futures...>::type
race_impl(Futures const& ... futures)
{
  Efl_Future* future = ::efl_future_race_internal(futures.native_handle()..., NULL);
  return typename race_result_type<Futures...>::type{ ::eo_ref(future)};
}
  
template <typename A0, typename F>
typename std::enable_if
<
  !std::is_same<A0, void>::value
  && !std::is_same<typename std::result_of<F(A0)>::type, void>::value
>::type
future_invoke(F f, Eo_Event const* event)
{
   Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);
   try
     {
        typename std::aligned_storage<sizeof(A0), alignof(A0)>::type storage;
        eina::copy_from_c_traits<A0>::copy_to_unitialized
          (static_cast<A0*>(static_cast<void*>(&storage)), info->value);
        auto r = f(*static_cast<A0*>(static_cast<void*>(&storage)));
        typedef decltype(r) result_type;
        typedef typename eina::alloc_to_c_traits<result_type>::c_type c_type;
        c_type* c_value = eina::alloc_to_c_traits<result_type>::copy_alloc(r);
        efl_promise_value_set(info->next, c_value, & eina::alloc_to_c_traits<result_type>::free_alloc);
     }
   catch(...)
     {
     }
}

template <typename A0, typename F>
typename std::enable_if<std::is_same<A0, void>::value>::type
future_invoke(F f, Eo_Event const* event)
{
   Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);
   try
     {
        f();
     }
   catch(...)
     {
     }
}

template <std::size_t N, typename...Args, typename...StorageArgs>
static void future_invoke_impl_read_accessor
  (Eina_Accessor*, std::tuple<StorageArgs...>&, std::tuple<Args...>*, std::true_type)
{
}

template <std::size_t N, typename...Args, typename...StorageArgs>
static void future_invoke_impl_read_accessor
  (Eina_Accessor* accessor
   , std::tuple<StorageArgs...>& storage_tuple
   , std::tuple<Args...>* args
   , std::false_type)
{
   typedef std::tuple<Args...> tuple_type;
   typedef typename std::tuple_element<N, tuple_type>::type type;
   void* value;
   if(eina_accessor_data_get(accessor, N, &value))
     {
        eina::copy_from_c_traits<type>::copy_to_unitialized
          (static_cast<type*>(static_cast<void*>(&std::get<N>(storage_tuple))), value);

        _impl::future_invoke_impl_read_accessor<N+1>
          (accessor, storage_tuple, args
           , std::integral_constant<bool, (N+1 == sizeof...(Args))>());
     }
   else
     {
        std::abort();
        // some error
     }
}
  
template <typename F, typename...Args, std::size_t...I>
void future_invoke_impl(F f, Eo_Event const* event, std::tuple<Args...>* arguments_dummy, eina::index_sequence<I...>)
{
   Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);
   try
     {
        typedef std::tuple<Args...> arguments;
        typedef std::tuple<typename std::aligned_storage<sizeof(Args), alignof(Args)>::type...>
          storage_tuple_type;
        storage_tuple_type storage_tuple;

        future_invoke_impl_read_accessor<0ul>
          (static_cast<Eina_Accessor*>(info->value)
           , storage_tuple
           , arguments_dummy, std::false_type{});

        auto r = f(*static_cast<typename std::tuple_element<I, arguments>::type*>
                   (static_cast<void*>(&std::get<I>(storage_tuple)))...);

        typedef decltype(r) result_type;
        typedef typename eina::alloc_to_c_traits<result_type>::c_type c_type;
        c_type* c_value = eina::alloc_to_c_traits<result_type>::copy_alloc(r);
        efl_promise_value_set(info->next, c_value, & eina::alloc_to_c_traits<result_type>::free_alloc);
     }
   catch(...)
     {
     }
}
  
template <typename A0, typename A1, typename...OtherArgs, typename F>
void
future_invoke(F f, Eo_Event const* event)
{
  std::tuple<A0, A1, OtherArgs...>* p = nullptr;
  _impl::future_invoke_impl(f, event, p, eina::make_index_sequence<sizeof...(OtherArgs) + 2>{});
}
  
}
  
template <typename T>
struct progress;

namespace _impl {

template <typename V = char>
struct wait_state
{
   bool available = false;
   bool has_failed = false;
   std::mutex mutex;
   std::condition_variable cv;
   typename std::aligned_storage<sizeof(V), alignof(V)>::type storage;
   Eina_Error error;
};

static void get_error_cb(void* data, Eo_Event const* event)
{ 
   struct wait_state<>* wait_state = static_cast<struct wait_state<>*>(data);
   Efl_Future_Event_Failure* info = static_cast<Efl_Future_Event_Failure*>(event->info);
   std::unique_lock<std::mutex> l(wait_state->mutex);
   wait_state->error = info->error;
   wait_state->has_failed = true;
   wait_state->available = true;
   wait_state->cv.notify_one();
}
  
struct shared_future_common
{
   explicit shared_future_common(Efl_Future* future)
     : _future(future) {}
   shared_future_common()
     : _future(nullptr) {}
   ~shared_future_common()
   {
      if(_future)
        eo_unref(_future);
   }
   shared_future_common(shared_future_common const& future)
     : _future(eo_ref(future._future))
   {
   }
   shared_future_common& operator=(shared_future_common const& other)
   {
      _self_type tmp(other);
      tmp.swap(*this);
      return *this;
   }
   shared_future_common(shared_future_common&& future)
     : _future(future._future)
   {
      future._future = nullptr;
   }
   shared_future_common& operator=(shared_future_common&& other)
   {
      other.swap(*this);
      return *this;
   }
   void swap(shared_future_common& other)
   {
      std::swap(_future, other._future);
   }
   bool valid() const noexcept
   {
      return _future != nullptr;
   }
   void wait() const
   {
      if(eina_main_loop_is())
        throw std::runtime_error("Deadlock");

      struct wait_state<> wait_state;
      
      efl::ecore::main_loop_thread_safe_call_async
        ([&]
         {
            efl_future_then(this->_future, &wait_success, &wait_success, nullptr, &wait_state);
         });

      std::unique_lock<std::mutex> lock(wait_state.mutex);
      while(!wait_state.available)
        wait_state.cv.wait(lock);
   }
   static void wait_success(void* data, Eo_Event const*)
   {
      struct wait_state<>* wait_state = static_cast<struct wait_state<>*>(data);
      std::unique_lock<std::mutex> l(wait_state->mutex);
      wait_state->available = true;
      wait_state->cv.notify_one();
   }

   typedef Efl_Future* native_handle_type;
   native_handle_type native_handle() const noexcept { return _future; }

   typedef shared_future_common _self_type;
   Efl_Future* _future;
};
  
template <typename T>
struct shared_future_1_type : private shared_future_common
{
   typedef shared_future_common _base_type;

   using _base_type::_base_type;
   using _base_type::swap;
   using _base_type::valid;
   using _base_type::native_handle;
   using _base_type::wait;
   typedef _base_type::native_handle_type native_handle_type;

   T get() const
   {
      if(eina_main_loop_is())
        throw std::runtime_error("Deadlock");

      struct wait_state<T> wait_state;

      efl::ecore::main_loop_thread_safe_call_async
        ([&]
         {
            efl_future_then(this->_future, &get_success, &_impl::get_error_cb, nullptr, &wait_state);
         });

      {
        std::unique_lock<std::mutex> lock(wait_state.mutex);
        while(!wait_state.available)
          wait_state.cv.wait(lock);
      }
      if(wait_state.has_failed)
        EFL_CXX_THROW(eina::system_error(eina::error_code(wait_state.error, eina::eina_error_category()), "EFL Eina Error"));
      return *static_cast<T*>(static_cast<void*>(&wait_state.storage));
   }

   static void get_success(void* data, Eo_Event const* event)
   {
      struct wait_state<T>* wait_state = static_cast<struct wait_state<T>*>(data);
      Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);

      std::unique_lock<std::mutex> l(wait_state->mutex);
      eina::copy_from_c_traits<T>::copy_to_unitialized
        (static_cast<T*>(static_cast<void*>(&wait_state->storage)), info->value);
      wait_state->available = true;
      wait_state->cv.notify_one();
   }

   typedef shared_future_1_type<T> _self_type;
};

template <typename...Args>
struct shared_future_varargs_type : private shared_future_common
{
   typedef shared_future_common _base_type;

   using _base_type::_base_type;
   using _base_type::swap;
   using _base_type::valid;
   using _base_type::native_handle;
   using _base_type::wait;
   typedef _base_type::native_handle_type native_handle_type;

   typedef std::tuple<Args...> tuple_type;

   std::tuple<Args...> get() const
   {
      if(eina_main_loop_is())
        throw std::runtime_error("Deadlock");

      struct wait_state<tuple_type> wait_state;

      efl::ecore::main_loop_thread_safe_call_async
        ([&]
         {
            efl_future_then(this->_future, &get_success, &_impl::get_error_cb, nullptr, &wait_state);
         });

      {
        std::unique_lock<std::mutex> lock(wait_state.mutex);
        while(!wait_state.available)
          wait_state.cv.wait(lock);
      }
      if(wait_state.has_failed)
        EFL_CXX_THROW(eina::system_error(eina::error_code(wait_state.error, eina::eina_error_category()), "EFL Eina Error"));
      return *static_cast<tuple_type*>(static_cast<void*>(&wait_state.storage));
   }

   template <std::size_t N>
   static void read_accessor(Eina_Accessor* accessor
                             , std::tuple<typename std::aligned_storage<sizeof(Args), alignof(Args)>::type...>& storage_tuple
                             , wait_state<tuple_type>* wait_state
                             , std::false_type)
   {
      typedef typename std::tuple_element<N, tuple_type>::type type;
      void* value;
      if(eina_accessor_data_get(accessor, N, &value))
        {
          eina::copy_from_c_traits<type>::copy_to_unitialized
            (static_cast<type*>(static_cast<void*>(&std::get<N>(storage_tuple))), value);

          _self_type::read_accessor<N+1>(accessor, storage_tuple, wait_state
                                         , std::integral_constant<bool, (N+1 == sizeof...(Args))>());
        }
      else
        {
          std::abort();
          // some error
        }
   }

   template <std::size_t N, std::size_t...I>
   static void read_accessor_end(std::tuple<typename std::aligned_storage<sizeof(Args), alignof(Args)>::type...>& storage_tuple
                                 , wait_state<tuple_type>* wait_state
                                 , eina::index_sequence<I...>)
   {
      std::unique_lock<std::mutex> l(wait_state->mutex);

      new (&wait_state->storage) tuple_type{(*static_cast<typename std::tuple_element<I, tuple_type>::type*>
                                             (static_cast<void*>(&std::get<I>(storage_tuple))))...};

      wait_state->available = true;
      wait_state->cv.notify_one();
   }
  
   template <std::size_t N>
   static void read_accessor(Eina_Accessor*
                             , std::tuple<typename std::aligned_storage<sizeof(Args), alignof(Args)>::type...>& storage_tuple
                             , wait_state<tuple_type>* wait_state
                             , std::true_type)
   {
      _self_type::read_accessor_end<N>(storage_tuple, wait_state, eina::make_index_sequence<sizeof...(Args)>{});
   }
  
   static void get_success(void* data, Eo_Event const* event)
   {
      struct wait_state<tuple_type>* wait_state = static_cast<struct wait_state<tuple_type>*>(data);
      Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);

      Eina_Accessor* accessor = static_cast<Eina_Accessor*>(info->value);
      std::tuple<typename std::aligned_storage<sizeof(Args), alignof(Args)>::type...> storage_tuple;

      _self_type::read_accessor<0u>(accessor, storage_tuple, wait_state, std::false_type());
   }
  
   typedef shared_future_varargs_type<Args...> _self_type;
};
  
}

template <typename...Args>
struct shared_future : private std::conditional<sizeof...(Args) == 1, _impl::shared_future_1_type<typename std::tuple_element<0u, std::tuple<Args...>>::type>, _impl::shared_future_varargs_type<Args...>>::type
{
   typedef typename std::conditional<sizeof...(Args) == 1, _impl::shared_future_1_type<typename std::tuple_element<0u, std::tuple<Args...>>::type>, _impl::shared_future_varargs_type<Args...>>::type _base_type;

   using _base_type::_base_type;
   using _base_type::swap;
   using _base_type::valid;
   using _base_type::get;
   using _base_type::wait;
   using _base_type::native_handle;
   typedef typename _base_type::native_handle_type native_handle_type;
};

template <typename...Args, typename Success, typename Error>
shared_future
<
  typename std::enable_if
  <
    !std::is_same<void, typename std::tuple_element<0, std::tuple<Args...>>::type>::value
    && !std::is_same<void, typename std::result_of<Success(Args...)>::type>::value
    , typename std::result_of<Success(Args...)>::type
  >::type
> then(shared_future<Args...> future, Success success_cb, Error error_cb)
{
  struct private_data
  {
    Success success_cb;
    Error error_cb;
    shared_future<Args...> future;
  };
  private_data* pdata = new private_data
    {std::move(success_cb), std::move(error_cb), std::move(future)};
     
  Eo_Event_Cb raw_success_cb =
    [] (void* data, Eo_Event const* event)
    {
       private_data* pdata = static_cast<private_data*>(data);
       try
         {
           _impl::future_invoke<Args...>(pdata->success_cb, event);
           // should value_set the next promise
         }
       catch(...)
         {
           // should fail the next promise
         }
       delete pdata;
    };
  Eo_Event_Cb raw_error_cb =
    [] (void* data, Eo_Event const* event)
    {
       private_data* pdata = static_cast<private_data*>(data);
       Efl_Future_Event_Failure* info = static_cast<Efl_Future_Event_Failure*>(event->info);
       pdata->error_cb(eina::error_code(info->error, eina::eina_error_category()));
       // should error the next promise (or should the promise do that for me automatically?)
       delete pdata;
    };
  
      assert(pdata->future.valid());
  Efl_Future* new_future
    = efl_future_then(pdata->future.native_handle(), raw_success_cb, raw_error_cb, nullptr, pdata);
  return shared_future<typename std::result_of<Success(Args...)>::type>{eo_ref(new_future)};
}

template <typename...Args, typename F>
void then(shared_future<Args...> future, F function)
{
  
}

template <typename...Args1, typename...Args2, typename...Futures>
typename _impl::all_result_type<shared_future<Args1...>, shared_future<Args2...>, Futures...>::type
all(shared_future<Args1...> future1, shared_future<Args2...> future2, Futures...futures)
{
  return _impl::all_impl(future1, future2, futures...);
}

template <typename...Args1, typename...Args2, typename...Futures>
typename _impl::race_result_type<shared_future<Args1...>, shared_future<Args2...>, Futures...>::type
race(shared_future<Args1...> future1, shared_future<Args2...> future2, Futures...futures)
{
  return _impl::race_impl(future1, future2, futures...);
}
  
template <typename...Args>
struct promise
{
  
};
    
}

#endif