diff --git a/Objects/Objects.vcxproj b/Objects/Objects.vcxproj
index 8295c7b..03965af 100644
--- a/Objects/Objects.vcxproj
+++ b/Objects/Objects.vcxproj
@@ -128,16 +128,16 @@
-
+
+
-
-
+
+
-
diff --git a/Objects/Objects.vcxproj.filters b/Objects/Objects.vcxproj.filters
index 65ba7cd..7ade7c4 100644
--- a/Objects/Objects.vcxproj.filters
+++ b/Objects/Objects.vcxproj.filters
@@ -33,13 +33,13 @@
Quelldateien
-
+
Quelldateien
-
+
Quelldateien
-
+
Quelldateien
@@ -50,13 +50,13 @@
Headerdateien
-
+
Headerdateien
-
+
Headerdateien
-
+
Headerdateien
diff --git a/Objects/main.c b/Objects/main.c
index 258268a..f37aa39 100644
--- a/Objects/main.c
+++ b/Objects/main.c
@@ -2,7 +2,15 @@
int main(void) {
+ Array* a = new_Array("double", 10);
+ Object* o = new_Object("double");
+
+ a->a->set(a, o, 0);
+
+ Object* o1 = a->a->get(a, 0);
+
+ printf("%s", BOOL_STRING(instanceOf(o1, "double")));
return 0;
}
\ No newline at end of file
diff --git a/Objects/objects/abstract/object.c b/Objects/objects/abstract/object.c
deleted file mode 100644
index c6a12e4..0000000
--- a/Objects/objects/abstract/object.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include "object.h"
-
-private void* clone(void* o);
-private bool equals(void* o1, void* o2);
-private char* toString(void* o);
-private _uint hashCode(void* o);
-
-Object* new_ObjectType(int32 type)
-{
- Object* o = new_Object();
- o->type = type;
- return o;
-}
-
-Object* new_Object()
-{
- CREATE_STRUCT_HEAP(Object, o);
- o->clone = clone;
- o->equals = equals;
- o->toString = toString;
- o->hashCode = hashCode;
- o->type = Object_t;
- o->magic_string = magic_string;
- return o;
-}
-
-private void* clone(void* o)
-{
- return new_Object();
-}
-
-private bool equals(void* o1, void* o2)
-{
- return o1 == o2;
-}
-
-private char* toString(void* o)
-{
- WRITE_BUFFER_INT(MACRO_BUFFER1, hashCode(o), 16);
- MERGE_STRING_STACK(MACRO_BUFFER2, "Object: 0x", MACRO_BUFFER1);
- return MACRO_BUFFER2;
-}
-
-private _uint hashCode(void* o)
-{
- return (_uint)o;
-}
-
-int32 instanceOf(void* o)
-{
- return (*(Object**)o)->type;
-}
-
-Object* cast_object(void* o)
-{
- if (strcmp(((Object*)o)->magic_string, magic_string) == 0) return o;
- return cast_object((*(Object**)o));
-}
diff --git a/Objects/objects/abstract/object.h b/Objects/objects/abstract/object.h
deleted file mode 100644
index aa2527e..0000000
--- a/Objects/objects/abstract/object.h
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef OBJECT_H
-
-#define OBJECT_H
-
-#include "..\macro.h"
-
-static char* magic_string = "Object";
-
-typedef struct _Object Object, * pObject;
-
-struct _Object {
- char* magic_string;
- int32 type;
- void* (*clone)(void* o);
- bool (*equals)(void* o1, void* o2);
- char* (*toString)(void* o);
- _uint (*hashCode)(void* o);
-};
-
-Object* new_Object();
-Object* new_ObjectType(int32 type);
-Object* cast_object(void* o);
-int32 instanceOf(void* o);
-
-#endif // !OBJECT_H
diff --git a/Objects/objects/base/object.c b/Objects/objects/base/object.c
new file mode 100644
index 0000000..e34cd6c
--- /dev/null
+++ b/Objects/objects/base/object.c
@@ -0,0 +1,45 @@
+#include "object.h"
+
+CAST_FN(Object)
+
+private _int hashcode(void* o);
+private char* toString(void* o);
+private void* clone(void* o);
+private bool equals(void* o1, void* o2);
+
+Object* new_Object(char* className)
+{
+ CREATE_STRUCT_HEAP(Object, o);
+ SET_MAGIC(o, Object);
+ o->className = className;
+ o->hashcode = hashcode;
+ o->toString = toString;
+ o->clone = clone;
+ o->equals = equals;
+ return o;
+}
+
+private _int hashcode(void* o)
+{
+ return (_int)o;
+}
+
+private char* toString(void* o)
+{
+ return CAST(Object, o)->MAGIX;
+}
+
+private void* clone(void* o)
+{
+ return new_Object("Object");
+}
+
+private bool equals(void* o1, void* o2)
+{
+ return CAST(Object, o1)->hashcode == CAST(Object, o2)->hashcode;
+}
+
+bool instanceOf(void* o, char* className)
+{
+ return (strcmp(castObject(o)->className, className) == 0);
+}
diff --git a/Objects/objects/base/object.h b/Objects/objects/base/object.h
new file mode 100644
index 0000000..d4aee9c
--- /dev/null
+++ b/Objects/objects/base/object.h
@@ -0,0 +1,45 @@
+#ifndef OBJECT_H
+
+#define OBJECT_H
+
+#include "..\macro.h"
+
+#define MAGIC char* MAGIX
+#define MAGIX magicString
+#define SET_MAGIC(ptr, className) \
+ ptr->magicString = className##magicString
+
+#define MAGIC_STRING(className) \
+ static char* className##magicString = #className
+
+#define CAST_FN(className) \
+ CAST_FN_PROTO(className) { \
+ className* clazz = o; \
+ if (strcmp(clazz->magicString, className##magicString) == 0) return o; \
+ else return cast##className((*(className**)o)); \
+ }
+
+#define CAST_FN_PROTO(className) \
+ className* cast##className(void* o)
+
+#define CAST(className, ptr) \
+ cast##className(ptr)
+
+MAGIC_STRING(Object);
+
+typedef struct _Object Object, * pObject;
+
+struct _Object {
+ MAGIC;
+ char* className;
+ _int (*hashcode)(void*);
+ char* (*toString)(void*);
+ void* (*clone)(void*);
+ bool (*equals)(void*, void*);
+};
+
+Object* new_Object(char* className);
+CAST_FN_PROTO(Object);
+bool instanceOf(void* o, char* className);
+
+#endif // !OBJECT_H
diff --git a/Objects/objects/macro.h b/Objects/objects/macro.h
index ff2f902..4b700a5 100644
--- a/Objects/objects/macro.h
+++ b/Objects/objects/macro.h
@@ -7,12 +7,6 @@
#include
#include
-enum types {
- Object_t = 0, Array_t,
- double_t,
- types_END
-};
-
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
@@ -51,7 +45,6 @@ static char MACRO_BUFFER2[MACRO_BUFFER_SIZE] = { 0 };
#define STRINGYFY(c) #c
#define ARRAY_SIZE(ptr) (sizeof((ptr)) / sizeof((*ptr)))
-//instance of()
#define CREATE_STRUCT_HEAP(type, id) type* id; MALLOC(id, type, 1);
#endif // !MACRO_H
diff --git a/Objects/objects/stdlib.h b/Objects/objects/stdlib.h
index e9d789f..3fc1bc9 100644
--- a/Objects/objects/stdlib.h
+++ b/Objects/objects/stdlib.h
@@ -3,7 +3,8 @@
#define STDLIB_H
#include "..\objects\macro.h"
-#include "abstract/object.h"
+#include "base/object.h"
#include "util/collections/array.h"
+
#endif // !STDLIB_H
diff --git a/Objects/objects/util/collections/abstractCollection.c b/Objects/objects/util/collections/abstractCollection.c
new file mode 100644
index 0000000..7241b72
--- /dev/null
+++ b/Objects/objects/util/collections/abstractCollection.c
@@ -0,0 +1,30 @@
+#include "abstractCollection.h"
+
+CAST_FN(AbstractCollection)
+
+private _int getSize(void* c);
+
+AbstractCollection* new_AbstractCollection(char* type, _int size)
+{
+ CREATE_STRUCT_HEAP(AbstractCollection, a);
+ a->o = new_Object("AbstractCollection");
+ SET_MAGIC(a, AbstractCollection);
+ a->type = type;
+ a->size = size;
+ a->getSize = getSize;
+ a->set = null;
+ a->get = null;
+ return a;
+}
+
+private _int getSize(void* c)
+{
+ return CAST(AbstractCollection, c)->size;
+}
+
+check_type(void* c, void* e)
+{
+ if (strcmp(CAST(AbstractCollection, c)->type, CAST(Object, e)->className) != 0) {
+ ERROR__("Tried to insert wrong class-type into a collection.");
+ }
+}
diff --git a/Objects/objects/util/collections/abstractCollection.h b/Objects/objects/util/collections/abstractCollection.h
new file mode 100644
index 0000000..1f6ab49
--- /dev/null
+++ b/Objects/objects/util/collections/abstractCollection.h
@@ -0,0 +1,26 @@
+#ifndef ABSTRACTCOLLECTION_H
+
+#define ABSTRACTCOLLECTION
+
+#include "..\..\macro.h"
+#include "..\..\base\object.h"
+
+MAGIC_STRING(AbstractCollection);
+
+typedef struct _AbstractCollection AbstractCollection, * pAbstractCollection;
+
+struct _AbstractCollection {
+ Object* o;
+ MAGIC;
+ char* type;
+ _int size;
+ _int (*getSize)(void*);
+ void* (*get)(void*, _int);
+ void (*set)(void*, void*, _int);
+};
+
+AbstractCollection* new_AbstractCollection(char* type, _int size);
+CAST_FN_PROTO(AbstractCollection);
+check_type(void* c, void* e);
+
+#endif // !ABSTRACTCOLLECTION_H
diff --git a/Objects/objects/util/collections/array.c b/Objects/objects/util/collections/array.c
index 7069a8e..d66ee86 100644
--- a/Objects/objects/util/collections/array.c
+++ b/Objects/objects/util/collections/array.c
@@ -1,146 +1,46 @@
#include "array.h"
-private void add(void* list, void* e);
-private void addIndex(void* list, void* e, _int index);
-private void addCollection(void* list, void* c);
-private void addCollectionIndex(void* list, void* c, _int index);
-private void clear(void* list);
-private bool contains(void* list, void* e);
-private bool containsAll(void* list, void* c);
-private _int indexOf(void* list, void* e);
-private _int lastIndexOf(void* list, void* e);
-private bool isEmpty(void* list);
-private void removeE(void* list, void* e);
-private void removeIndex(void* list, _int index);
-private void removeAll(void* list, void* c);
-private void set(void* list, void* e, _int index);
-private void* get(void* list, _int index);
-private _int getSize(void* list);
-private void* subList(void* list, _int start, _int end);
+CAST_FN(Array)
-Array* new_Array(_int size, int32 type)
+private void set(void* a, void* e, _int index);
+private void* get(void* a, _int index);
+
+private void check_index(void* a, _int index);
+
+Array* new_Array(char* type, _int size)
{
- if (!valid_list_size(size)) ERROR__("Invalid array size.");
CREATE_STRUCT_HEAP(Array, a);
- a->c = new_Collection(type, Array_t, size, add, addIndex, addCollection, addCollectionIndex,
- clear, contains, containsAll, indexOf, lastIndexOf, isEmpty, removeE,
- removeIndex, removeAll, set, get, getSize, subList);
+ if (size <= 0) ERROR__("Invalid array size.");
+ a->a = new_AbstractCollection(type, size);
+ SET_MAGIC(a, Array);
MALLOC(a->e, void*, size);
- for (_int i = 0; i < size; ++i) {
+ for (_int i = 0; i < size; i++) {
a->e[i] = null;
}
+ a->a->o->className = "Array";
+ a->a->get = get;
+ a->a->set = set;
return a;
}
-private void add(void* list, void* e)
-{
-}
-
-private void addIndex(void* list, void* e, _int index)
-{
-}
-
-private void addCollection(void* list, void* c)
-{
-}
-
-private void addCollectionIndex(void* list, void* c, _int index)
-{
-}
-
-private void clear(void* list)
-{
- Array* a = list;
- for (_int i = 0; i < a->c->size; ++i) {
- a->e[i] = null;
- }
-}
-
-private bool contains(void* list, void* e)
-{
- Array* a = list;
- Object* o = cast_object(e);
- for (_int i = 0; i < a->c->size; ++i) {
- if (o->equals(e, a->e[i])) return true;
- }
- return false;
-}
-
-private bool containsAll(void* list, void* c)
-{
- Array* a = list;
- Collection* col = cast_collection(c);
- for (_int i = 0; i < col->size; ++i) {
- if (!contains(list, col->get(c, i))) return false;
- }
- return true;
-}
-
-private _int indexOf(void* list, void* e)
-{
- Array* a = list;
- Object* o = cast_object(e);
- for (_int i = 0; i < a->c->size; ++i) {
- if (o->equals(e, a->e[i])) return i;
- }
- return -1;
-}
-
-private _int lastIndexOf(void* list, void* e)
-{
- Array* a = list;
- Object* o = cast_object(e);
- for (_int i = a->c->size; i >= 0; --i) {
- if (o->equals(e, a->e[i])) return i;
- }
- return -1;
-}
-
-private bool isEmpty(void* list)
-{
- Array* a = list;
- return !(a->e[0]);
-}
-
-private void removeE(void* list, void* e)
-{
- _int index = indexOf(list, e);
- if (index != -1) set(list, null, index);
-}
-
-private void removeIndex(void* list, _int index)
+private void set(void* a, void* e, _int index)
{
- set(list, null, index);
+ check_index(a, index);
+ check_type(a, e);
+ Array* array = a;
+ array->e[index] = e;
}
-private void removeAll(void* list, void* c)
+private void* get(void* a, _int index)
{
- Array* a = list;
- Collection* col = cast_collection(c);
-
+ check_index(a, index);
+ Array* array = a;
+ return array->e[index];
}
-private void set(void* list, void* e, _int index)
+private void check_index(void* a, _int index)
{
- valid_index(list, index);
- valid_type(list, e);
- Array* a = list;
- a->e[index] = e;
-}
-
-private void* get(void* list, _int index)
-{
- valid_index(list, index);
- Array* a = list;
- return a->e[index];
-}
-
-private _int getSize(void* list)
-{
- return cast_collection(list)->size;
-}
-
-private void* subList(void* list, _int start, _int end)
-{
- return null;
-}
+ Array* array = a;
+ if (index < 0 || index >= array->a->getSize(a))
+ ERROR__("Array index out of bounds.");
+}
\ No newline at end of file
diff --git a/Objects/objects/util/collections/array.h b/Objects/objects/util/collections/array.h
index 466636e..640be4d 100644
--- a/Objects/objects/util/collections/array.h
+++ b/Objects/objects/util/collections/array.h
@@ -3,15 +3,19 @@
#define ARRAY_H
#include "..\..\macro.h"
-#include "..\collections\collection.h"
+#include "..\collections\abstractCollection.h"
+
+MAGIC_STRING(Array);
typedef struct _Array Array, * pArray;
struct _Array {
- Collection* c;
+ AbstractCollection* a;
+ MAGIC;
void** e;
};
-Array* new_Array(_int size, int32 type);
+Array* new_Array(char* type, _int size);
+CAST_FN_PROTO(Array);
#endif // !ARRAY_H
diff --git a/Objects/objects/util/collections/collection.c b/Objects/objects/util/collections/collection.c
deleted file mode 100644
index 032acbb..0000000
--- a/Objects/objects/util/collections/collection.c
+++ /dev/null
@@ -1,66 +0,0 @@
-#include "collection.h"
-
-Collection* new_Collection(int32 type, int32 collectionType, _int size,
- void (*add)(void* list, void* e),
- void (*addIndex)(void* list, void* e, _int index),
- void (*addCollection)(void* list, void* c),
- void (*addCollectionIndex)(void* list, void* c, _int index),
- void (*clear)(void* list),
- bool (*contains)(void* list, void* e),
- bool (*containsAll)(void* list, void* c),
- _int(*indexOf)(void* list, void* e),
- _int(*lastIndexOf)(void* list, void* e),
- bool (*isEmpty)(void* list),
- void (*removeE)(void* list, void* e),
- void (*removeIndex)(void* list, _int index),
- void (*removeAll)(void* list, void* c),
- void (*set)(void* list, void* e, _int index),
- void* (*get)(void* list, _int index),
- _int (*getSize)(void* list),
- void* (*subList)(void* list, _int start, _int end))
-{
- CREATE_STRUCT_HEAP(Collection, c);
- c->add = add;
- c->addIndex = addIndex;
- c->addCollection = addCollection;
- c->addCollectionIndex = addCollectionIndex;
- c->clear = clear;
- c->contains = contains;
- c->containsAll = containsAll;
- c->indexOf = indexOf;
- c->lastIndexOf = lastIndexOf;
- c->isEmpty = isEmpty;
- c->removeE = removeE;
- c->removeIndex = removeIndex;
- c->removeAll = removeAll;
- c->set = set;
- c->get = get;
- c->getSize = getSize;
- c->subList = subList;
- c->size = size;
- c->type = type;
- c->collectionType = collectionType;
- c->o = new_ObjectType(collectionType);
- return c;
-}
-
-Collection* cast_collection(void* c)
-{
- return (*(Collection**)c);
-}
-
-bool valid_list_size(_int size)
-{
- if (size <= 0) return false;
- else return true;
-}
-
-void valid_index(void* c, _int index)
-{
- if (index < 0 || index >= cast_collection(c)->size) ERROR__("Index out of bounds.");
-}
-
-void valid_type(void* c, void* e)
-{
- if (instanceOf(cast_object(e)) != cast_collection(c)->type) ERROR__("Wrong type in collection.");
-}
\ No newline at end of file
diff --git a/Objects/objects/util/collections/collection.h b/Objects/objects/util/collections/collection.h
deleted file mode 100644
index 1924628..0000000
--- a/Objects/objects/util/collections/collection.h
+++ /dev/null
@@ -1,58 +0,0 @@
-#ifndef COLLECTION_H
-
-#define COLLECTION_H
-
-#include "..\..\macro.h"
-#include "..\..\abstract\object.h"
-
-typedef struct _Collection Collection, * pCollection;
-
-struct _Collection {
- Object* o;
- int32 type;
- int32 collectionType;
- _int size;
- void (*add)(void* list, void* e);
- void (*addIndex)(void* list, void* e, _int index);
- void (*addCollection)(void* list, void* c);
- void (*addCollectionIndex)(void* list, void* c, _int index);
- void (*clear)(void* list);
- bool (*contains)(void* list, void* e);
- bool (*containsAll)(void* list, void* c);
- _int(*indexOf)(void* list, void* e);
- _int(*lastIndexOf)(void* list, void* e);
- bool (*isEmpty)(void* list);
- void (*removeE)(void* list, void* e);
- void (*removeIndex)(void* list, _int index);
- void (*removeAll)(void* list, void* c);
- void (*set)(void* list, void* e, _int index);
- void* (*get)(void* list, _int index);
- _int (*getSize)(void* list);
- void* (*subList)(void* list, _int start, _int end);
-};
-
-Collection* new_Collection(int32 type, int32 collectionType, _int size,
- void (*add)(void* list, void* e),
- void (*addIndex)(void* list, void* e, _int index),
- void (*addCollection)(void* list, void* c),
- void (*addCollectionIndex)(void* list, void* c, _int index),
- void (*clear)(void* list),
- bool (*contains)(void* list, void* e),
- bool (*containsAll)(void* list, void* c),
- _int(*indexOf)(void* list, void* e),
- _int(*lastIndexOf)(void* list, void* e),
- bool (*isEmpty)(void* list),
- void (*removeE)(void* list, void* e),
- void (*removeIndex)(void* list, _int index),
- void (*removeAll)(void* list, void* c),
- void (*set)(void* list, void* e, _int index),
- void* (*get)(void* list, _int index),
- _int (*getSize)(void* list),
- void* (*subList)(void* list, _int start, _int end));
-
-Collection* cast_collection(void* c);
-bool valid_list_size(_int size);
-void valid_index(void* c, _int index);
-void valid_type(void* c, void* e);
-
-#endif //!COLLECTION_H