2003-09-11 06:03:13 +00:00
|
|
|
/*
|
|
|
|
gib_classes.c
|
|
|
|
|
|
|
|
GIB built-in classes
|
|
|
|
|
|
|
|
Copyright (C) 2003 Brian Koropoff
|
|
|
|
|
|
|
|
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
|
|
|
|
of the License, 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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-05-26 07:11:50 +00:00
|
|
|
#include "QF/cbuf.h"
|
2003-09-11 06:03:13 +00:00
|
|
|
#include "QF/gib.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/llist.h"
|
|
|
|
#include "gib_object.h"
|
|
|
|
#include "gib_thread.h"
|
|
|
|
#include "gib_classes.h"
|
|
|
|
#include "gib_tree.h"
|
|
|
|
#include "gib_vars.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Begin Object class
|
|
|
|
|
|
|
|
The root of the GIB class hierarchy. Responsible for retain/release
|
|
|
|
reference counting, creating instances of an object, returning
|
|
|
|
an object handle, and reporting object type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct baseobj_s {
|
2012-05-21 23:23:22 +00:00
|
|
|
unsigned long int ref;
|
2003-09-11 06:03:13 +00:00
|
|
|
} baseobj_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_Retain_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
2003-09-11 08:51:44 +00:00
|
|
|
baseobj_t *base = data;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 08:51:44 +00:00
|
|
|
base->ref++;
|
2003-09-11 06:03:13 +00:00
|
|
|
GIB_Object_Incref (obj);
|
2003-09-21 00:30:47 +00:00
|
|
|
GIB_Reply (obj, mesg, 1, &obj->handstr);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_Release_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2012-05-21 23:23:22 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
2003-09-11 08:51:44 +00:00
|
|
|
baseobj_t *base = data;
|
|
|
|
if (base->ref) {
|
|
|
|
base->ref--;
|
|
|
|
GIB_Object_Decref (obj);
|
|
|
|
}
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_Init_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
2003-09-21 00:30:47 +00:00
|
|
|
GIB_Reply (obj, mesg, 1, &obj->handstr);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_Class_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Reply (obj, mesg, 1, &obj->class->name);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
static int
|
|
|
|
Object_SuperClass_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
if (obj->class->parent)
|
|
|
|
GIB_Reply (obj, mesg, 1, &obj->class->parent->name);
|
|
|
|
else
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_IsKindOf_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
gib_class_t *c;
|
|
|
|
static const char *one = "1";
|
|
|
|
static const char *zero = "0";
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
if (mesg.argc < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (c = obj->class; c; c = c->parent)
|
|
|
|
if (!strcmp (mesg.argv[1], c->name)) {
|
|
|
|
GIB_Reply (obj, mesg, 1, &one);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
GIB_Reply (obj, mesg, 1, &zero);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
static int
|
|
|
|
Object_Dispose_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
static const char *disposed = "disposed";
|
|
|
|
GIB_Object_Signal_Emit (obj, 1, &disposed);
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_SignalConnect_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
gib_object_t *other;
|
|
|
|
|
|
|
|
if (mesg.argc >= 4 && (other = GIB_Object_Get (mesg.argv[2])))
|
|
|
|
GIB_Object_Signal_Slot_Pair (obj, mesg.argv[1], other,
|
|
|
|
mesg.argv[3]);
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_SignalDisconnect_f (gib_object_t *obj, gib_method_t *method, void
|
2003-09-11 08:51:44 +00:00
|
|
|
*data, gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
gib_object_t *other;
|
|
|
|
|
|
|
|
if (mesg.argc >= 4 && (other = GIB_Object_Get (mesg.argv[2])))
|
|
|
|
GIB_Object_Signal_Slot_Destroy (obj, mesg.argv[1], other,
|
|
|
|
mesg.argv[3]);
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Object_Class_New_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
const char *old;
|
|
|
|
gib_object_t *new;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
new = GIB_Object_Create (obj->class->name, false);
|
|
|
|
old = mesg.argv[0];
|
|
|
|
mesg.argv[0] = "init";
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Send (new, sender, mesg.argc, mesg.argv, mesg.reply, mesg.replydata);
|
|
|
|
GIB_Object_Decref (obj);
|
2003-09-11 06:03:13 +00:00
|
|
|
mesg.argv[0] = old;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-21 00:30:47 +00:00
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
static const char **g_occ_reply;
|
|
|
|
static unsigned int g_occ_i = 0;
|
|
|
|
|
|
|
|
static qboolean occ_iterator (gib_class_t *class, void *unused)
|
|
|
|
{
|
|
|
|
g_occ_reply[g_occ_i++] = class->name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
static int
|
|
|
|
Object_Class_Children_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
unsigned int size;
|
2007-03-22 23:20:57 +00:00
|
|
|
|
|
|
|
g_occ_i = 0;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
size = llist_size (obj->class->children);
|
|
|
|
if (size) {
|
2007-03-22 23:20:57 +00:00
|
|
|
g_occ_reply = malloc (sizeof (char *) * size);
|
|
|
|
llist_iterate (obj->class->children, LLIST_ICAST (occ_iterator));
|
|
|
|
GIB_Reply (obj, mesg, size, g_occ_reply);
|
2003-09-21 00:30:47 +00:00
|
|
|
} else
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
static void *
|
|
|
|
Object_Construct (gib_object_t *obj)
|
|
|
|
{
|
|
|
|
baseobj_t *base = malloc (sizeof (baseobj_t));
|
|
|
|
|
2003-09-11 08:51:44 +00:00
|
|
|
base->ref = 1;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Object_Destruct (void *data)
|
|
|
|
{
|
|
|
|
free (data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t Object_methods[] = {
|
2003-09-11 06:03:13 +00:00
|
|
|
{"retain", Object_Retain_f, NULL},
|
|
|
|
{"release", Object_Release_f, NULL},
|
|
|
|
{"init", Object_Init_f, NULL},
|
|
|
|
{"class", Object_Class_f, NULL},
|
2003-09-21 00:30:47 +00:00
|
|
|
{"superClass", Object_SuperClass_f, NULL},
|
|
|
|
{"isKindOf", Object_IsKindOf_f, NULL},
|
2003-09-11 06:03:13 +00:00
|
|
|
{"dispose", Object_Dispose_f, NULL},
|
|
|
|
{"signalConnect", Object_SignalConnect_f, NULL},
|
|
|
|
{"signalDisconnect", Object_SignalDisconnect_f, NULL},
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t Object_class_methods[] = {
|
2003-09-21 00:30:47 +00:00
|
|
|
{"parent", Object_SuperClass_f, NULL},
|
|
|
|
{"children", Object_Class_Children_f, NULL},
|
2003-09-11 06:03:13 +00:00
|
|
|
{"new", Object_Class_New_f, NULL},
|
|
|
|
{"signalConnect", Object_SignalConnect_f, NULL},
|
|
|
|
{"signalDisconnect", Object_SignalDisconnect_f, NULL},
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_classdesc_t Object_class = {
|
2003-09-11 06:03:13 +00:00
|
|
|
"Object", NULL,
|
|
|
|
Object_Construct, NULL,
|
|
|
|
Object_Destruct,
|
|
|
|
Object_methods, Object_class_methods
|
|
|
|
};
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
/* End Object class */
|
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
/*
|
|
|
|
Begin Thread class
|
|
|
|
|
|
|
|
Creates a front end to a separate GIB buffer that is executed every frame.
|
|
|
|
The object is initialized with a GIB function and set of arguments to that
|
|
|
|
function that gets executed immediately. When the thread exits, the object
|
|
|
|
gets sent a release message. Causing the object to be destroyed before the
|
|
|
|
thread exits will immediately terminate the thread. The object can be
|
|
|
|
retained so that it is not destroyed when the thread exits on its own; in
|
|
|
|
this case, the object must be released manually later on, but otherwise
|
|
|
|
will do nothing.
|
|
|
|
*/
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
typedef struct Thread_class_s {
|
|
|
|
hashtab_t *methods;
|
|
|
|
} Thread_class_t;
|
|
|
|
|
|
|
|
typedef struct Thread_s {
|
|
|
|
gib_object_t *obj;
|
|
|
|
cbuf_t *thread;
|
|
|
|
qboolean ended;
|
|
|
|
} Thread_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
Thread_Init_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
Thread_t *t = (Thread_t *) data;
|
|
|
|
gib_function_t *f;
|
|
|
|
|
|
|
|
if (mesg.argc < 2 || !(f = GIB_Function_Find (mesg.argv[1]))) {
|
|
|
|
GIB_Object_Destroy (obj);
|
2003-09-11 08:51:44 +00:00
|
|
|
return -1;
|
2003-09-11 06:03:13 +00:00
|
|
|
} else {
|
|
|
|
GIB_Function_Execute (t->thread, f, mesg.argv+1, mesg.argc-1);
|
|
|
|
Cbuf_Execute_Stack (t->thread);
|
|
|
|
return GIB_ForwardToSuper (mesg, obj, method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Thread_Cbuf_Freed (cbuf_t *cbuf, void *data)
|
|
|
|
{
|
|
|
|
Thread_t *t = (Thread_t *) data;
|
|
|
|
static const char *release = "release";
|
|
|
|
|
|
|
|
t->thread = NULL;
|
|
|
|
t->ended = true;
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Send (t->obj, t->obj, 1, &release, NULL, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
Thread_Construct (gib_object_t *obj)
|
|
|
|
{
|
|
|
|
Thread_t *t = malloc (sizeof (Thread_t));
|
|
|
|
t->thread = GIB_Thread_New ();
|
|
|
|
GIB_DATA (t->thread)->dnotify = Thread_Cbuf_Freed;
|
|
|
|
GIB_DATA (t->thread)->ddata = t;
|
|
|
|
t->obj = obj;
|
|
|
|
t->ended = false;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Thread_Destruct (void *data)
|
|
|
|
{
|
|
|
|
Thread_t *t = (Thread_t *) data;
|
|
|
|
|
|
|
|
if (!t->ended) {
|
|
|
|
GIB_DATA (t->thread)->dnotify = NULL;
|
|
|
|
GIB_Thread_Delete (t->thread);
|
|
|
|
}
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t Thread_methods[] = {
|
2003-09-11 06:03:13 +00:00
|
|
|
{"init", Thread_Init_f, NULL},
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t Thread_class_methods[] = {
|
2003-09-11 06:03:13 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_classdesc_t Thread_class = {
|
2003-09-11 06:03:13 +00:00
|
|
|
"Thread", "Object",
|
|
|
|
Thread_Construct, NULL,
|
|
|
|
Thread_Destruct,
|
|
|
|
Thread_methods, Thread_class_methods
|
|
|
|
};
|
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
/*
|
2003-11-29 02:01:31 +00:00
|
|
|
Object Hash class
|
2003-09-21 00:30:47 +00:00
|
|
|
|
|
|
|
Stores references to objects in a Hash
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct ObjRef_s {
|
|
|
|
const char *key;
|
|
|
|
gib_object_t *obj;
|
|
|
|
} ObjRef_t;
|
|
|
|
|
|
|
|
typedef struct ObjectHash_s {
|
|
|
|
hashtab_t *objects;
|
|
|
|
} ObjectHash_t;
|
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
ObjRef_Get_Key (const void *ele, void *ptr)
|
2003-09-21 00:30:47 +00:00
|
|
|
{
|
|
|
|
return ((ObjRef_t *) ele)->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ObjRef_Free (void *ele, void *ptr)
|
|
|
|
{
|
|
|
|
ObjRef_t *ref = ele;
|
|
|
|
free ((void *)ref->key);
|
|
|
|
GIB_Object_Decref (ref->obj);
|
|
|
|
free (ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ObjectHash_Construct (gib_object_t *obj)
|
|
|
|
{
|
|
|
|
ObjectHash_t *data = malloc (sizeof (ObjectHash_t));
|
|
|
|
|
2020-03-25 06:43:16 +00:00
|
|
|
data->objects = Hash_NewTable (1024, ObjRef_Get_Key, ObjRef_Free, NULL, 0);
|
2003-09-21 00:30:47 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ObjectHash_Destruct (void *data)
|
|
|
|
{
|
|
|
|
ObjectHash_t *objh = data;
|
|
|
|
|
|
|
|
Hash_DelTable (objh->objects);
|
|
|
|
free (objh);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ObjectHash_Insert_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
ObjectHash_t *objh = data;
|
|
|
|
gib_object_t *ins;
|
|
|
|
ObjRef_t *new;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (mesg.argc < 3)
|
|
|
|
return -1;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
for (i = 2; i < mesg.argc; i++) {
|
|
|
|
if ((ins = GIB_Object_Get (mesg.argv[i]))) {
|
|
|
|
new = malloc (sizeof (ObjRef_t));
|
|
|
|
new->key = strdup (mesg.argv[1]);
|
|
|
|
new->obj = ins;
|
|
|
|
GIB_Object_Incref (ins);
|
|
|
|
Hash_Add (objh->objects, new);
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ObjectHash_Get_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
ObjectHash_t *objh = data;
|
|
|
|
ObjRef_t **refs, **r;
|
|
|
|
const char **reply;
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
if (mesg.argc < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((refs = (ObjRef_t **) Hash_FindList (objh->objects,
|
|
|
|
mesg.argv[1]))) {
|
|
|
|
for (r = refs, len = 0; *r; r++, len++);
|
2018-09-07 11:00:57 +00:00
|
|
|
reply = malloc (sizeof (char *) * len);
|
2003-09-21 00:30:47 +00:00
|
|
|
for (r = refs, i = 0; *r; r++, i++)
|
|
|
|
reply[i] = (*r)->obj->handstr;
|
|
|
|
GIB_Reply (obj, mesg, len, reply);
|
2007-03-22 23:20:57 +00:00
|
|
|
free ((void*)reply);
|
2003-09-21 00:30:47 +00:00
|
|
|
} else
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ObjectHash_Remove_f (gib_object_t *obj, gib_method_t *method, void *data,
|
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
|
|
|
{
|
|
|
|
ObjectHash_t *objh = data;
|
|
|
|
ObjRef_t **refs, **r;
|
|
|
|
int i;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-21 00:30:47 +00:00
|
|
|
if (mesg.argc < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((refs = (ObjRef_t **) Hash_FindList (objh->objects,
|
|
|
|
mesg.argv[1]))) {
|
|
|
|
if (mesg.argc == 2)
|
|
|
|
for (r = refs; *r; r++) {
|
|
|
|
Hash_DelElement (objh->objects, *r);
|
|
|
|
Hash_Free (objh->objects, *r);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (r = refs; *r; r++)
|
|
|
|
for (i = 2; i < mesg.argc; i++)
|
|
|
|
if (!strcmp (mesg.argv[i],
|
|
|
|
(*r)->obj->handstr))
|
|
|
|
{
|
|
|
|
Hash_DelElement
|
|
|
|
(objh->objects,
|
|
|
|
*r);
|
|
|
|
Hash_Free
|
|
|
|
(objh->objects,
|
|
|
|
*r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GIB_Reply (obj, mesg, 0, NULL);
|
|
|
|
return 0;
|
2012-05-21 23:23:22 +00:00
|
|
|
}
|
2003-09-21 00:30:47 +00:00
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t ObjectHash_methods[] = {
|
2003-09-21 00:30:47 +00:00
|
|
|
{"insert", ObjectHash_Insert_f, NULL},
|
|
|
|
{"get", ObjectHash_Get_f, NULL},
|
|
|
|
{"remove", ObjectHash_Remove_f, NULL},
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_methodtab_t ObjectHash_class_methods[] = {
|
2003-09-21 00:30:47 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-29 02:01:31 +00:00
|
|
|
static gib_classdesc_t ObjectHash_class = {
|
2003-09-21 00:30:47 +00:00
|
|
|
"ObjectHash", "Object",
|
|
|
|
ObjectHash_Construct, NULL,
|
|
|
|
ObjectHash_Destruct,
|
|
|
|
ObjectHash_methods, ObjectHash_class_methods
|
|
|
|
};
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
/*
|
|
|
|
Scripted object class factory
|
|
|
|
|
|
|
|
These functions and structs are used to create a class that executes
|
|
|
|
GIB functions in a new thread in response to messages. This allows classes
|
|
|
|
to be created in GIB itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct Scrobj_s {
|
|
|
|
hashtab_t *shared;
|
|
|
|
} Scrobj_t;
|
|
|
|
|
|
|
|
typedef struct Scrobj_method_s {
|
|
|
|
gib_function_t *func;
|
|
|
|
} Scrobj_method_t;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
Scrobj_Construct (gib_object_t *obj)
|
|
|
|
{
|
2003-10-19 00:51:47 +00:00
|
|
|
//Scrobj_t *new = malloc (sizeof (Scrobj_t));
|
|
|
|
//
|
|
|
|
//new->shared = GIB_Var_Hash_New ();
|
2003-09-11 06:03:13 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
//return new;
|
|
|
|
|
|
|
|
if (!obj->vars)
|
|
|
|
obj->vars = GIB_Var_Hash_New ();
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
return NULL;
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
Scrobj_Class_Construct (gib_object_t *obj)
|
|
|
|
{
|
|
|
|
Scrobj_t *new = malloc (sizeof (Scrobj_t));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
new->shared = GIB_Domain_Get (obj->class->name);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Scrobj_Destruct (void *data)
|
|
|
|
{
|
2003-10-19 00:51:47 +00:00
|
|
|
//Scrobj_t *s = (Scrobj_t *) data;
|
2003-09-11 06:03:13 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
//Hash_DelTable (s->shared);
|
|
|
|
//free (s);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Scrobj_Thread_Died (cbuf_t *thread, void *data)
|
|
|
|
{
|
2003-09-11 08:51:44 +00:00
|
|
|
GIB_Reply (GIB_DATA(thread)->reply.obj, GIB_DATA(thread)->reply.mesg, 0, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Scrobj_Method_f (gib_object_t *obj, gib_method_t *method, void *data,
|
2003-09-11 08:51:44 +00:00
|
|
|
gib_object_t *sender, gib_message_t mesg)
|
2003-09-11 06:03:13 +00:00
|
|
|
{
|
|
|
|
static char this[] = "this";
|
2003-09-11 08:51:44 +00:00
|
|
|
static char send[] = "sender";
|
2003-09-11 06:03:13 +00:00
|
|
|
unsigned int ind;
|
|
|
|
cbuf_t *thread = GIB_Thread_New ();
|
|
|
|
static hashtab_t *nhash = NULL;
|
|
|
|
gib_var_t *var;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
if (GIB_Function_Execute (thread, ((Scrobj_method_t *)method->data)->func,
|
|
|
|
mesg.argv, mesg.argc))
|
|
|
|
return -1;
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
GIB_DATA(thread)->dnotify = Scrobj_Thread_Died;
|
|
|
|
GIB_DATA(thread)->reply.obj = obj;
|
|
|
|
GIB_DATA(thread)->reply.method = method;
|
|
|
|
GIB_DATA(thread)->reply.mesg = mesg;
|
2003-10-19 00:51:47 +00:00
|
|
|
GIB_DATA(thread)->globals = obj->vars;
|
2003-09-11 06:03:13 +00:00
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA(thread)->locals, &nhash, this,
|
|
|
|
&ind, true);
|
|
|
|
if (obj->handle)
|
|
|
|
dsprintf (var->array[0].value, "%lu", obj->handle);
|
|
|
|
else
|
|
|
|
dstring_copystr (var->array[0].value, obj->class->name);
|
2003-09-11 08:51:44 +00:00
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA(thread)->locals, &nhash, send,
|
|
|
|
&ind, true);
|
|
|
|
if (sender)
|
|
|
|
dsprintf (var->array[0].value, "%lu", sender->handle);
|
|
|
|
else
|
|
|
|
dstring_copystr (var->array[0].value, "0");
|
2003-09-11 06:03:13 +00:00
|
|
|
Cbuf_Execute_Stack (thread);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
static void gcbs_mtabfree (void *mtab, void *unused)
|
|
|
|
{
|
|
|
|
free (mtab);
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum {CLASS, INSTANCE} g_gcbs_mode;
|
|
|
|
static const char *g_gcbs_name;
|
|
|
|
|
|
|
|
static const char *gcbs_fname (const char *str)
|
|
|
|
{
|
|
|
|
if (g_gcbs_mode == INSTANCE)
|
2021-01-31 07:01:20 +00:00
|
|
|
return va (0, "__%s_%s__", g_gcbs_name, str);
|
2007-03-22 23:20:57 +00:00
|
|
|
else
|
2021-01-31 07:01:20 +00:00
|
|
|
return va (0, "%s::%s", g_gcbs_name, str);
|
2007-03-22 23:20:57 +00:00
|
|
|
}
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
void
|
|
|
|
GIB_Classes_Build_Scripted (const char *name, const char *parentname,
|
|
|
|
gib_tree_t *tree, gib_script_t *script)
|
|
|
|
{
|
|
|
|
gib_tree_t *line;
|
|
|
|
llist_t *methods, *cmethods;
|
|
|
|
gib_methodtab_t *mtab, *cmtab;
|
|
|
|
gib_classdesc_t desc;
|
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
g_gcbs_mode = INSTANCE;
|
|
|
|
g_gcbs_name = name;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
methods = llist_new (gcbs_mtabfree, NULL, NULL);
|
|
|
|
cmethods = llist_new (gcbs_mtabfree, NULL, NULL);
|
2003-09-11 06:03:13 +00:00
|
|
|
|
|
|
|
for (line = tree; line; line = line->next)
|
|
|
|
switch (line->type) {
|
|
|
|
case TREE_T_LABEL:
|
|
|
|
if (!strcmp (line->str, "class"))
|
2007-03-22 23:20:57 +00:00
|
|
|
g_gcbs_mode = CLASS;
|
2003-09-11 06:03:13 +00:00
|
|
|
else if (!strcmp (line->str, "instance"))
|
2007-03-22 23:20:57 +00:00
|
|
|
g_gcbs_mode = INSTANCE;
|
2003-09-11 06:03:13 +00:00
|
|
|
break;
|
|
|
|
case TREE_T_CMD:
|
|
|
|
if (!strcmp (line->children->str,
|
|
|
|
"function")) {
|
2003-10-19 00:51:47 +00:00
|
|
|
gib_tree_t *cur, *last;
|
2003-09-11 06:03:13 +00:00
|
|
|
gib_methodtab_t *new = malloc (sizeof
|
|
|
|
(gib_methodtab_t));
|
|
|
|
Scrobj_method_t *data = malloc (sizeof
|
|
|
|
(Scrobj_method_t));
|
2003-10-19 00:51:47 +00:00
|
|
|
for (last =
|
|
|
|
line->children->next->next;
|
|
|
|
last->next; last =
|
|
|
|
last->next);
|
2003-09-11 06:03:13 +00:00
|
|
|
data->func = GIB_Function_Define
|
2007-03-22 23:20:57 +00:00
|
|
|
(gcbs_fname
|
2003-09-11 06:03:13 +00:00
|
|
|
(line->children->next->str),
|
2003-10-19 00:51:47 +00:00
|
|
|
last->str,
|
|
|
|
last->children,
|
2003-09-11 06:03:13 +00:00
|
|
|
script, NULL);
|
2003-10-19 00:51:47 +00:00
|
|
|
llist_flush (data->func->arglist);
|
|
|
|
data->func->minargs = 1;
|
|
|
|
for (cur = line->children->next->next;
|
|
|
|
cur != last; cur =
|
|
|
|
cur->next) {
|
|
|
|
llist_append
|
|
|
|
(data->func->arglist,
|
|
|
|
strdup (cur->str));
|
|
|
|
data->func->minargs++;
|
|
|
|
}
|
2003-09-11 06:03:13 +00:00
|
|
|
new->data = data;
|
|
|
|
new->name = line->children->next->str;
|
|
|
|
new->func = Scrobj_Method_f;
|
2007-03-22 23:20:57 +00:00
|
|
|
if (g_gcbs_mode == INSTANCE)
|
2003-09-11 06:03:13 +00:00
|
|
|
llist_append (methods, new);
|
|
|
|
else
|
|
|
|
llist_append (cmethods, new);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
llist_append (methods, calloc (1, sizeof (gib_methodtab_t)));
|
|
|
|
llist_append (cmethods, calloc (1, sizeof (gib_methodtab_t)));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
mtab = llist_createarray (methods, sizeof (gib_methodtab_t));
|
|
|
|
cmtab = llist_createarray (cmethods, sizeof (gib_methodtab_t));
|
|
|
|
|
|
|
|
desc.name = name;
|
|
|
|
desc.parentname = parentname;
|
|
|
|
desc.construct = Scrobj_Construct;
|
|
|
|
desc.class_construct = Scrobj_Class_Construct;
|
|
|
|
desc.destruct = Scrobj_Destruct;
|
|
|
|
desc.methods = mtab;
|
|
|
|
desc.class_methods = cmtab;
|
|
|
|
|
|
|
|
GIB_Class_Create (&desc);
|
|
|
|
|
|
|
|
free (mtab);
|
|
|
|
free (cmtab);
|
|
|
|
llist_delete (methods);
|
|
|
|
llist_delete (cmethods);
|
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
void
|
|
|
|
GIB_Classes_Init (void)
|
|
|
|
{
|
|
|
|
GIB_Class_Create (&Object_class);
|
|
|
|
GIB_Class_Create (&Thread_class);
|
2003-09-21 00:30:47 +00:00
|
|
|
GIB_Class_Create (&ObjectHash_class);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|