Eobj: Changed weak-ref to work without allocating a structure.
authorTom Hacohen <tom@stosb.com>
Mon, 23 Apr 2012 10:57:04 +0000 (10:57 +0000)
committerTom Hacohen <tom@stosb.com>
Mon, 23 Apr 2012 10:57:04 +0000 (10:57 +0000)
Thanks to cedric.

SVN revision: 70415

legacy/eobj/lib/Eobj.h
legacy/eobj/lib/eobj.c
legacy/eobj/tests/eobj_test_general.c

index b6a40a8..d8a508c 100644 (file)
@@ -535,44 +535,17 @@ EAPI void eobj_del(Eobj *obj);
  */
 
 /**
- * @struct _Eobj_Weak_Ref
- * This is exposed for performance, please use eobj_weak_ref_get() when you
- * actually want to get the refed object.
- */
-struct _Eobj_Weak_Ref
-{
-   Eobj *obj; /**< The object being referenced. */
-};
-
-/**
- * @typedef Eobj_Weak_Ref
- * Weak reference type for Eobj.
- */
-typedef struct _Eobj_Weak_Ref Eobj_Weak_Ref;
-
-/**
  * @brief Create a new weak reference to obj.
  * @param obj The object being referenced.
- * @return A new weak reference.
+ * @param wref The pointer to use for the weak ref.
  */
-EAPI Eobj_Weak_Ref *eobj_weak_ref_new(const Eobj *obj);
+EAPI void eobj_weak_ref_add(const Eobj *obj, Eobj **wref);
 
 /**
  * @brief Free the weak reference passed.
  * @param wref the weak reference to free.
  */
-EAPI void eobj_weak_ref_free(Eobj_Weak_Ref *wref);
-
-/**
- * @brief Get the referenced object from the weak reference.
- * @param wref the weak reference to get the object from.
- * @return The object referenced by wref.
- */
-static inline Eobj *
-eobj_weak_ref_get(const Eobj_Weak_Ref *wref)
-{
-   return wref->obj;
-}
+EAPI void eobj_weak_ref_del(Eobj **wref);
 
 /**
  * @}
index aef1ebf..f60824e 100644 (file)
@@ -1083,35 +1083,30 @@ eobj_ref_get(const Eobj *obj)
 Eina_Bool
 _eobj_weak_ref_cb(void *data, Eobj *obj EINA_UNUSED, const Eobj_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
 {
-   Eobj_Weak_Ref *wref = data;
-   wref->obj = NULL;
+   Eobj **wref = data;
+   *wref = NULL;
 
    return EOBJ_CALLBACK_CONTINUE;
 }
 
-EAPI Eobj_Weak_Ref *
-eobj_weak_ref_new(const Eobj *_obj)
+EAPI void
+eobj_weak_ref_add(const Eobj *_obj, Eobj **wref)
 {
    Eobj *obj = (Eobj *) _obj;
-   Eobj_Weak_Ref *wref = calloc(1, sizeof(*wref));
-
-   EOBJ_MAGIC_RETURN_VAL(obj, EOBJ_EINA_MAGIC, wref);
+   EOBJ_MAGIC_RETURN(obj, EOBJ_EINA_MAGIC);
 
-   wref->obj = obj;
+   *wref = obj;
    eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
-
-   return wref;
 }
 
 EAPI void
-eobj_weak_ref_free(Eobj_Weak_Ref *wref)
+eobj_weak_ref_del(Eobj **wref)
 {
-   if (wref->obj)
+   if (*wref)
      {
-        eobj_event_callback_del_full(wref->obj, EOBJ_EV_DEL, _eobj_weak_ref_cb,
+        eobj_event_callback_del_full(*wref, EOBJ_EV_DEL, _eobj_weak_ref_cb,
               wref);
      }
-   free(wref);
 }
 
 /* EOF Weak reference. */
index b425a73..7b1b0f3 100644 (file)
@@ -69,32 +69,33 @@ START_TEST(eobj_weak_reference)
    eobj_init();
 
    Eobj *obj = eobj_add(SIMPLE_CLASS, NULL);
-   Eobj_Weak_Ref *wref = eobj_weak_ref_new(obj);
-   fail_if(!eobj_weak_ref_get(wref));
+   Eobj *wref;
+   eobj_weak_ref_add(obj, &wref);
+   fail_if(!wref);
 
    eobj_unref(obj);
-   fail_if(eobj_weak_ref_get(wref));
+   fail_if(wref);
 
-   eobj_weak_ref_free(wref);
+   eobj_weak_ref_del(&wref);
 
    obj = eobj_add(SIMPLE_CLASS, NULL);
-   wref = eobj_weak_ref_new(obj);
+   eobj_weak_ref_add(obj, &wref);
 
    eobj_ref(obj);
-   fail_if(!eobj_weak_ref_get(wref));
+   fail_if(!wref);
 
    eobj_del(obj);
-   fail_if(eobj_weak_ref_get(wref));
+   fail_if(wref);
 
    eobj_unref(obj);
-   fail_if(eobj_weak_ref_get(wref));
+   fail_if(wref);
 
-   eobj_weak_ref_free(wref);
+   eobj_weak_ref_del(&wref);
 
    obj = eobj_add(SIMPLE_CLASS, NULL);
 
-   wref = eobj_weak_ref_new(obj);
-   eobj_weak_ref_free(wref);
+   eobj_weak_ref_add(obj, &wref);
+   eobj_weak_ref_del(&wref);
 
    eobj_unref(obj);
 
@@ -275,9 +276,10 @@ START_TEST(eobj_magic_checks)
 
    fail_if(0 != eobj_ref_get((Eobj *) buf));
 
-   Eobj_Weak_Ref *wref = eobj_weak_ref_new((Eobj *) buf);
-   fail_if(eobj_weak_ref_get(wref));
-   eobj_weak_ref_free(wref);
+   Eobj *wref = NULL;
+   eobj_weak_ref_add((Eobj *) buf, &wref);
+   fail_if(wref);
+   eobj_weak_ref_del(&wref);
 
    eobj_del((Eobj *) buf);