summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2015-03-11 04:13:08 +0200
committerKai Huuhko <kai.huuhko@gmail.com>2015-03-11 04:13:08 +0200
commit7fad9e3ff4ab12b996e8f37577131519cd258977 (patch)
tree4841502a0c9c59a22c407b2fcf2f0e20a2c22d69
parentc4786f997e32b809e7d1506338014049c5a6b05a (diff)
Evas.SmartObject: Simplify callback handler code
This should do the same as the previous approach, except heaps and tons faster. No more dicts and lists to go through.
-rw-r--r--efl/evas/efl.evas_object_smart.pxi81
-rw-r--r--include/efl.evas.pxd1
2 files changed, 32 insertions, 50 deletions
diff --git a/efl/evas/efl.evas_object_smart.pxi b/efl/evas/efl.evas_object_smart.pxi
index 17ed4ff..091da06 100644
--- a/efl/evas/efl.evas_object_smart.pxi
+++ b/efl/evas/efl.evas_object_smart.pxi
@@ -324,7 +324,7 @@ cdef void _smart_callback(void *data, Evas_Object *o, void *event_info) with gil
324 cdef: 324 cdef:
325 void *tmp = NULL 325 void *tmp = NULL
326 SmartObject obj 326 SmartObject obj
327 object event, ei 327 object ei
328 328
329 eo_do_ret(o, tmp, eo_key_data_get("python-eo")) 329 eo_do_ret(o, tmp, eo_key_data_get("python-eo"))
330 if tmp == NULL: 330 if tmp == NULL:
@@ -333,18 +333,20 @@ cdef void _smart_callback(void *data, Evas_Object *o, void *event_info) with gil
333 else: 333 else:
334 obj = <SmartObject>tmp 334 obj = <SmartObject>tmp
335 335
336 event = <object>data 336 if data == NULL:
337 lst = tuple(obj._smart_callbacks[event]) 337 EINA_LOG_DOM_ERR(PY_EFL_EVAS_LOG_DOMAIN, "data is NULL!", NULL)
338 return
339 else:
340 event_conv, func, args, kargs = <tuple>data
338 341
339 for event_conv, func, args, kargs in lst: 342 try:
340 try: 343 if event_conv is None:
341 if event_conv is None: 344 func(obj, *args, **kargs)
342 func(obj, *args, **kargs) 345 else:
343 else: 346 ei = event_conv(<uintptr_t>event_info)
344 ei = event_conv(<uintptr_t>event_info) 347 func(obj, ei, *args, **kargs)
345 func(obj, ei, *args, **kargs) 348 except Exception:
346 except Exception: 349 traceback.print_exc()
347 traceback.print_exc()
348 350
349 351
350cdef class Smart(object): 352cdef class Smart(object):
@@ -589,12 +591,6 @@ cdef class SmartObject(Object):
589 should be instantiated and passed to this classes constructor as 591 should be instantiated and passed to this classes constructor as
590 parameter ``smart`` 592 parameter ``smart``
591 """ 593 """
592 def __cinit__(self):
593 self._smart_callbacks = dict()
594
595 def __dealloc__(self):
596 self._smart_callbacks = None
597
598 def __init__(self, Canvas canvas not None, Smart smart not None, **kwargs): 594 def __init__(self, Canvas canvas not None, Smart smart not None, **kwargs):
599 #_smart_classes.append(<uintptr_t>cls_def) 595 #_smart_classes.append(<uintptr_t>cls_def)
600 self._set_obj(evas_object_smart_add(canvas.obj, smart.cls)) 596 self._set_obj(evas_object_smart_add(canvas.obj, smart.cls))
@@ -750,18 +746,14 @@ cdef class SmartObject(Object):
750 raise TypeError("func must be callable") 746 raise TypeError("func must be callable")
751 if event_conv is not None and not callable(event_conv): 747 if event_conv is not None and not callable(event_conv):
752 raise TypeError("event_conv must be None or callable") 748 raise TypeError("event_conv must be None or callable")
749 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event)
753 750
754 if self._smart_callbacks is None: 751 spec = (event_conv, func, args, kargs)
755 self._smart_callbacks = {} 752 Py_INCREF(spec)
756 753
757 e = intern(event) 754 evas_object_smart_callback_add(self.obj,
758 lst = self._smart_callbacks.setdefault(e, []) 755 <const char *>event if event is not None else NULL,
759 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event) 756 _smart_callback, <void *>spec)
760 if not lst:
761 evas_object_smart_callback_add(self.obj,
762 <const char *>event if event is not None else NULL,
763 _smart_callback, <void *>e)
764 lst.append((event_conv, func, args, kargs))
765 757
766 return 1 758 return 1
767 759
@@ -783,31 +775,22 @@ cdef class SmartObject(Object):
783 :raise ValueError: if there was no **func** connected with this event. 775 :raise ValueError: if there was no **func** connected with this event.
784 776
785 """ 777 """
786 try: 778 cdef:
787 lst = self._smart_callbacks[event] 779 void *tmp
788 except KeyError as e: 780 tuple spec
789 raise ValueError("Unknown event %r" % event) 781
790
791 i = -1
792 ec = None
793 f = None
794 for i, (ec, f, a, k) in enumerate(lst):
795 if event_conv == ec and func == f:
796 break
797
798 if f != func or ec != event_conv:
799 raise ValueError("Callback %s was not registered with event %r" %
800 (func, event))
801
802 lst.pop(i)
803 if lst:
804 return 1
805 self._smart_callbacks.pop(event)
806 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event) 782 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event)
807 evas_object_smart_callback_del(self.obj, 783
784 tmp = evas_object_smart_callback_del(self.obj,
808 <const char *>event if event is not None else NULL, 785 <const char *>event if event is not None else NULL,
809 _smart_callback) 786 _smart_callback)
810 787
788 if tmp == NULL:
789 return 1
790 else:
791 spec = <tuple>tmp
792 Py_DECREF(spec)
793
811 return 1 794 return 1
812 795
813 cdef int _callback_add(self, event, func, args, kargs) except 0: 796 cdef int _callback_add(self, event, func, args, kargs) except 0:
diff --git a/include/efl.evas.pxd b/include/efl.evas.pxd
index e322abb..ffb3170 100644
--- a/include/efl.evas.pxd
+++ b/include/efl.evas.pxd
@@ -1205,7 +1205,6 @@ cdef class Textblock(Object):
1205 1205
1206 1206
1207cdef class SmartObject(Object): 1207cdef class SmartObject(Object):
1208 cdef dict _smart_callbacks
1209 cdef int _set_obj(self, cEo *obj) except 0 1208 cdef int _set_obj(self, cEo *obj) except 0
1210 cdef int _callback_add_full(self, event, event_conv, func, tuple args, dict kargs) except 0 1209 cdef int _callback_add_full(self, event, event_conv, func, tuple args, dict kargs) except 0
1211 cdef int _callback_del_full(self, event, event_conv, func) except 0 1210 cdef int _callback_del_full(self, event, event_conv, func) except 0