From 8a0260d36f9fc887a5a1c235a11b855a4373278d Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 11 Mar 2017 02:00:02 +0100 Subject: [PATCH] new style convertion (java style objects) --- memory/memory.c | 16 +++++++-------- oop/example.c | 40 ++++++++++++++++++------------------- oop/oop.c | 20 +++++++++---------- oop/oop.h | 15 +++++++------- strbuilder/example.c | 2 +- strbuilder/strbuilder.c | 38 +++++++++++++++++------------------ strbuilder/strbuilder.h | 24 +++++++++++----------- try/example.c | 8 ++++---- try/exceptions/exceptions.c | 16 +++++++-------- try/exceptions/exceptions.h | 28 +++++++------------------- try/try.c | 2 +- 11 files changed, 98 insertions(+), 111 deletions(-) diff --git a/memory/memory.c b/memory/memory.c index 0e3107e..8b98128 100644 --- a/memory/memory.c +++ b/memory/memory.c @@ -5,35 +5,35 @@ #include #include -void allocate_sub(size_t size, void** ptr) { throws(exception_t); +void allocate_sub(size_t size, void** ptr) { throws(Exception_t); *ptr = malloc(size); if (*ptr == NULL) { - throw(new exception(strerror(errno))); + throw(new Exception(strerror(errno))); } } -void* allocate(size_t size) { throws(exception_t); +void* allocate(size_t size) { throws(Exception_t); void* ptr; s_(allocate_sub(size, &ptr)); return ptr; } -void reallocate_sub(size_t size, void** ptr, void* old) { throws(exception_t); +void reallocate_sub(size_t size, void** ptr, void* old) { throws(Exception_t); *ptr = realloc(old, size); if (*ptr == NULL) { - throw(new exception(strerror(errno))); + throw(new Exception(strerror(errno))); } } -void* reallocate(void* old, size_t size) { throws(exception_t); +void* reallocate(void* old, size_t size) { throws(Exception_t); void* ptr; s_(reallocate_sub(size, &ptr, old)); return ptr; } -void* clone(const void* org, size_t size) { throws(exception_t); +void* clone(const void* org, size_t size) { throws(Exception_t); void* ptr; - trycall allocate_sub(size, &ptr); + s_(allocate_sub(size, &ptr)); memcpy(ptr, org, size); return ptr; } diff --git a/oop/example.c b/oop/example.c index 9f12803..10eaba5 100644 --- a/oop/example.c +++ b/oop/example.c @@ -3,34 +3,34 @@ #include "oop.h" -#define test construct(test) +#define Test construct(Test) -class(test, object_class, true) { - extends(object_t); +class(Test, Object_class, true) { + extends(Object_t); const char* string; - void (*print)(defclass test*); - void (*destruct)(defclass test*); -} test_t; + void (*print)(defclass Test*); + void (*destruct)(defclass Test*); +} Test_t; -void method(test, print)(test_t* this) { +void method(Test, print)(Test_t* this) { printf("%s\n", this->string); } -void method(test, destruct)(test_t* this) { - this->super.destruct((object_t*) this); +void method(Test, destruct)(Test_t* this) { + this->super.destruct((Object_t*) this); } -void method(test, populate)(test_t* obj, class_t c) { - populate(object)((object_t*) obj); +void method(Test, populate)(Test_t* obj, class_t c) { + populate(Object)((Object_t*) obj, c); - add_method(obj, test, destruct); - add_method(obj, test, print); + add_method(obj, Test, destruct); + add_method(obj, Test, print); } -test_t* method(test, construct)(const char* string) { - test_t* obj = malloc(sizeof(test_t)); - populate(test)(obj, test_class); +Test_t* method(Test, construct)(const char* string) { + Test_t* obj = malloc(sizeof(Test_t)); + populate(Test)(obj, Test_class); obj->string = string; @@ -38,13 +38,13 @@ test_t* method(test, construct)(const char* string) { } int main(void) { - test_t* obj = new test("Hallo Welt"); + Test_t* obj = new Test("Hallo Welt"); - class_t c = oop_get_class_from_obj((object_t*) obj); + class_t c = oop_get_class_from_obj((Object_t*) obj); printf("class: %s\n", oop_get_class_name(c)); printf("superclass: %s\n", oop_get_class_name(oop_get_super_class(c))); - printf("instanceof test: %i\n", instanceof(obj, test_class)); - printf("instanceof object: %i\n", instanceof(obj, object_class)); + printf("instanceof test: %i\n", instanceof(obj, Test_class)); + printf("instanceof object: %i\n", instanceof(obj, Object_class)); obj->print(obj); obj->destruct(obj); diff --git a/oop/oop.c b/oop/oop.c index 494b527..2d99b2f 100644 --- a/oop/oop.c +++ b/oop/oop.c @@ -21,7 +21,7 @@ class_id_t oop_add_class(const char* name, class_t super, bool instanceable) { } bool oop_instance_of_id(void* object, class_id_t id) { - class_id_t cid = ((object_t*) object)->meta_obj.type.id; + class_id_t cid = ((Object_t*) object)->meta_obj.type.id; meta_class_t c = classes[cid]; if (cid == id) return true; @@ -65,25 +65,25 @@ class_t oop_get_super_class(class_t type) { return oop_class_from_id(classes[type.id].super); } -class_t oop_get_class_from_obj(object_t* obj) { - return oop_class_from_id(((object_t*) obj)->meta_obj.type.id); +class_t oop_get_class_from_obj(Object_t* obj) { + return oop_class_from_id(((Object_t*) obj)->meta_obj.type.id); } // defined by class macro in h-file -class_t object_class; +class_t Object_class; -void method(object, destruct)(object_t* obj) { +void method(Object, destruct)(Object_t* obj) { free(obj); } -object_t* method(object, construct)() { - object_t* obj = malloc(sizeof(object_t)); - populate(object)(obj, object_class); +Object_t* method(Object, construct)() { + Object_t* obj = malloc(sizeof(Object_t)); + populate(Object)(obj, Object_class); return obj; } -void method(object, populate)(object_t* obj, class_t type) { +void method(Object, populate)(Object_t* obj, class_t type) { obj->meta_obj.type = type; - add_method(obj, object, destruct); + add_method(obj, Object, destruct); } diff --git a/oop/oop.h b/oop/oop.h index 3f9dcba..bffadcf 100644 --- a/oop/oop.h +++ b/oop/oop.h @@ -9,6 +9,7 @@ #define class(name, superclass, instanceable) class_t name##_class; __attribute__ ((constructor)) static void add_##name##_class(void) { name##_class.id = oop_add_class(#name, superclass, instanceable);} typedef struct name #define method(class, method) class ##_method_## method #define add_method(object, class, method) object->method = class ##_method_## method +#define implements(i) i##_interface #define extends(type) type super @@ -43,12 +44,12 @@ typedef struct meta_object { class_id_t oop_add_class(const char*, class_t, bool); -#define object construct(object) +#define Object construct(Object) -extern class(object, NO_SUPER_CLASS, true) { +extern class(Object, NO_SUPER_CLASS, true) { meta_object_t meta_obj; - void (*destruct)(defclass object*); -} object_t; + void (*destruct)(defclass Object*); +} Object_t; bool oop_instance_of_id(void*, class_id_t); @@ -58,9 +59,9 @@ class_id_t oop_id_from_name(const char*); bool oop_class_exists(const char*); class_t oop_class_from_id(class_id_t); class_t oop_get_super_class(class_t); -class_t oop_get_class_from_obj(object_t*); +class_t oop_get_class_from_obj(Object_t*); -object_t* method(object, construct)(void); -void method(object, populate)(object_t* obj, class_t); +Object_t* method(Object, construct)(void); +void method(Object, populate)(Object_t* obj, class_t); #endif diff --git a/strbuilder/example.c b/strbuilder/example.c index ea68544..f588617 100644 --- a/strbuilder/example.c +++ b/strbuilder/example.c @@ -5,7 +5,7 @@ #include "strbuilder.h" int main(void) { - strbuilder_t* builder = new strbuilder("Hallo"); + Strbuilder_t* builder = new Strbuilder("Hallo"); builder->add(builder, " Welt"); builder->add(builder, "!"); diff --git a/strbuilder/strbuilder.c b/strbuilder/strbuilder.c index 4b42e0e..c5b37cb 100644 --- a/strbuilder/strbuilder.c +++ b/strbuilder/strbuilder.c @@ -3,9 +3,9 @@ #include #include -class_t strbuilder_class; +class_t Strbuilder_class; -void method(strbuilder, clear)(strbuilder_t* this) { +void method(Strbuilder, clear)(Strbuilder_t* this) { for (int i = 0; i < this->nrstrings; i++) { free(this->strings[i]); } @@ -14,18 +14,18 @@ void method(strbuilder, clear)(strbuilder_t* this) { this->nrstrings = 0; } -void method(strbuilder, destruct)(strbuilder_t* this) { +void method(Strbuilder, destruct)(Strbuilder_t* this) { this->clear(this); - this->super.destruct((object_t*) this); + this->super.destruct((Object_t*) this); } -void method(strbuilder, add)(strbuilder_t* this, const char* string) { +void method(Strbuilder, add)(Strbuilder_t* this, const char* string) { this->strings = realloc(this->strings, ++this->nrstrings * sizeof(char*)); this->strings[this->nrstrings - 1] = malloc(strlen(string) + 1); strcpy(this->strings[this->nrstrings - 1], string); } -size_t method(strbuilder, length)(strbuilder_t* this) { +size_t method(Strbuilder, length)(Strbuilder_t* this) { size_t length = 0; if (this->string != NULL) length = strlen(this->string); @@ -35,7 +35,7 @@ size_t method(strbuilder, length)(strbuilder_t* this) { return length; } -void method(strbuilder, build)(strbuilder_t* this) { +void method(Strbuilder, build)(Strbuilder_t* this) { size_t length = this->length(this); bool empty = this->string == NULL; this->string = realloc(this->string, length + 1); @@ -48,14 +48,14 @@ void method(strbuilder, build)(strbuilder_t* this) { this->nrstrings = 0; } -const char* method(strbuilder, get)(strbuilder_t* this) { +const char* method(Strbuilder, get)(Strbuilder_t* this) { return this->string; } -strbuilder_t* method(strbuilder, construct)(const char* string) { - strbuilder_t* obj = malloc(sizeof(strbuilder_t)); +Strbuilder_t* method(Strbuilder, construct)(const char* string) { + Strbuilder_t* obj = malloc(sizeof(Strbuilder_t)); - populate(strbuilder)(obj, strbuilder_class); + populate(Strbuilder)(obj, Strbuilder_class); obj->string = malloc(strlen(string) + 1); strcpy(obj->string, string); @@ -64,17 +64,17 @@ strbuilder_t* method(strbuilder, construct)(const char* string) { } -void method(strbuilder, populate)(strbuilder_t* obj, class_t c) { - populate(object)((object_t*) obj, c); +void method(Strbuilder, populate)(Strbuilder_t* obj, class_t c) { + populate(Object)((Object_t*) obj, c); obj->string = NULL; obj->strings = NULL; obj->nrstrings = 0; - add_method(obj, strbuilder, destruct); - add_method(obj, strbuilder, add); - add_method(obj, strbuilder, build); - add_method(obj, strbuilder, get); - add_method(obj, strbuilder, clear); - add_method(obj, strbuilder, length); + add_method(obj, Strbuilder, destruct); + add_method(obj, Strbuilder, add); + add_method(obj, Strbuilder, build); + add_method(obj, Strbuilder, get); + add_method(obj, Strbuilder, clear); + add_method(obj, Strbuilder, length); } diff --git a/strbuilder/strbuilder.h b/strbuilder/strbuilder.h index b18764e..568adbd 100644 --- a/strbuilder/strbuilder.h +++ b/strbuilder/strbuilder.h @@ -4,23 +4,23 @@ #include #include "../oop/oop.h" -#define strbuilder construct(strbuilder) -extern class(strbuilder, object_class, true) { - extends(object_t); +#define Strbuilder construct(Strbuilder) +extern class(Strbuilder, Object_class, true) { + extends(Object_t); char* string; char** strings; int nrstrings; - void (*destruct)(defclass strbuilder*); - void (*add)(defclass strbuilder*, const char*); - void (*build)(defclass strbuilder*); - const char* (*get)(defclass strbuilder*); - void (*clear)(defclass strbuilder*); - size_t (*length)(defclass strbuilder*); -} strbuilder_t; + void (*destruct)(defclass Strbuilder*); + void (*add)(defclass Strbuilder*, const char*); + void (*build)(defclass Strbuilder*); + const char* (*get)(defclass Strbuilder*); + void (*clear)(defclass Strbuilder*); + size_t (*length)(defclass Strbuilder*); +} Strbuilder_t; -strbuilder_t* method(strbuilder, construct)(const char*); -void method(strbuilder, populate)(strbuilder_t*); +Strbuilder_t* method(Strbuilder, construct)(const char*); +void method(Strbuilder, populate)(Strbuilder_t*, class_t); #endif diff --git a/try/example.c b/try/example.c index 39794cc..fa7b157 100644 --- a/try/example.c +++ b/try/example.c @@ -5,15 +5,15 @@ #include -void function(void) { throws(exception_class) +void function(void) { throws(Exception_class) - throw(new exception("This is an uncaught exception.")); + throw(new Exception("This is an uncaught exception.")); } int main(void) { try { - throw(new exception("This is a caught exception.")); - } catch(exception_class, exception_t* e) { + throw(new Exception("This is a caught exception.")); + } catch(Exception_class, Exception_t* e) { printf("Caught: %s\n", e->msg); e->destruct(e); } endtry; diff --git a/try/exceptions/exceptions.c b/try/exceptions/exceptions.c index 8c8ad2b..5211935 100644 --- a/try/exceptions/exceptions.c +++ b/try/exceptions/exceptions.c @@ -2,19 +2,19 @@ #include -void method(exception, destruct)(exception_t* this) { - this->super.destruct((object_t*) this); +void method(Exception, destruct)(Exception_t* this) { + this->super.destruct((Object_t*) this); } -void method(exception, populate)(exception_t* obj, class_t c) { - populate(object)((object_t*) obj, c); +void method(Exception, populate)(Exception_t* obj, class_t c) { + populate(Object)((Object_t*) obj, c); - add_method(obj, exception, destruct); + add_method(obj, Exception, destruct); } -exception_t* method(exception, construct)(const char* msg) { - exception_t* obj = malloc(sizeof(exception_t)); - populate(exception)(obj, exception_class); +Exception_t* method(Exception, construct)(const char* msg) { + Exception_t* obj = malloc(sizeof(Exception_t)); + populate(Exception)(obj, Exception_class); obj->msg = msg; diff --git a/try/exceptions/exceptions.h b/try/exceptions/exceptions.h index 9a9e701..dead2da 100644 --- a/try/exceptions/exceptions.h +++ b/try/exceptions/exceptions.h @@ -3,30 +3,16 @@ #include "../../oop/oop.h" -#define exception construct(exception) -class(exception, object_class, true) { - extends(object_t); +#define Exception construct(Exception) +class(Exception, Object_class, true) { + extends(Object_t); const char* msg; - void (*destruct)(defclass exception*); -} exception_t; + void (*destruct)(defclass Exception*); +} Exception_t; -void method(exception, populate)(exception_t*, class_t); -exception_t* method(exception, construct)(const char*); - -/*#define exception construct(exception) -class_t exception_class; -class(exception, exception_class, object_class, true) { - extends(object_t); - - const char* msg; - - void (*destruct)(defclass exception*); -} exception_t; - -void method(exception, populate)(exception_t*); -exception_t* method(exception, construct)(const char*); -*/ +void method(Exception, populate)(Exception_t*, class_t); +Exception_t* method(Exception, construct)(const char*); #endif diff --git a/try/try.c b/try/try.c index 2ec741c..0638005 100644 --- a/try/try.c +++ b/try/try.c @@ -94,7 +94,7 @@ void print_backtrace(FILE* file) { void try_throw(try_t id, void* exception) { if (id == NO_TRY_BODY) { - exception_t* e = exception; + Exception_t* e = exception; #ifdef PTHREAD_SCOPE_SYSTEM // we are in a pthread environment