summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/ecore_cxx
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-08-07 16:52:48 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-09-11 23:44:05 -0300
commita63cfcafc7f8524dfad4e1bba763fb17c69d50e1 (patch)
treeadc59470b9cb5f6458351842bcc82de523dec247 /src/bindings/cxx/ecore_cxx
parent98229bc5d6cf1ebe11af4bb05a32f2a30b470f6e (diff)
eolian-cxx: Implement future template class for C++
Diffstat (limited to 'src/bindings/cxx/ecore_cxx')
-rw-r--r--src/bindings/cxx/ecore_cxx/Ecore.hh164
-rw-r--r--src/bindings/cxx/ecore_cxx/Ecore_Manual.hh176
2 files changed, 177 insertions, 163 deletions
diff --git a/src/bindings/cxx/ecore_cxx/Ecore.hh b/src/bindings/cxx/ecore_cxx/Ecore.hh
index 75ebc3e26a..bd2cd7e40a 100644
--- a/src/bindings/cxx/ecore_cxx/Ecore.hh
+++ b/src/bindings/cxx/ecore_cxx/Ecore.hh
@@ -13,168 +13,6 @@
13#ifdef EFL_BETA_API_SUPPORT 13#ifdef EFL_BETA_API_SUPPORT
14#include <Ecore.eo.hh> 14#include <Ecore.eo.hh>
15#endif 15#endif
16 16#include <Ecore_Manual.hh>
17namespace efl { namespace ecore {
18
19template <typename T>
20struct _identity
21{
22 typedef T type;
23};
24
25template <typename F>
26void _ecore_main_loop_thread_safe_call_async_callback(void* data)
27{
28 std::unique_ptr<F> f (static_cast<F*>(data));
29 try
30 {
31 (*f)();
32 }
33 catch(std::bad_alloc const& e)
34 {
35 eina_error_set(ENOMEM);
36 }
37 catch(std::system_error const& e)
38 {
39 efl::eina::set_error_code(e.code());
40 }
41 catch(...)
42 {
43 eina_error_set( efl::eina::unknown_error() );
44 }
45}
46
47template <typename T>
48struct _return_buffer
49{
50 typename std::aligned_storage<sizeof(T),std::alignment_of<T>::value>::type buffer;
51};
52
53template <>
54struct _return_buffer<void>
55{
56};
57
58template <typename F>
59struct _data
60{
61 F& f;
62 std::exception_ptr exception;
63 typedef typename std::result_of<F()>::type result_type;
64 _return_buffer<result_type> return_buffer;
65};
66
67template <typename F>
68void* _ecore_main_loop_thread_safe_call_sync_callback_aux(_data<F>* d, _identity<void>)
69{
70 d->f();
71 if(eina_error_get())
72 d->exception = make_exception_ptr(std::system_error(efl::eina::get_error_code()));
73 return 0;
74}
75
76template <typename F, typename R>
77void* _ecore_main_loop_thread_safe_call_sync_callback_aux(_data<F>* d, _identity<R>)
78{
79 typedef R result_type;
80 new (&d->return_buffer.buffer) result_type ( std::move(d->f()) );
81 if(eina_error_get())
82 {
83 d->exception = make_exception_ptr(std::system_error(efl::eina::get_error_code()));
84 eina_error_set(0);
85 result_type* p = static_cast<result_type*>(static_cast<void*>(&d->return_buffer.buffer));
86 p->~result_type();
87 }
88 return 0;
89}
90
91template <typename F>
92void* _ecore_main_loop_thread_safe_call_sync_callback(void* data)
93{
94 _data<F>* d = static_cast<_data<F>*>(data);
95 try
96 {
97 return _ecore_main_loop_thread_safe_call_sync_callback_aux
98 (d, _identity<typename std::result_of<F()>::type>());
99 }
100 catch(std::bad_alloc const& e)
101 {
102 d->exception = std::current_exception();
103 }
104 catch(std::system_error const& e)
105 {
106 d->exception = std::current_exception();
107 }
108 catch(...)
109 {
110 d->exception = std::current_exception();
111 }
112 return 0;
113}
114
115template <typename F>
116void main_loop_thread_safe_call_async(F&& f)
117{
118 ::ecore_main_loop_thread_safe_call_async( &ecore::_ecore_main_loop_thread_safe_call_async_callback<F>
119 , new F(std::forward<F>(f)) );
120}
121
122template <typename F>
123void _get_return_value(_data<F>& data, _identity<void>)
124{
125 if(data.exception)
126 {
127 std::rethrow_exception(data.exception);
128 }
129}
130
131template <typename F, typename R>
132R _get_return_value(_data<F>& data, _identity<R>)
133{
134 if(!data.exception)
135 {
136 R* b_ = static_cast<R*>(static_cast<void*>(&data.return_buffer.buffer));
137 struct destroy
138 {
139 destroy(R* x_) : p_(x_)
140 {}
141 ~destroy()
142 {
143 p_->~R();
144 }
145 R* p_;
146 } destroy_temp(b_);
147 return std::move(*b_);
148 }
149 else
150 {
151 std::rethrow_exception(data.exception);
152 }
153}
154
155template <typename F>
156typename std::result_of<F()>::type
157main_loop_thread_safe_call_sync(F&& f)
158{
159 typedef typename std::result_of<F()>::type result_type;
160 _data<F> data {f, nullptr, {}};
161 ::ecore_main_loop_thread_safe_call_sync
162 (&ecore::_ecore_main_loop_thread_safe_call_sync_callback<F>, &data);
163 return _get_return_value(data, _identity<result_type>());
164}
165
166struct ecore_init
167{
168 ecore_init()
169 {
170 ::ecore_init();
171 }
172 ~ecore_init()
173 {
174 ::ecore_shutdown();
175 }
176};
177
178} }
179 17
180#endif 18#endif
diff --git a/src/bindings/cxx/ecore_cxx/Ecore_Manual.hh b/src/bindings/cxx/ecore_cxx/Ecore_Manual.hh
new file mode 100644
index 0000000000..4bc90629a3
--- /dev/null
+++ b/src/bindings/cxx/ecore_cxx/Ecore_Manual.hh
@@ -0,0 +1,176 @@
1#ifndef _EFL_ECORE_CXX_ECORE_MANUAL_HH
2#define _EFL_ECORE_CXX_ECORE_MANUAL_HH
3
4#include <Ecore.h>
5
6#include <Eina.hh>
7
8#include <utility>
9#include <type_traits>
10#include <memory>
11#include <cstring>
12
13namespace efl { namespace ecore {
14
15template <typename T>
16struct _identity
17{
18 typedef T type;
19};
20
21template <typename F>
22void _ecore_main_loop_thread_safe_call_async_callback(void* data)
23{
24 std::unique_ptr<F> f (static_cast<F*>(data));
25 try
26 {
27 (*f)();
28 }
29 catch(std::bad_alloc const& e)
30 {
31 eina_error_set( ::EINA_ERROR_OUT_OF_MEMORY);
32 }
33 catch(std::system_error const& e)
34 {
35 efl::eina::set_error_code(e.code());
36 }
37 catch(...)
38 {
39 eina_error_set( efl::eina::unknown_error() );
40 }
41}
42
43template <typename T>
44struct _return_buffer
45{
46 typename std::aligned_storage<sizeof(T),std::alignment_of<T>::value>::type buffer;
47};
48
49template <>
50struct _return_buffer<void>
51{
52};
53
54template <typename F>
55struct _data
56{
57 F& f;
58 std::exception_ptr exception;
59 typedef typename std::result_of<F()>::type result_type;
60 _return_buffer<result_type> return_buffer;
61};
62
63template <typename F>
64void* _ecore_main_loop_thread_safe_call_sync_callback_aux(_data<F>* d, _identity<void>)
65{
66 d->f();
67 if(eina_error_get())
68 d->exception = make_exception_ptr(std::system_error(efl::eina::get_error_code()));
69 return 0;
70}
71
72template <typename F, typename R>
73void* _ecore_main_loop_thread_safe_call_sync_callback_aux(_data<F>* d, _identity<R>)
74{
75 typedef R result_type;
76 new (&d->return_buffer.buffer) result_type ( std::move(d->f()) );
77 if(eina_error_get())
78 {
79 d->exception = make_exception_ptr(std::system_error(efl::eina::get_error_code()));
80 eina_error_set(0);
81 result_type* p = static_cast<result_type*>(static_cast<void*>(&d->return_buffer.buffer));
82 p->~result_type();
83 }
84 return 0;
85}
86
87template <typename F>
88void* _ecore_main_loop_thread_safe_call_sync_callback(void* data)
89{
90 _data<F>* d = static_cast<_data<F>*>(data);
91 try
92 {
93 return _ecore_main_loop_thread_safe_call_sync_callback_aux
94 (d, _identity<typename std::result_of<F()>::type>());
95 }
96 catch(std::bad_alloc const& e)
97 {
98 d->exception = std::current_exception();
99 }
100 catch(std::system_error const& e)
101 {
102 d->exception = std::current_exception();
103 }
104 catch(...)
105 {
106 d->exception = std::current_exception();
107 }
108 return 0;
109}
110
111template <typename F>
112void main_loop_thread_safe_call_async(F&& f)
113{
114 ::ecore_main_loop_thread_safe_call_async( &ecore::_ecore_main_loop_thread_safe_call_async_callback<F>
115 , new F(std::forward<F>(f)) );
116}
117
118template <typename F>
119void _get_return_value(_data<F>& data, _identity<void>)
120{
121 if(data.exception)
122 {
123 std::rethrow_exception(data.exception);
124 }
125}
126
127template <typename F, typename R>
128R _get_return_value(_data<F>& data, _identity<R>)
129{
130 if(!data.exception)
131 {
132 R* b_ = static_cast<R*>(static_cast<void*>(&data.return_buffer.buffer));
133 struct destroy
134 {
135 destroy(R* x_) : p_(x_)
136 {}
137 ~destroy()
138 {
139 p_->~R();
140 }
141 R* p_;
142 } destroy_temp(b_);
143 return std::move(*b_);
144 }
145 else
146 {
147 std::rethrow_exception(data.exception);
148 }
149}
150
151template <typename F>
152typename std::result_of<F()>::type
153main_loop_thread_safe_call_sync(F&& f)
154{
155 typedef typename std::result_of<F()>::type result_type;
156 _data<F> data {f, nullptr, {}};
157 ::ecore_main_loop_thread_safe_call_sync
158 (&ecore::_ecore_main_loop_thread_safe_call_sync_callback<F>, &data);
159 return _get_return_value(data, _identity<result_type>());
160}
161
162struct ecore_init
163{
164 ecore_init()
165 {
166 ::ecore_init();
167 }
168 ~ecore_init()
169 {
170 ::ecore_shutdown();
171 }
172};
173
174} }
175
176#endif