Remove snax' object module.

We don't use it for anything. If we ever want it again, we can just pull
it from history.
This commit is contained in:
Bill Currie 2012-02-08 15:21:57 +09:00
parent 5c062acc80
commit fe3f0fa7ce
11 changed files with 0 additions and 1189 deletions

View file

@ -1,239 +0,0 @@
/*
ArrayList.c
Implements List by storing elements in a dynamic array
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 03, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/classes/ArrayList.h"
static ObjRefs_t *
ArrayListIterator_AllRefs_f (Object *self)
{
return &ARRAYLISTITERATOR(self)->allrefs;
}
static Object *
ArrayListIterator_Next_f (Iterator *self)
{
/* Was list structurally modified while we are iterating? */
if (ARRAYLISTITERATOR(self)->smods != LIST(ARRAYLISTITERATOR(self)->list)->smods) {
ARRAYLISTITERATOR(self)->alive = false;
// FIXME: Throw an exception or something
}
if (ARRAYLISTITERATOR(self)->alive) {
Object *o = ARRAYLISTITERATOR(self)->list->elements[ARRAYLISTITERATOR(self)->pos++];
if (ARRAYLISTITERATOR(self)->pos >= COLLECTION(ARRAYLISTITERATOR(self)->list)->count)
ARRAYLISTITERATOR(self)->alive = false;
return o;
}
return NULL;
}
static qboolean
ArrayListIterator_HasNext_f (Iterator *self)
{
if (ARRAYLISTITERATOR(self)->smods != LIST(ARRAYLISTITERATOR(self)->list)->smods)
ARRAYLISTITERATOR(self)->alive = false;
return ARRAYLISTITERATOR(self)->alive;
}
static Object *
ArrayListIterator_Init_f (Object *self, ArrayList *list)
{
superInit (ArrayListIterator, self);
self->allRefs = ArrayListIterator_AllRefs_f;
ITERATOR(self)->next = ArrayListIterator_Next_f;
ITERATOR(self)->hasNext = ArrayListIterator_HasNext_f;
ARRAYLISTITERATOR(self)->list = ARRAYLIST(list);
ARRAYLISTITERATOR(self)->allrefs.objs = (Object **)&ARRAYLISTITERATOR(self)->list;
ARRAYLISTITERATOR(self)->allrefs.count = 1;
ARRAYLISTITERATOR(self)->allrefs.next = NULL;
ARRAYLISTITERATOR(self)->smods = LIST(list)->smods;
ARRAYLISTITERATOR(self)->pos = 0;
ARRAYLISTITERATOR(self)->alive = COLLECTION(list)->count ? true : false;
return self;
}
static void
ArrayListIterator_Deinit_f (Object *self)
{
}
static ObjRefs_t *
ArrayList_AllRefs_f (Object *self)
{
ARRAYLIST(self)->allrefs.objs = ARRAYLIST(self)->elements;
ARRAYLIST(self)->allrefs.count = COLLECTION(self)->count;
ARRAYLIST(self)->allrefs.next = NULL;
return &ARRAYLIST(self)->allrefs;
}
static void
ArrayList_EnsureCapacity (ArrayList *list, unsigned int size)
{
if (list->realsize < size) {
unsigned int newsize = size | (list->realsize + list->realsize/2);
list->elements = realloc (list->elements, newsize);
memset (list->elements + list->realsize, 0, newsize - list->realsize);
list->realsize = newsize;
}
}
static qboolean
ArrayList_Set_f (List *self, unsigned int index, Object *o)
{
ArrayList *list = ARRAYLIST(self);
if (o) {
if (!Object_InstanceOf (o, COLLECTION(list)->type))
return false;
ArrayList_EnsureCapacity (list, index+1);
list->elements[index] = o;
} else if (index < COLLECTION(list)->count) {
list->elements[index] = NULL;
}
if (COLLECTION(list)->count < index+1)
COLLECTION(list)->count = index+1;
return true;
}
static Object *
ArrayList_Get_f (List *self, unsigned int index)
{
ArrayList *list = ARRAYLIST(self);
if (index >= COLLECTION(list)->count || index >= list->realsize)
return NULL;
else
return list->elements[index];
}
static void
ArrayList_MakeRoomAt (ArrayList *list, unsigned int index)
{
ArrayList_EnsureCapacity(list, ++COLLECTION(list)->count);
memmove(list->elements+index+1, list->elements+index, sizeof(Object *) * (COLLECTION(list)->count-index-1));
LIST(list)->smods++;
}
static qboolean
ArrayList_InsertAt_f (List *self, unsigned int index, Object *o)
{
ArrayList *list = ARRAYLIST(self);
if (index >= COLLECTION(list)->count)
return ArrayList_Set_f (self, index, o);
else if (o && !Object_InstanceOf(o, COLLECTION(list)->type))
return false;
else {
ArrayList_MakeRoomAt (list, index);
list->elements[index] = o;
return true;
}
}
static Object *
ArrayList_RemoveAt_f (List *self, unsigned int index)
{
ArrayList *list = ARRAYLIST(self);
if (index >= COLLECTION(list)->count)
return NULL;
else {
Object *o = list->elements[index];
COLLECTION(list)->count--;
memmove(list->elements+index, list->elements+index+1, sizeof(Object *) * (COLLECTION(list)->count-index));
LIST(list)->smods++;
return o;
}
}
static qboolean
ArrayList_Add_f (Collection *self, Object *o)
{
ArrayList *list = ARRAYLIST(self);
if (o && !Object_InstanceOf(o, COLLECTION(list)->type))
return false;
else {
ArrayList_EnsureCapacity (list, ++COLLECTION(list)->count);
list->elements[COLLECTION(list)->count-1] = o;
return true;
}
}
static Iterator *
ArrayList_Iterator_f (Collection *self)
{
return new(ArrayListIterator, ARRAYLIST(self));
}
static Object *
ArrayList_Init_f (Object *self, Class *type, Collection *source)
{
ARRAYLIST(self)->realsize = 32;
ARRAYLIST(self)->elements = calloc(32, sizeof(Object *));
LIST(self)->set = ArrayList_Set_f;
LIST(self)->get = ArrayList_Get_f;
LIST(self)->insertAt = ArrayList_InsertAt_f;
LIST(self)->removeAt = ArrayList_RemoveAt_f;
COLLECTION(self)->add = ArrayList_Add_f;
COLLECTION(self)->iterator = ArrayList_Iterator_f;
superInit(List, self, type, source);
self->allRefs = ArrayList_AllRefs_f;
return self;
}
static void
ArrayList_Deinit_f (Object *self)
{
free(ARRAYLIST(self)->elements);
}
classInitFunc(ArrayListIterator) {
classObj (ArrayListIterator) = new (Class,
"ArrayListIterator", sizeof(ArrayListIterator), classObj(Iterator),
ArrayListIterator_Init_f, ArrayListIterator_Deinit_f, false);
}
classInitFunc(ArrayList) {
classInit (ArrayListIterator);
classObj (ArrayList) = new (Class,
"ArrayList", sizeof(ArrayList), classObj(List),
ArrayList_Init_f, ArrayList_Deinit_f, false);
}

View file

@ -1,106 +0,0 @@
/*
Collection.c
Abstract class for collections of objects
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 03, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include "QF/dstring.h"
#include "QF/classes/Collection.h"
static String *
Collection_ToString_f (Object *self)
{
Iterator *iter = methodCall(COLLECTION(self), iterator);
dstring_t *dstr = dstring_newstr();
Object *o;
String *ret;
dstring_appendstr (dstr, "[");
while ((o = methodCall(iter, next))) {
String *s = methodCall(o, toString);
dstring_appendstr (dstr, s->str);
if (methodCall(iter, hasNext))
dstring_appendstr (dstr, ", ");
}
dstring_appendstr (dstr, "]");
ret = new(String, dstr->str);
dstring_delete (dstr);
return ret;
}
static qboolean
Collection_Contains_f (Collection *self, Object *test)
{
Iterator *iter = methodCall(self, iterator);
Object *o;
while ((o = methodCall(iter, next)))
if (o == test)
return true;
return false;
}
static Object *
Collection_Init_f (Object *self, Class *type, Collection *source)
{
superInit(Collection, self);
COLLECTION(self)->contains = Collection_Contains_f;
COLLECTION(self)->count = 0;
COLLECTION(self)->type = type;
self->toString = Collection_ToString_f;
// FIXME allow for subclasses too
if (source && source->type == type) {
Iterator *iter = methodCall(source, iterator);
Object *o;
while ((o = methodCall(iter, next)))
methodCall(COLLECTION(self), add, o);
}
return self;
}
static void
Collection_Deinit_f (Object *self)
{
}
classInitFunc(Collection) {
classObj (Collection) = new (Class,
"Collection", sizeof(Collection), classObj(Object),
Collection_Init_f, Collection_Deinit_f, true);
}

View file

@ -1,80 +0,0 @@
/*
Double.c
Class for double values
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 06, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
#include "QF/classes/Double.h"
#include "QF/va.h"
static String *
Double_ToString_f (Object *self)
{
return new(String, va("%f", DOUBLE(self)->value));
}
static int
Double_IntValue_f (Number *self)
{
return DOUBLE(self)->value;
}
static double
Double_DoubleValue_f (Number *self)
{
return (int) DOUBLE(self)->value;
}
static Object *
Double_Init_f (Object *self, double value)
{
superInit(Double, self);
DOUBLE(self)->value = value;
NUMBER(self)->intValue = Double_IntValue_f;
NUMBER(self)->doubleValue = Double_DoubleValue_f;
self->toString = Double_ToString_f;
return self;
}
static void
Double_Deinit_f (Object *self)
{
}
classInitFunc(Double)
{
classObj(Double) = new(Class, "Double", sizeof(Double), classObj(Number), Double_Init_f, Double_Deinit_f, false);
}

View file

@ -1,80 +0,0 @@
/*
Integer.c
Class for integer values
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 06, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
#include "QF/classes/Integer.h"
#include "QF/va.h"
static String *
Integer_ToString_f (Object *self)
{
return new (String, va("%i", INTEGER(self)->value));
}
static int
Integer_IntValue_f (Number *self)
{
return INTEGER(self)->value;
}
static double
Integer_DoubleValue_f (Number *self)
{
return (double) INTEGER(self)->value;
}
static Object *
Integer_Init_f (Object *self, int value)
{
superInit(Integer, self);
INTEGER(self)->value = value;
NUMBER(self)->intValue = Integer_IntValue_f;
NUMBER(self)->doubleValue = Integer_DoubleValue_f;
self->toString = Integer_ToString_f;
return self;
}
static void
Integer_Deinit_f (Object *self)
{
}
classInitFunc(Integer)
{
classObj(Integer) = new(Class, "Integer", sizeof(Integer), classObj(Number), Integer_Init_f, Integer_Deinit_f, false);
}

View file

@ -1,56 +0,0 @@
/*
Iterator.c
Abstract class for Collection iterators
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 03, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include "QF/classes/Iterator.h"
static Object *
Iterator_Init_f (Object *self)
{
superInit(Iterator, self);
return self;
}
static void
Iterator_Deinit_f (Object *self)
{
}
classInitFunc(Iterator) {
classObj (Iterator) = new (Class,
"Iterator", sizeof(Iterator), classObj(Object),
Iterator_Init_f, Iterator_Deinit_f, true);
}

View file

@ -1,75 +0,0 @@
/*
List.c
Abstract class for lists
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 03, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include "QF/classes/List.h"
static Object *
List_Remove_f (Collection *self, Object *o)
{
unsigned int i = 0;
Object *test;
Iterator *iter = methodCall(self, iterator);
while ((test = methodCall(iter, next))) {
if (test == o) {
methodCall(LIST(self), removeAt, i);
return o;
}
i++;
}
return NULL;
}
static Object *
List_Init_f (Object *self, Class *type, Collection *source)
{
superInit(List, self, type, source);
COLLECTION(self)->remove = List_Remove_f;
LIST(self)->smods = 0;
return self;
}
static void
List_Deinit_f (Object *self)
{
}
classInitFunc(List) {
classObj (List) = new (Class,
"List", sizeof(List), classObj(Collection),
List_Init_f, List_Deinit_f, true);
}

View file

@ -1,16 +0,0 @@
AUTOMAKE_OPTIONS= foreign
AM_CFLAGS= @PREFER_PIC@
INCLUDES= -I$(top_srcdir)/include
lib_ldflags=-version-info $(QUAKE_LIBRARY_VERSION_INFO) \
-rpath $(libdir) -no-undefined
object_libs=$(top_builddir)/libs/util/libQFutil.la
lib_LTLIBRARIES= libQFobject.la
libQFobject_la_LDFLAGS= $(lib_ldflags)
libQFobject_la_LIBADD= $(object_libs)
libQFobject_la_DEPENDENCIES=$(object_libs)
libQFobject_la_SOURCES= \
ArrayList.c Collection.c Double.c Integer.c Iterator.c \
List.c Number.c Selector.c String.c garbage.c object.c

View file

@ -1,57 +0,0 @@
/*
Number.c
Abstract class for numeric values
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 06, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
#include "QF/classes/Number.h"
static Object *
Number_Init_f (Object *self)
{
superInit(String, self);
return self;
}
static void
Number_Deinit_f (Object *self)
{
}
classInitFunc(Number)
{
classObj(Number) = new(Class, "Number", sizeof(Number), classObj(Object), Number_Init_f, Number_Deinit_f, true);
}

View file

@ -1,73 +0,0 @@
/*
String.c
Class for containing immutable strings
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: December 03, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/classes/String.h"
static String *
String_ToString_f (Object *self)
{
return STRING(self);
}
static Object *
String_Init_f (Object *self, const char *value)
{
superInit(String, self);
self->toString = String_ToString_f;
STRING(self)->str = strdup (value);
return self;
}
static void
String_Deinit_f (Object *self)
{
free((void *)STRING(self)->str);
}
classInitFunc(String)
{
classObj(String) = new(Class, "String", sizeof(String), classObj(Object), String_Init_f, String_Deinit_f, false);
}

View file

@ -1,120 +0,0 @@
/*
garbage.c
Object system garbage collector.
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: November 28, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
#include "QF/sys.h"
#include "QF/object.h"
#include "garbage.h"
Object *junk = NULL;
unsigned int junked = 0;
void
Garbage_Do_Mark (Object *root)
{
if (!root->marked) {
ObjRefs_t *allrefs;
root->marked = true;
Sys_MaskPrintf (SYS_DEV, "GC: Marked %s@%p.\n", root->cl->name, root);
if (root->allRefs)
for (allrefs = methodCall(root, allRefs); allrefs;
allrefs = allrefs->next) {
unsigned int i;
for (i = 0; i < allrefs->count; i++)
Garbage_Do_Mark (allrefs->objs[i]);
}
}
}
Object *
Garbage_Do_Sweep (Object **allobjs)
{
Object **prevNext;
Object *obj;
for (prevNext = allobjs, obj = *allobjs;; obj = *prevNext) {
if (obj->marked) {
obj->marked = false;
prevNext = &obj->next;
} else if (!obj->refs) {
if (!obj->finalized && methodCall(obj, finalize)) {
obj->finalized = true;
Garbage_Do_Mark (obj);
prevNext = &obj->next;
} else {
*prevNext = obj->next;
obj->next = junk;
junk = obj;
junked++;
Sys_MaskPrintf (SYS_DEV,
"GC: %s@%p is ready for disposal...\n",
obj->cl->name, obj);
}
} else
*prevNext = obj->next;
if (!*prevNext)
return obj;
}
}
unsigned int
Garbage_Pending (void)
{
return junked;
}
void
Garbage_Dispose (Object **allobjs, unsigned int amount)
{
Object *next;
for (; junk && amount; junk = next, amount--) {
next = junk->next;
if (junk->marked) {
junk->marked = false;
junk->next = *allobjs;
*allobjs = junk;
} else {
Sys_MaskPrintf (SYS_DEV, "GC: Disposing of %s@%p...\n",
junk->cl->name, junk);
Object_Delete (junk);
junked--;
}
}
}

View file

@ -1,287 +0,0 @@
/*
object.c
Provides a primitive object framework to back objects
in higher level languages of QF so that they can share
objects. For example, Ruamoko and GIB would be able to
pass String objects to each other, even if the higher
-level implementations of String in each language differ.
Copyright (C) 2003 Brian Koropoff
Author: Brian Koropoff
Date: November 28, 2003
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
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/dstring.h"
#include "QF/sys.h"
#include "QF/object.h"
#include "QF/va.h"
#include "garbage.h"
#include "QF/classes/ArrayList.h"
#include "QF/classes/Integer.h"
#include "QF/classes/Double.h"
Object *allObjs = NULL;
ArrayList *rootObj = NULL;
static qboolean
Object_Finalize_f (Object *self)
{
return false;
}
static String *
Object_ToString_f (Object *self)
{
return new(String, va("%s@%p", self->cl->name, self));
}
static void
Object_Init_f (Object *self)
{
self->allRefs = NULL;
self->toString = Object_ToString_f;
self->finalize = Object_Finalize_f;
Sys_DPrintf("%s@%p initing...\n", self->cl->name, self);
}
static void
Object_Deinit_f (Object *self)
{
Sys_DPrintf("%s@%p deiniting...\n", self->cl->name, self);
free (self);
}
static String *
Class_ToString_f (Object *self)
{
return new(String, CLASS(self)->name);
}
static Object *
Class_Init_f (Object *self, const char *name, unsigned int size, Class *parent, void *init, void *deinit, qboolean abstract)
{
superInit(Class, self);
CLASS(self)->name = strdup(name);
CLASS(self)->size = size;
CLASS(self)->parent = parent;
CLASS(self)->init = (Object_Init_t) init;
CLASS(self)->deinit = (Object_Deinit_t) deinit;
self->toString = Class_ToString_f;
return self;
}
static void
Class_Deinit_f (Object *self) {
free ((void *)CLASS(self)->name);
}
Object *
Object_Create (Class *cl, qboolean perm)
{
Object *new;
if (cl->abstract)
return NULL;
new = calloc (1, cl->size);
new->cl = cl;
new->marked = false;
new->finalized = false;
if (perm || cl->alwaysperm) {
new->refs = 1;
new->nogc = true;
} else {
new->refs = 0;
new->nogc = false;
new->next = allObjs;
allObjs = new;
}
return new;
}
void
Object_Delete (Object *obj)
{
Class *c;
for (c = obj->cl; c; c = c->parent)
c->deinit (obj);
}
Object *
Object_Retain (Object *obj)
{
obj->refs++;
return obj;
}
Object *
Object_Release (Object *obj)
{
if (obj->refs)
obj->refs--;
if (obj->nogc && !obj->refs) {
if (!obj->finalized) {
obj->finalized = true;
methodCall(obj, finalize);
}
if (!obj->refs) {
Object_Delete (obj);
return NULL;
}
}
return obj;
}
qboolean
Object_InstanceOf (Object *obj, Class *cl)
{
Class *c;
for (c = obj->cl; c; c = c->parent)
if (c == cl)
return true;
return false;
}
void
Object_AddToRoot (Object *obj)
{
methodCall(COLLECTION(rootObj), add, obj);
}
void
Object_RemoveFromRoot (Object *obj)
{
methodCall(COLLECTION(rootObj), remove, obj);
}
static void
Object_Test (void)
{
String *liststr;
Collection *list = new(ArrayList, classObj(Object), NULL);
methodCall(list, add, new(String, "Testing..."));
methodCall(list, add, new(String, "One"));
methodCall(list, add, new(Integer, 2));
methodCall(list, add, new(Double, 3.0));
liststr = methodCall(OBJECT(list), toString);
Sys_DPrintf("List: %s\n", liststr->str);
methodCall(LIST(list), removeAt, 2);
liststr = methodCall(OBJECT(list), toString);
Sys_DPrintf("List: %s\n", liststr->str);
methodCall(LIST(list), insertAt, 2, new(String, "Mr. Two!"));
liststr = methodCall(OBJECT(list), toString);
Sys_DPrintf("List: %s\n", liststr->str);
list = new(ArrayList, classObj(Object), NULL);
methodCall(list, add, new(String, "Don't free me!"));
methodCall(list, add, new(Integer, 5));
methodCall(list, add, new(Double, 3.14));
Object_AddToRoot (OBJECT(methodCall(list, iterator)));
}
Class * classObj(Object);
Class * classObj(Class);
void
Object_Init (void)
{
/* There is somewhat of a chicken and egg problem
here.
*/
classObj(Object) = malloc (sizeof (Class));
classObj(Class) = malloc (sizeof (Class));
OBJECT(classObj(Object))->cl = Class_class;
OBJECT(classObj(Class))->cl = Class_class;
classObj(Class)->parent = classObj(Class);
classObj(Object)->init = (Object_Init_t) Object_Init_f;
classObj(Class)->alwaysperm = true;
OBJECT(classObj(Class))->nogc = OBJECT(classObj(Object))->nogc = true;
Class_Init_f (OBJECT(classObj(Object)), "Object", sizeof(Object), NULL, Object_Init_f, Object_Deinit_f, true);
Class_Init_f (OBJECT(classObj(Class)), "Class", sizeof(Class), classObj(Object), Class_Init_f, Class_Deinit_f, false);
retain(classObj(Object));
retain(classObj(Class));
/* Phew... Object and Class are now bootstrapped,
classes can now be created by instantiating
Class
*/
/* Initialize standard classes */
classInit(String);
classInit(Number);
classInit(Integer);
classInit(Double);
classInit(Iterator);
classInit(Collection);
classInit(List);
classInit(ArrayList);
rootObj = new(ArrayList, classObj(Object), NULL);
/* Run test */
Object_Test();
}
void
Object_Garbage_Collect (void)
{
static unsigned int frames = 0;
frames++;
if (frames % 2000 == 0) {
Object *all, *last;
Sys_DPrintf("GC: Marking...\n");
Garbage_Do_Mark (OBJECT(rootObj));
Sys_DPrintf("GC: Sweeping...\n");
all = allObjs;
allObjs = NULL;
last = Garbage_Do_Sweep (&all);
last->next = allObjs;
allObjs = all;
}
if (frames % 50 == 0 && Garbage_Pending()) {
Sys_DPrintf("GC: Disposing...\n");
Garbage_Dispose (&allObjs, Garbage_Pending()/2 + 1);
}
}