summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-08-16 23:26:52 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-08-16 23:26:52 -0300
commitd6bde682e981738e416d332d30eff90be6c82cbe (patch)
tree01d3d15b342edffc5de71175c7ee503429571bab
parentdb5de2f63b7a1f8413c0694315eb628b23622e5f (diff)
eo-cxx: Add race promises through eina::variantdevs/felipealmeida/promises-cxx
-rw-r--r--src/bindings/cxx/eina_cxx/eina_copy_traits.hh23
-rw-r--r--src/bindings/cxx/eo_cxx/eo_promise.hh50
2 files changed, 68 insertions, 5 deletions
diff --git a/src/bindings/cxx/eina_cxx/eina_copy_traits.hh b/src/bindings/cxx/eina_cxx/eina_copy_traits.hh
index 0db8bd85b4..e6e0164e76 100644
--- a/src/bindings/cxx/eina_cxx/eina_copy_traits.hh
+++ b/src/bindings/cxx/eina_cxx/eina_copy_traits.hh
@@ -7,6 +7,15 @@
7 7
8namespace efl { namespace eina { 8namespace efl { namespace eina {
9 9
10namespace _impl {
11template<bool...> struct bool_pack;
12template <bool...Args>
13struct and_ : std::is_same<bool_pack<Args..., true>, bool_pack<true, Args...>> {};
14}
15
16template <typename... Args>
17struct variant;
18
10template <typename T, typename Enable = void> 19template <typename T, typename Enable = void>
11struct copy_from_c_traits; 20struct copy_from_c_traits;
12 21
@@ -19,6 +28,18 @@ struct copy_from_c_traits<T, typename std::enable_if<std::is_fundamental<T>::val
19 } 28 }
20}; 29};
21 30
31template <typename...Args>
32struct copy_from_c_traits<eina::variant<Args...>,
33 typename std::enable_if<_impl::and_<std::is_fundamental<Args>::value...>::value>::type>
34{
35 template <typename T>
36 static void copy_to_unitialized(eina::variant<Args...>* storage, T const* data)
37 {
38 new (storage) eina::variant<Args...>{*data};
39 }
40};
41
42
22template <typename T, typename Enable = void> 43template <typename T, typename Enable = void>
23struct alloc_to_c_traits; 44struct alloc_to_c_traits;
24 45
@@ -37,7 +58,7 @@ struct alloc_to_c_traits<T, typename std::enable_if<std::is_fundamental<T>::valu
37 ::free(data); 58 ::free(data);
38 } 59 }
39}; 60};
40 61
41} } 62} }
42 63
43#endif 64#endif
diff --git a/src/bindings/cxx/eo_cxx/eo_promise.hh b/src/bindings/cxx/eo_cxx/eo_promise.hh
index 74d662b615..666cde36f0 100644
--- a/src/bindings/cxx/eo_cxx/eo_promise.hh
+++ b/src/bindings/cxx/eo_cxx/eo_promise.hh
@@ -125,6 +125,50 @@ race_impl(Futures const& ... futures)
125 Efl_Future* future = ::efl_future_race_internal(futures.native_handle()..., NULL); 125 Efl_Future* future = ::efl_future_race_internal(futures.native_handle()..., NULL);
126 return typename race_result_type<Futures...>::type{ ::eo_ref(future)}; 126 return typename race_result_type<Futures...>::type{ ::eo_ref(future)};
127} 127}
128
129template <typename T, typename Enabler = void>
130struct future_copy_traits
131{
132 static void copy(T* storage, Efl_Future_Event_Success const* info)
133 {
134 eina::copy_from_c_traits<T>::copy_to_unitialized
135 (storage, info->value);
136 }
137};
138
139template <typename...Args>
140struct future_copy_traits<eina::variant<Args...>>
141{
142 template <std::size_t I>
143 static void copy_impl(eina::variant<Args...>*, void const*, int, std::integral_constant<std::size_t, I>
144 , std::integral_constant<std::size_t, I>)
145 {
146 std::abort();
147 }
148
149 template <std::size_t I, std::size_t N>
150 static void copy_impl(eina::variant<Args...>* storage, void const* value, int index, std::integral_constant<std::size_t, I>
151 , std::integral_constant<std::size_t, N> max
152 , typename std::enable_if<I != N>::type* = 0)
153 {
154 if(I == index)
155 {
156 eina::copy_from_c_traits<eina::variant<Args...>>::copy_to_unitialized
157 (storage, static_cast<typename std::tuple_element<I, std::tuple<Args...>>::type const*>
158 (static_cast<void const*>(value)));
159 }
160 else
161 copy_impl(storage, value, index, std::integral_constant<std::size_t, I+1>{}, max);
162 }
163
164 static void copy(eina::variant<Args...>* storage, Efl_Future_Event_Success const* other_info)
165 {
166 Efl_Future_Race_Success const* info = static_cast<Efl_Future_Race_Success const*>
167 (static_cast<void const*>(other_info));
168 copy_impl(storage, info->value, info->index, std::integral_constant<std::size_t, 0ul>{}
169 , std::integral_constant<std::size_t, sizeof...(Args)>{});
170 }
171};
128 172
129template <typename A0, typename F> 173template <typename A0, typename F>
130typename std::enable_if 174typename std::enable_if
@@ -138,8 +182,7 @@ future_invoke(F f, Eo_Event const* event)
138 try 182 try
139 { 183 {
140 typename std::aligned_storage<sizeof(A0), alignof(A0)>::type storage; 184 typename std::aligned_storage<sizeof(A0), alignof(A0)>::type storage;
141 eina::copy_from_c_traits<A0>::copy_to_unitialized 185 future_copy_traits<A0>::copy(static_cast<A0*>(static_cast<void*>(&storage)), info);
142 (static_cast<A0*>(static_cast<void*>(&storage)), info->value);
143 auto r = f(*static_cast<A0*>(static_cast<void*>(&storage))); 186 auto r = f(*static_cast<A0*>(static_cast<void*>(&storage)));
144 typedef decltype(r) result_type; 187 typedef decltype(r) result_type;
145 typedef typename eina::alloc_to_c_traits<result_type>::c_type c_type; 188 typedef typename eina::alloc_to_c_traits<result_type>::c_type c_type;
@@ -375,8 +418,7 @@ struct shared_future_1_type : private shared_future_common
375 Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info); 418 Efl_Future_Event_Success* info = static_cast<Efl_Future_Event_Success*>(event->info);
376 419
377 std::unique_lock<std::mutex> l(wait_state->mutex); 420 std::unique_lock<std::mutex> l(wait_state->mutex);
378 eina::copy_from_c_traits<T>::copy_to_unitialized 421 _impl::future_copy_traits<T>::copy(static_cast<T*>(static_cast<void*>(&wait_state->storage)), info);
379 (static_cast<T*>(static_cast<void*>(&wait_state->storage)), info->value);
380 wait_state->available = true; 422 wait_state->available = true;
381 wait_state->cv.notify_one(); 423 wait_state->cv.notify_one();
382 } 424 }