2010-12-21 15:35:39 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx_c_observer.c
|
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_OBSERVER_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_c_observer.h"
|
|
|
|
|
2013-03-02 03:11:17 +01:00
|
|
|
//#define DEBUG_ME 1
|
|
|
|
|
2015-04-19 15:43:33 +02:00
|
|
|
void COBSERVER_lock(COBSERVER *this, bool lock)
|
|
|
|
{
|
|
|
|
if (lock)
|
|
|
|
this->locked++;
|
|
|
|
else
|
|
|
|
this->locked--;
|
|
|
|
}
|
|
|
|
|
2010-12-21 15:35:39 +01:00
|
|
|
void COBSERVER_attach(COBSERVER *this, void *parent, const char *name)
|
|
|
|
{
|
2013-03-02 03:11:17 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "COBSERVER_attach: %p: %s %p\n", this, parent ? OBJECT_class(parent)->name : "", parent);
|
|
|
|
#endif
|
2018-12-24 17:57:36 +01:00
|
|
|
if (this->object && this->event)
|
2010-12-21 15:35:39 +01:00
|
|
|
EVENT_search(OBJECT_class(this->object), this->event, name, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void COBSERVER_detach(COBSERVER *this)
|
|
|
|
{
|
2013-03-02 03:11:17 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "COBSERVER_detach: %p\n", this);
|
|
|
|
#endif
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
if (this->event)
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&this->event);
|
2010-12-21 15:35:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_METHOD(Observer_new, GB_OBJECT object; GB_BOOLEAN after)
|
|
|
|
|
|
|
|
OBJECT *object;
|
|
|
|
OBJECT_EVENT *ev;
|
|
|
|
char *name;
|
|
|
|
CLASS *class;
|
|
|
|
void *parent;
|
|
|
|
|
|
|
|
object = (OBJECT *)VARG(object);
|
|
|
|
if (GB_CheckObject(object))
|
|
|
|
return;
|
|
|
|
|
2018-10-08 16:36:38 +02:00
|
|
|
parent = OBJECT_parent(THIS);
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
class = OBJECT_class(object);
|
|
|
|
if (class->n_event == 0)
|
|
|
|
return;
|
|
|
|
|
2018-10-08 16:36:38 +02:00
|
|
|
name = EVENT_Name;
|
2010-12-21 15:35:39 +01:00
|
|
|
if (!name || !*name)
|
|
|
|
return;
|
|
|
|
|
2013-03-02 03:11:17 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "Observer_new: %p %d %s (%s %p)\n", THIS, OBJECT_class(object)->n_event, name, GB_GetClassName(parent), parent);
|
|
|
|
#endif
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
ev = OBJECT_event(object);
|
|
|
|
|
|
|
|
THIS->after = VARGOPT(after, FALSE);
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC_ZERO(&THIS->event, sizeof(ushort) * class->n_event);
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
THIS->object = object;
|
|
|
|
OBJECT_attach((OBJECT *)THIS, parent, name);
|
|
|
|
COBSERVER_attach(THIS, parent, name);
|
|
|
|
|
|
|
|
LIST_insert((void **)&ev->observer, THIS, &THIS->list);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(THIS);
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Observer_free)
|
|
|
|
|
2013-03-02 03:11:17 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "Observer_free: %p\n", THIS);
|
|
|
|
#endif
|
2010-12-21 15:35:39 +01:00
|
|
|
|
|
|
|
GB_StoreVariant(NULL, &THIS->tag);
|
|
|
|
COBSERVER_detach(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(Observer_Object)
|
|
|
|
|
|
|
|
GB_ReturnObject(THIS->object);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(Observer_Tag)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2011-11-14 02:06:51 +01:00
|
|
|
GB_ReturnVariant(&THIS->tag);
|
2010-12-21 15:35:39 +01:00
|
|
|
else
|
|
|
|
GB_StoreVariant(PROP(GB_VARIANT), &THIS->tag);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2022-02-01 02:05:19 +01:00
|
|
|
BEGIN_METHOD_VOID(Observer_Detach)
|
|
|
|
|
|
|
|
OBJECT_EVENT *ev;
|
|
|
|
|
|
|
|
if (!THIS->object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ev = OBJECT_event(THIS->object);
|
|
|
|
LIST_remove((void **)&ev->observer, THIS, &THIS->list);
|
|
|
|
THIS->object = NULL;
|
|
|
|
OBJECT_UNREF(_object);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-21 15:35:39 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
GB_DESC NATIVE_Observer[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Observer", sizeof(COBSERVER)),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, Observer_new, "(Object)o[(After)b]"),
|
|
|
|
GB_METHOD("_free", NULL, Observer_free, NULL),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Object", "o", Observer_Object),
|
|
|
|
GB_PROPERTY("Tag", "v", Observer_Tag),
|
|
|
|
|
2022-02-01 02:05:19 +01:00
|
|
|
GB_METHOD("Detach", NULL, Observer_Detach, NULL),
|
|
|
|
|
2010-12-21 15:35:39 +01:00
|
|
|
GB_END_DECLARE
|
|
|
|
};
|