From fe04cef0515afffcafa1d197191642122b6768e7 Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 19:41:54 +0100 Subject: [PATCH 1/6] init --- Objects/Objects.vcxproj | 6 - Objects/Objects.vcxproj.filters | 18 --- Objects/objects/abstract/object.c | 58 ------- Objects/objects/abstract/object.h | 25 --- Objects/objects/util/collections/array.c | 146 ------------------ Objects/objects/util/collections/array.h | 17 -- Objects/objects/util/collections/collection.c | 66 -------- Objects/objects/util/collections/collection.h | 58 ------- 8 files changed, 394 deletions(-) delete mode 100644 Objects/objects/abstract/object.c delete mode 100644 Objects/objects/abstract/object.h delete mode 100644 Objects/objects/util/collections/array.c delete mode 100644 Objects/objects/util/collections/array.h delete mode 100644 Objects/objects/util/collections/collection.c delete mode 100644 Objects/objects/util/collections/collection.h diff --git a/Objects/Objects.vcxproj b/Objects/Objects.vcxproj index 8295c7b..b775da9 100644 --- a/Objects/Objects.vcxproj +++ b/Objects/Objects.vcxproj @@ -128,16 +128,10 @@ - - - - - - diff --git a/Objects/Objects.vcxproj.filters b/Objects/Objects.vcxproj.filters index 65ba7cd..399dd39 100644 --- a/Objects/Objects.vcxproj.filters +++ b/Objects/Objects.vcxproj.filters @@ -33,15 +33,6 @@ Quelldateien - - Quelldateien - - - Quelldateien - - - Quelldateien - @@ -50,14 +41,5 @@ Headerdateien - - Headerdateien - - - Headerdateien - - - Headerdateien - \ 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/util/collections/array.c b/Objects/objects/util/collections/array.c deleted file mode 100644 index 7069a8e..0000000 --- a/Objects/objects/util/collections/array.c +++ /dev/null @@ -1,146 +0,0 @@ -#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); - -Array* new_Array(_int size, int32 type) -{ - 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); - MALLOC(a->e, void*, size); - for (_int i = 0; i < size; ++i) { - a->e[i] = null; - } - 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) -{ - set(list, null, index); -} - -private void removeAll(void* list, void* c) -{ - Array* a = list; - Collection* col = cast_collection(c); - -} - -private void set(void* list, void* e, _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; -} diff --git a/Objects/objects/util/collections/array.h b/Objects/objects/util/collections/array.h deleted file mode 100644 index 466636e..0000000 --- a/Objects/objects/util/collections/array.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef ARRAY_H - -#define ARRAY_H - -#include "..\..\macro.h" -#include "..\collections\collection.h" - -typedef struct _Array Array, * pArray; - -struct _Array { - Collection* c; - void** e; -}; - -Array* new_Array(_int size, int32 type); - -#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 From 6c4f1059d5debbcef7cdcb770525895244b13c54 Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 20:20:37 +0100 Subject: [PATCH 2/6] cast macro --- Objects/Objects.vcxproj | 4 ++ Objects/Objects.vcxproj.filters | 12 +++++ Objects/objects/base/object.c | 45 +++++++++++++++++++ Objects/objects/base/object.h | 35 +++++++++++++++ Objects/objects/stdlib.h | 3 +- .../util/collections/abstractCollection.c | 32 +++++++++++++ .../util/collections/abstractCollection.h | 20 +++++++++ 7 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 Objects/objects/base/object.c create mode 100644 Objects/objects/base/object.h create mode 100644 Objects/objects/util/collections/abstractCollection.c create mode 100644 Objects/objects/util/collections/abstractCollection.h diff --git a/Objects/Objects.vcxproj b/Objects/Objects.vcxproj index b775da9..4c5f24b 100644 --- a/Objects/Objects.vcxproj +++ b/Objects/Objects.vcxproj @@ -128,10 +128,14 @@ + + + + diff --git a/Objects/Objects.vcxproj.filters b/Objects/Objects.vcxproj.filters index 399dd39..22ae034 100644 --- a/Objects/Objects.vcxproj.filters +++ b/Objects/Objects.vcxproj.filters @@ -33,6 +33,12 @@ Quelldateien + + Quelldateien + + + Quelldateien + @@ -41,5 +47,11 @@ Headerdateien + + Headerdateien + + + Headerdateien + \ No newline at end of file diff --git a/Objects/objects/base/object.c b/Objects/objects/base/object.c new file mode 100644 index 0000000..0ea6b90 --- /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); + o->magicString = Object_magicString; + o->className = className; + o->hashcode = hashcode; + o->toString = toString; + o->clone = clone; + o->equals = equals; + return o; +} + +private _int hashcode(void* o) +{ + +} + +private char* toString(void* o) +{ + +} + +private void* clone(void* o) +{ + +} + +private bool equals(void* o1, void* o2) +{ + +} + +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..3243a12 --- /dev/null +++ b/Objects/objects/base/object.h @@ -0,0 +1,35 @@ +#ifndef OBJECT_H + +#define OBJECT_H + +#include "..\macro.h" + +#define CAST_FN(className) \ + CAST_FN_PROTO(className) { \ + className* name = o; \ + if (strcmp(name->magicString, className##_magicString) == 0) return o; \ + else return cast##className((*(className**)o)); \ + } + +#define CAST_FN_PROTO(className) \ + className* cast##className(void* o) + +static char* Object_magicString = "Object"; + +typedef struct _Object Object, * pObject; + +struct _Object { + char* magicString; + 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/stdlib.h b/Objects/objects/stdlib.h index e9d789f..b64e40e 100644 --- a/Objects/objects/stdlib.h +++ b/Objects/objects/stdlib.h @@ -3,7 +3,6 @@ #define STDLIB_H #include "..\objects\macro.h" -#include "abstract/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..0ab047b --- /dev/null +++ b/Objects/objects/util/collections/abstractCollection.c @@ -0,0 +1,32 @@ +#include "abstractCollection.h" + +private _int getSize(void* c); +private void* get(void* c, _int index); +private void set(void* c, void* e, _int index); + +AbstractCollection* new_AbstractCollection(char* classNames, _int size) +{ + CREATE_STRUCT_HEAP(AbstractCollection, a); + a->o = new_Object("AbstractCollection"); + a->size = size; + a->getSize = getSize; + a->set = set; + a->get = get; + return a; +} + +private _int getSize(void* c) +{ + +} + +private void* get(void* c, _int index) +{ + +} + +private void set(void* c, void* e, _int index) +{ + +} + diff --git a/Objects/objects/util/collections/abstractCollection.h b/Objects/objects/util/collections/abstractCollection.h new file mode 100644 index 0000000..3960080 --- /dev/null +++ b/Objects/objects/util/collections/abstractCollection.h @@ -0,0 +1,20 @@ +#ifndef ABSTRACTCOLLECTION_H + +#define ABSTRACTCOLLECTION + +#include "..\..\macro.h" +#include "..\..\base\object.h" + +typedef struct _AbstractCollection AbstractCollection, * pAbstractCollection; + +struct _AbstractCollection { + Object* o; + _int size; + _int (*getSize)(void*); + void* (*get)(void*, _int); + void (*set)(void*, void*, _int); +}; + +AbstractCollection* new_AbstractCollection(char* classNames, _int size); + +#endif // !ABSTRACTCOLLECTION_H From 73b94b347909984774e123710ee96bcf3c1f763d Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 20:23:40 +0100 Subject: [PATCH 3/6] magic string --- Objects/objects/base/object.h | 1 - Objects/objects/util/collections/abstractCollection.c | 3 +++ Objects/objects/util/collections/abstractCollection.h | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Objects/objects/base/object.h b/Objects/objects/base/object.h index 3243a12..072d598 100644 --- a/Objects/objects/base/object.h +++ b/Objects/objects/base/object.h @@ -31,5 +31,4 @@ Object* new_Object(char* className); CAST_FN_PROTO(Object); bool instanceOf(void* o, char* className); - #endif // !OBJECT_H diff --git a/Objects/objects/util/collections/abstractCollection.c b/Objects/objects/util/collections/abstractCollection.c index 0ab047b..f7f59e9 100644 --- a/Objects/objects/util/collections/abstractCollection.c +++ b/Objects/objects/util/collections/abstractCollection.c @@ -1,5 +1,7 @@ #include "abstractCollection.h" +CAST_FN(AbstractCollection) + private _int getSize(void* c); private void* get(void* c, _int index); private void set(void* c, void* e, _int index); @@ -8,6 +10,7 @@ AbstractCollection* new_AbstractCollection(char* classNames, _int size) { CREATE_STRUCT_HEAP(AbstractCollection, a); a->o = new_Object("AbstractCollection"); + a->magicString = AbstractCollection_magicString; a->size = size; a->getSize = getSize; a->set = set; diff --git a/Objects/objects/util/collections/abstractCollection.h b/Objects/objects/util/collections/abstractCollection.h index 3960080..fab326f 100644 --- a/Objects/objects/util/collections/abstractCollection.h +++ b/Objects/objects/util/collections/abstractCollection.h @@ -5,10 +5,13 @@ #include "..\..\macro.h" #include "..\..\base\object.h" +static char* AbstractCollection_magicString = "AbstractCollection"; + typedef struct _AbstractCollection AbstractCollection, * pAbstractCollection; struct _AbstractCollection { Object* o; + char* magicString; _int size; _int (*getSize)(void*); void* (*get)(void*, _int); @@ -16,5 +19,6 @@ struct _AbstractCollection { }; AbstractCollection* new_AbstractCollection(char* classNames, _int size); +CAST_FN_PROTO(AbstractCollection); #endif // !ABSTRACTCOLLECTION_H From 9d384d1b8ece413c4646307fbf276b333d1c62db Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 20:36:46 +0100 Subject: [PATCH 4/6] karsten macros --- Objects/Objects.vcxproj | 1 + Objects/Objects.vcxproj.filters | 3 +++ Objects/objects/base/object.c | 2 +- Objects/objects/base/object.h | 18 +++++++++++++--- .../util/collections/abstractCollection.c | 21 ++++--------------- .../util/collections/abstractCollection.h | 4 ++-- Objects/objects/util/collections/array.h | 17 +++++++++++++++ 7 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 Objects/objects/util/collections/array.h diff --git a/Objects/Objects.vcxproj b/Objects/Objects.vcxproj index 4c5f24b..b19d189 100644 --- a/Objects/Objects.vcxproj +++ b/Objects/Objects.vcxproj @@ -136,6 +136,7 @@ + diff --git a/Objects/Objects.vcxproj.filters b/Objects/Objects.vcxproj.filters index 22ae034..02d61cc 100644 --- a/Objects/Objects.vcxproj.filters +++ b/Objects/Objects.vcxproj.filters @@ -53,5 +53,8 @@ Headerdateien + + Headerdateien + \ No newline at end of file diff --git a/Objects/objects/base/object.c b/Objects/objects/base/object.c index 0ea6b90..a91b276 100644 --- a/Objects/objects/base/object.c +++ b/Objects/objects/base/object.c @@ -10,7 +10,7 @@ private bool equals(void* o1, void* o2); Object* new_Object(char* className) { CREATE_STRUCT_HEAP(Object, o); - o->magicString = Object_magicString; + SET_MAGIC(o, Object); o->className = className; o->hashcode = hashcode; o->toString = toString; diff --git a/Objects/objects/base/object.h b/Objects/objects/base/object.h index 072d598..ca86776 100644 --- a/Objects/objects/base/object.h +++ b/Objects/objects/base/object.h @@ -4,22 +4,34 @@ #include "..\macro.h" +#define MAGIC char* 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* name = o; \ - if (strcmp(name->magicString, className##_magicString) == 0) return o; \ + if (strcmp(name->magicString, className##magicString) == 0) return o; \ else return cast##className((*(className**)o)); \ } #define CAST_FN_PROTO(className) \ className* cast##className(void* o) -static char* Object_magicString = "Object"; +#define CAST(className, ptr) \ + cast##className(ptr) + + + +MAGIC_STRING(Object); typedef struct _Object Object, * pObject; struct _Object { - char* magicString; + MAGIC; char* className; _int (*hashcode)(void*); char* (*toString)(void*); diff --git a/Objects/objects/util/collections/abstractCollection.c b/Objects/objects/util/collections/abstractCollection.c index f7f59e9..aeafc44 100644 --- a/Objects/objects/util/collections/abstractCollection.c +++ b/Objects/objects/util/collections/abstractCollection.c @@ -3,33 +3,20 @@ CAST_FN(AbstractCollection) private _int getSize(void* c); -private void* get(void* c, _int index); -private void set(void* c, void* e, _int index); AbstractCollection* new_AbstractCollection(char* classNames, _int size) { CREATE_STRUCT_HEAP(AbstractCollection, a); a->o = new_Object("AbstractCollection"); - a->magicString = AbstractCollection_magicString; + SET_MAGIC(a, AbstractCollection); a->size = size; a->getSize = getSize; - a->set = set; - a->get = get; + a->set = null; + a->get = null; return a; } private _int getSize(void* c) { - -} - -private void* get(void* c, _int index) -{ - + return CAST(AbstractCollection, c)->size; } - -private void set(void* c, void* e, _int index) -{ - -} - diff --git a/Objects/objects/util/collections/abstractCollection.h b/Objects/objects/util/collections/abstractCollection.h index fab326f..847eb2c 100644 --- a/Objects/objects/util/collections/abstractCollection.h +++ b/Objects/objects/util/collections/abstractCollection.h @@ -5,13 +5,13 @@ #include "..\..\macro.h" #include "..\..\base\object.h" -static char* AbstractCollection_magicString = "AbstractCollection"; +MAGIC_STRING(AbstractCollection); typedef struct _AbstractCollection AbstractCollection, * pAbstractCollection; struct _AbstractCollection { Object* o; - char* magicString; + MAGIC; _int size; _int (*getSize)(void*); void* (*get)(void*, _int); diff --git a/Objects/objects/util/collections/array.h b/Objects/objects/util/collections/array.h new file mode 100644 index 0000000..0cbcabb --- /dev/null +++ b/Objects/objects/util/collections/array.h @@ -0,0 +1,17 @@ +#ifndef ARRAY_H + +#define ARRAY_H + +#include "..\..\macro.h" +#include "..\collections\abstractCollection.h" + +typedef struct _Array Array, * pArray; + +struct _Array { + AbstractCollection* a; + char* magicString; + void** e; +}; + + +#endif // !ARRAY_H From 8f3f145940c8b71ae43af1b36f24c5ccfb887913 Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 21:03:00 +0100 Subject: [PATCH 5/6] array, abstractcollection done, no test --- Objects/Objects.vcxproj | 1 + Objects/Objects.vcxproj.filters | 3 ++ Objects/objects/base/object.c | 8 ++-- Objects/objects/base/object.h | 7 +-- .../util/collections/abstractCollection.c | 10 +++- .../util/collections/abstractCollection.h | 4 +- Objects/objects/util/collections/array.c | 46 +++++++++++++++++++ Objects/objects/util/collections/array.h | 6 ++- 8 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 Objects/objects/util/collections/array.c diff --git a/Objects/Objects.vcxproj b/Objects/Objects.vcxproj index b19d189..03965af 100644 --- a/Objects/Objects.vcxproj +++ b/Objects/Objects.vcxproj @@ -130,6 +130,7 @@ + diff --git a/Objects/Objects.vcxproj.filters b/Objects/Objects.vcxproj.filters index 02d61cc..7ade7c4 100644 --- a/Objects/Objects.vcxproj.filters +++ b/Objects/Objects.vcxproj.filters @@ -39,6 +39,9 @@ Quelldateien + + Quelldateien + diff --git a/Objects/objects/base/object.c b/Objects/objects/base/object.c index a91b276..e34cd6c 100644 --- a/Objects/objects/base/object.c +++ b/Objects/objects/base/object.c @@ -21,22 +21,22 @@ Object* new_Object(char* className) 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) diff --git a/Objects/objects/base/object.h b/Objects/objects/base/object.h index ca86776..0bb88be 100644 --- a/Objects/objects/base/object.h +++ b/Objects/objects/base/object.h @@ -4,7 +4,8 @@ #include "..\macro.h" -#define MAGIC char* magicString; +#define MAGIC char* MAGIX +#define MAGIX magicString #define SET_MAGIC(ptr, className) \ ptr->magicString = className##magicString @@ -13,8 +14,8 @@ #define CAST_FN(className) \ CAST_FN_PROTO(className) { \ - className* name = o; \ - if (strcmp(name->magicString, className##magicString) == 0) return o; \ + className* clazz = o; \ + if (strcmp(clazz->magicString, className##magicString) == 0) return o; \ else return cast##className((*(className**)o)); \ } diff --git a/Objects/objects/util/collections/abstractCollection.c b/Objects/objects/util/collections/abstractCollection.c index aeafc44..7241b72 100644 --- a/Objects/objects/util/collections/abstractCollection.c +++ b/Objects/objects/util/collections/abstractCollection.c @@ -4,11 +4,12 @@ CAST_FN(AbstractCollection) private _int getSize(void* c); -AbstractCollection* new_AbstractCollection(char* classNames, _int size) +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; @@ -20,3 +21,10 @@ 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 index 847eb2c..1f6ab49 100644 --- a/Objects/objects/util/collections/abstractCollection.h +++ b/Objects/objects/util/collections/abstractCollection.h @@ -12,13 +12,15 @@ 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* classNames, _int size); +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 new file mode 100644 index 0000000..d66ee86 --- /dev/null +++ b/Objects/objects/util/collections/array.c @@ -0,0 +1,46 @@ +#include "array.h" + +CAST_FN(Array) + +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) +{ + CREATE_STRUCT_HEAP(Array, a); + 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++) { + a->e[i] = null; + } + a->a->o->className = "Array"; + a->a->get = get; + a->a->set = set; + return a; +} + +private void set(void* a, void* e, _int index) +{ + check_index(a, index); + check_type(a, e); + Array* array = a; + array->e[index] = e; +} + +private void* get(void* a, _int index) +{ + check_index(a, index); + Array* array = a; + return array->e[index]; +} + +private void check_index(void* a, _int index) +{ + 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 0cbcabb..640be4d 100644 --- a/Objects/objects/util/collections/array.h +++ b/Objects/objects/util/collections/array.h @@ -5,13 +5,17 @@ #include "..\..\macro.h" #include "..\collections\abstractCollection.h" +MAGIC_STRING(Array); + typedef struct _Array Array, * pArray; struct _Array { AbstractCollection* a; - char* magicString; + MAGIC; void** e; }; +Array* new_Array(char* type, _int size); +CAST_FN_PROTO(Array); #endif // !ARRAY_H From 68fb294b447d1303a97566a14add14322e9f7178 Mon Sep 17 00:00:00 2001 From: Stefan Paul Date: Wed, 28 Feb 2024 21:11:52 +0100 Subject: [PATCH 6/6] tested casting up and down? --- Objects/main.c | 8 ++++++++ Objects/objects/base/object.h | 2 -- Objects/objects/macro.h | 7 ------- Objects/objects/stdlib.h | 2 ++ 4 files changed, 10 insertions(+), 9 deletions(-) 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/base/object.h b/Objects/objects/base/object.h index 0bb88be..d4aee9c 100644 --- a/Objects/objects/base/object.h +++ b/Objects/objects/base/object.h @@ -25,8 +25,6 @@ #define CAST(className, ptr) \ cast##className(ptr) - - MAGIC_STRING(Object); typedef struct _Object Object, * pObject; 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 b64e40e..3fc1bc9 100644 --- a/Objects/objects/stdlib.h +++ b/Objects/objects/stdlib.h @@ -3,6 +3,8 @@ #define STDLIB_H #include "..\objects\macro.h" +#include "base/object.h" +#include "util/collections/array.h" #endif // !STDLIB_H