summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/ecore_cxx
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-23 12:01:30 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-23 15:05:50 -0300
commite3c8b280c639058c54f5576f189d0dc288105fc7 (patch)
treee313fa05bd9d2b2cb88623f6adea2c3b396b66e8 /src/bindings/cxx/ecore_cxx
parent930f39b91b12b311f40cd96b4cf2e85085b01832 (diff)
c++: Reorganized C++ binding generation
Moved all generation to a single Makefile_Cxx.am file instead of throughout the whole project. And fixed C++ generation on Elementary after elm merge.
Diffstat (limited to 'src/bindings/cxx/ecore_cxx')
-rw-r--r--src/bindings/cxx/ecore_cxx/Ecore.hh180
1 files changed, 180 insertions, 0 deletions
diff --git a/src/bindings/cxx/ecore_cxx/Ecore.hh b/src/bindings/cxx/ecore_cxx/Ecore.hh
new file mode 100644
index 0000000000..c671287f1e
--- /dev/null
+++ b/src/bindings/cxx/ecore_cxx/Ecore.hh
@@ -0,0 +1,180 @@
1#ifndef _EFL_ECORE_CXX_ECORE_HH
2#define _EFL_ECORE_CXX_ECORE_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
13#ifdef EFL_BETA_API_SUPPORT
14#include <Ecore.eo.hh>
15#endif
16
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( ::EINA_ERROR_OUT_OF_MEMORY);
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
180#endif