-
-
Save Frityet/66eacfe8af4457c8a6608832164d5e06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "C+.h" | |
class(BaseClass, { | |
int field; | |
string s; | |
method(void, setstr)(string val); | |
}); | |
constructor(BaseClass) | |
{ | |
var this = init(BaseClass); | |
this->field = 5; | |
this->s = mstr("Hello!"); | |
this->defmethod(setstr)((string str) { | |
mc_release(this->s); | |
this->s = mc_reference(str); | |
}); | |
return this; | |
} | |
deconstructor(BaseClass) | |
{ | |
mc_release(this->s); | |
free_method(this->setstr); | |
} | |
class(TestClass, { | |
inherit(BaseClass); | |
int *array; | |
int count; | |
method(int, add_item)(int i); | |
}); | |
constructor(TestClass, int count) | |
{ | |
var this = init(TestClass); | |
this->super_init(BaseClass); | |
this->array = array(int, count); | |
this->count = 0; | |
this->defmethod(add_item)((int i) { | |
this->array[this->count++] = i; | |
return this->count; | |
}); | |
return this; | |
} | |
deconstructor(TestClass) | |
{ | |
this->super_deinit(BaseClass); | |
mc_release(this->array); | |
} | |
int main() | |
{ | |
auto var tc = new(TestClass)(5); | |
tc->add_item(10); | |
tc->add_item(100); | |
auto var s = mstr("Hello!"); | |
tc->setstr(s); | |
printf("%s: %d\n", tc->s, tc->array[tc->count - 1]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <Block.h> | |
#include "mstring.h" | |
#define lambda ^ | |
#define make_method(...) Block_copy(lambda __VA_ARGS__) | |
#define defmethod(name, ...) name = make_method | |
#define free_method Block_release | |
#define method(ret, name, ...) ret (^ name) | |
#define var __auto_type | |
#define let const var | |
#define overloadable __attribute__((overloadable)) | |
#define used __attribute__((used)) | |
#define super(type) $super_##type##$ | |
#define inherit(type) union { struct type; struct type super(type); } | |
#define new(type, ...) (struct type *)type | |
#define class(name, ...) struct name __VA_ARGS__ ; struct name *name(); void name##_free(struct name *) | |
#define constructor(name, ...) struct name *name(__VA_ARGS__) | |
#define deconstructor(name) void name##_free(struct name *this) | |
used overloadable static inline void *alloc(size_t size, size_t count, mc_FreePointer_f *free) | |
{ return mc_alloc_managed(size, count, free); } | |
used overloadable static inline void *alloc(size_t size, size_t count) | |
{ return mc_alloc_managed(size, count, NULL); } | |
used overloadable static inline void *alloc(size_t size) | |
{ return mc_alloc_managed(size, 1, NULL); } | |
#define array(type, count) (type *)alloc(sizeof(type), count, (mc_FreePointer_f *)type##_free) | |
#define init(type) (struct type *)alloc(sizeof(struct type), 1, (mc_FreePointer_f *)type##_free) | |
#define super_init(stype, ...) $super_##stype##$; ({ auto var so = new(stype)(__VA_ARGS__); memcpy(this, so, sizeof(stype)); }) | |
#define super_deinit(type) $super_##type##$; type##_free(&this->$super_##type##$) | |
#define PRIMITVE_DTOR(type) used static inline void type##_free(type *this){ (void)this; } | |
PRIMITVE_DTOR(char); | |
PRIMITVE_DTOR(short); | |
PRIMITVE_DTOR(int); | |
PRIMITVE_DTOR(long); | |
PRIMITVE_DTOR(float); | |
PRIMITVE_DTOR(double); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment