Eobj: Changed weak-ref to work without allocating a structure.
Thanks to cedric. SVN revision: 70415
This commit is contained in:
parent
945cf1e9c2
commit
5c5fc69bef
|
@ -534,45 +534,18 @@ 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.
|
* @brief Create a new weak reference to obj.
|
||||||
* @param obj The object being referenced.
|
* @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.
|
* @brief Free the weak reference passed.
|
||||||
* @param wref the weak reference to free.
|
* @param wref the weak reference to free.
|
||||||
*/
|
*/
|
||||||
EAPI void eobj_weak_ref_free(Eobj_Weak_Ref *wref);
|
EAPI void eobj_weak_ref_del(Eobj **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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|
|
@ -1083,35 +1083,30 @@ eobj_ref_get(const Eobj *obj)
|
||||||
Eina_Bool
|
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_cb(void *data, Eobj *obj EINA_UNUSED, const Eobj_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
|
||||||
{
|
{
|
||||||
Eobj_Weak_Ref *wref = data;
|
Eobj **wref = data;
|
||||||
wref->obj = NULL;
|
*wref = NULL;
|
||||||
|
|
||||||
return EOBJ_CALLBACK_CONTINUE;
|
return EOBJ_CALLBACK_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
EAPI Eobj_Weak_Ref *
|
EAPI void
|
||||||
eobj_weak_ref_new(const Eobj *_obj)
|
eobj_weak_ref_add(const Eobj *_obj, Eobj **wref)
|
||||||
{
|
{
|
||||||
Eobj *obj = (Eobj *) _obj;
|
Eobj *obj = (Eobj *) _obj;
|
||||||
Eobj_Weak_Ref *wref = calloc(1, sizeof(*wref));
|
EOBJ_MAGIC_RETURN(obj, EOBJ_EINA_MAGIC);
|
||||||
|
|
||||||
EOBJ_MAGIC_RETURN_VAL(obj, EOBJ_EINA_MAGIC, wref);
|
*wref = obj;
|
||||||
|
|
||||||
wref->obj = obj;
|
|
||||||
eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
|
eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
|
||||||
|
|
||||||
return wref;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EAPI void
|
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);
|
wref);
|
||||||
}
|
}
|
||||||
free(wref);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* EOF Weak reference. */
|
/* EOF Weak reference. */
|
||||||
|
|
|
@ -69,32 +69,33 @@ START_TEST(eobj_weak_reference)
|
||||||
eobj_init();
|
eobj_init();
|
||||||
|
|
||||||
Eobj *obj = eobj_add(SIMPLE_CLASS, NULL);
|
Eobj *obj = eobj_add(SIMPLE_CLASS, NULL);
|
||||||
Eobj_Weak_Ref *wref = eobj_weak_ref_new(obj);
|
Eobj *wref;
|
||||||
fail_if(!eobj_weak_ref_get(wref));
|
eobj_weak_ref_add(obj, &wref);
|
||||||
|
fail_if(!wref);
|
||||||
|
|
||||||
eobj_unref(obj);
|
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);
|
obj = eobj_add(SIMPLE_CLASS, NULL);
|
||||||
wref = eobj_weak_ref_new(obj);
|
eobj_weak_ref_add(obj, &wref);
|
||||||
|
|
||||||
eobj_ref(obj);
|
eobj_ref(obj);
|
||||||
fail_if(!eobj_weak_ref_get(wref));
|
fail_if(!wref);
|
||||||
|
|
||||||
eobj_del(obj);
|
eobj_del(obj);
|
||||||
fail_if(eobj_weak_ref_get(wref));
|
fail_if(wref);
|
||||||
|
|
||||||
eobj_unref(obj);
|
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);
|
obj = eobj_add(SIMPLE_CLASS, NULL);
|
||||||
|
|
||||||
wref = eobj_weak_ref_new(obj);
|
eobj_weak_ref_add(obj, &wref);
|
||||||
eobj_weak_ref_free(wref);
|
eobj_weak_ref_del(&wref);
|
||||||
|
|
||||||
eobj_unref(obj);
|
eobj_unref(obj);
|
||||||
|
|
||||||
|
@ -275,9 +276,10 @@ START_TEST(eobj_magic_checks)
|
||||||
|
|
||||||
fail_if(0 != eobj_ref_get((Eobj *) buf));
|
fail_if(0 != eobj_ref_get((Eobj *) buf));
|
||||||
|
|
||||||
Eobj_Weak_Ref *wref = eobj_weak_ref_new((Eobj *) buf);
|
Eobj *wref = NULL;
|
||||||
fail_if(eobj_weak_ref_get(wref));
|
eobj_weak_ref_add((Eobj *) buf, &wref);
|
||||||
eobj_weak_ref_free(wref);
|
fail_if(wref);
|
||||||
|
eobj_weak_ref_del(&wref);
|
||||||
|
|
||||||
eobj_del((Eobj *) buf);
|
eobj_del((Eobj *) buf);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue