summaryrefslogtreecommitdiff
path: root/src/bindings/eo_cxx/eo_event.hh
blob: d0b335e0e512985b2af776b60117eb8cc642f7a2 (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

///
/// @file eo_event.hh
///

#ifndef EFL_CXX_EO_EVENT_HH
#define EFL_CXX_EO_EVENT_HH

#include <Eo.h>
#include <Ecore.h>

#include <functional>
#include <memory>

namespace efl { namespace eo {

typedef ::Eo_Callback_Priority callback_priority;
namespace callback_priorities
{
static const callback_priority before = -100;
static const callback_priority default_ = 0;
static const callback_priority  after = 100;
}

struct signal_connection
{
  signal_connection(std::function<void()> deleter)
    : _deleter(deleter) {}
  void disconnect()
  {
    if(_deleter)
      {
        _deleter();
        _deleter = std::function<void()>();
      }
  }
private:
  std::function<void()> _deleter;
  friend struct scoped_signal_connection;
};

struct scoped_signal_connection
{
  scoped_signal_connection(signal_connection const& other)
    : _deleter(other._deleter)
  {
  }
  ~scoped_signal_connection()
  {
    disconnect();
  }
  void disconnect()
  {
    if(_deleter)
      {
        _deleter();
        _deleter = std::function<void()>();
      }
  }
  scoped_signal_connection(scoped_signal_connection&& other)
    : _deleter(other._deleter)
  {
    other._deleter = std::function<void()>();
  }
private:
  std::function<void()> _deleter;

  scoped_signal_connection& operator=(scoped_signal_connection const&) = delete;
  scoped_signal_connection(scoped_signal_connection const&) = delete;
};

template <typename F>
struct _event_deleter
{
  _event_deleter(F* data, Eo* eo, ::Eo_Event_Cb cb, Eo_Event_Description const* description)
    : _data(data), _eo( ::eo_ref(eo)), _cb(cb), _description(description)
  {
  }
  ~_event_deleter()
  {
    ::eo_unref(_eo);
  }
  _event_deleter(_event_deleter const& other)
    : _data(other._data), _eo( ::eo_ref(other._eo)), _cb(other._cb), _description(other._description)
  {}
  _event_deleter& operator=(_event_deleter const& other)
  {
    ::eo_unref( _eo);
    _data = other._data;
    _eo = ::eo_ref(other._eo);
    _cb = other._cb;
    _description = other._description;
    return *this;
  }

  void operator()() const
  {
    eo_do(_eo, ::eo_event_callback_del(_description, _cb, _data));
    ::ecore_main_loop_thread_safe_call_async(&_deleter_call, _data);
  }

private:
  static void _deleter_call(void* data)
  {
    delete static_cast<F*>(data);
  }

  F* _data;
  Eo* _eo;
  ::Eo_Event_Cb _cb;
  Eo_Event_Description const* _description;
};

template <typename F>
signal_connection make_signal_connection(std::unique_ptr<F>& data, Eo* eo, ::Eo_Event_Cb cb, Eo_Event_Description const* description)
{
  signal_connection c(_event_deleter<F>(data.get(), eo, cb, description));
  data.release();
  return c;
}

namespace _detail {

template <typename T, typename F>
Eina_Bool really_call_event(T& wrapper, F& f, Eo_Event_Description const& desc, void *info
                            , std::true_type)
{
   f(wrapper, desc, info);
   return true;
}
template <typename T, typename F>
Eina_Bool really_call_event(T& wrapper, F& f, Eo_Event_Description const& desc, void *info
                            , std::false_type)
{
   return f(wrapper, desc, info);
}

template <typename T, typename F>
Eina_Bool
event_callback(void *data, ::Eo_Event2 const* event)
{
   T wrapper(::eo_ref(event->obj));
   F *f = static_cast<F*>(data);
   return _detail::really_call_event(wrapper, *f, *event->desc, event->event_info
                                     , std::is_void<decltype((*f)(wrapper, *event->desc, event->event_info))>());
}

}

} }

#endif