summaryrefslogtreecommitdiff
path: root/src/cobj.h
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2025-05-22 22:59:39 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2025-05-22 23:01:28 +0600
commit29b8e5efb43bdb71737f1b417fedb3b1558bb9af (patch)
tree79eaca3ba73974af3c4d6989e3ebbb006b2404c5 /src/cobj.h
parentcefb440148fd80cac7537b3743dd0e018df3a797 (diff)
downloadc-obp-example-29b8e5efb43bdb71737f1b417fedb3b1558bb9af.tar.gz
c-obp-example-29b8e5efb43bdb71737f1b417fedb3b1558bb9af.zip
Clean up and fix warnings
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src/cobj.h')
-rw-r--r--src/cobj.h121
1 files changed, 75 insertions, 46 deletions
diff --git a/src/cobj.h b/src/cobj.h
index c12181b..1e5c75e 100644
--- a/src/cobj.h
+++ b/src/cobj.h
@@ -1,56 +1,43 @@
-#ifndef __COBJ_IMPLS_H__
-# define __COBJ_IMPLS_H__
+#if defined(__COBJ_PUB__) && defined(__COBJ_DECL__) || defined(__COBJ_IMPL__) && defined(__COBJ_H__)
+# undef Atom
+# undef Attr
+# undef Call
+# undef Class
+# undef DefaultDelete
+# undef Define
+# undef Del
+# undef DelF
+# undef Func
+# undef GET
+# undef Get
+# undef List
+# undef New
+# undef Prop
+# undef Self
+# undef Set
+# undef TYPE
+# undef UNUSED
+# undef __COBJ_DECL__
+# undef __COBJ_PUB__
+#endif
+
+
+#if ! defined(__COBJ_H__) && ! defined(__COBJ_IMPL__) && ! defined(__COBJ_DECL__)
# define Get(instance, field, ...) \
(instance)->get_##field(instance, ## __VA_ARGS__)
# define Set(instance, field, ...) \
(instance)->set_##field(instance, ## __VA_ARGS__)
# define Call(instance, fun, ...) \
(instance)->fun(instance, ## __VA_ARGS__)
-# define New(klass, ...) klass##_new(__VA_ARGS__)
-# define Del(klass, ...) klass##_del(__VA_ARGS__)
-#endif /* __COBJ_IMPLS_H__ */
-
-#if defined(__USE_C_OBJSYS__)
-# undef New
-# undef Del
-# undef __USE_C_OBJSYS__
-#elif defined(__COBJ_PRIV_IMPLS_H__)
-# define UNUSED(arg) while(0 && (arg))
-# define TYPE(klass) \
- struct __ ## klass ## _impl
-# define GETTER(klass, field, type, body, ...) \
- type klass##_get_##field(klass self, ## __VA_ARGS__) \
- body
-# define SETTER(klass, field, type, body, ...) \
- bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) \
- body
-# define DEFINE(klass, name, type, body, ...) \
- type klass##_##name(klass self, ## __VA_ARGS__) \
- body
-# define NEW(klass, body, ...) \
- klass klass ## _new(__VA_ARGS__) \
- body
-# define DEL(klass, body, ...) \
- void klass ## _del(klass self, ## __VA_ARGS__) \
- body
-
-# define Property(klass, name) \
- (self->get_ ## name) = (& klass ## _ ## get_ ## name), \
- (self->set_ ## name) = (& klass ## _ ## set_ ## name)
-
-# define Callable(klass, name) \
- (self->name) = (& klass ## _ ## name)
-
-# define SELF(name) \
- self->_property_ ## name
-# define GET(obj, name) \
- (obj)->_property_ ## name
-
-#else
+# define New(klass, ...) \
+ klass##_new(__VA_ARGS__)
+# define Del(instance) \
+ (instance)->destroy((instance)), instance = NULL
+#elif defined(__COBJ_DECL__)
# define Class(klass, data, ...) \
typedef struct __##klass##_impl * klass; \
klass klass ## _new(__VA_ARGS__); \
- void klass ## _delete(); \
+ void klass ## _delete(klass); \
struct __##klass##_impl data
# define List(klass, field, type, dim, ...) \
@@ -65,7 +52,49 @@
# define Func(klass, name, type, ...) \
type (*name)(klass, ## __VA_ARGS__)
-# define Del (klass) \
+
+# define Del(klass) \
void (*destroy)(klass)
-# define __USE_C_OBJSYS__
+#elif defined(__COBJ_IMPL__)
+# define UNUSED(arg) while(0 && (arg))
+# define TYPE(klass) \
+ struct __ ## klass ## _impl
+# define Get(klass, field, type, body, ...) \
+ type klass##_get_##field(klass self, ## __VA_ARGS__) \
+ body
+# define Set(klass, field, type, body, ...) \
+ bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) \
+ body
+# define Def(klass, name, type, body, ...) \
+ type klass##_##name(klass self, ## __VA_ARGS__) \
+ body
+# define New(klass, body, ...) \
+ klass klass ## _new(__VA_ARGS__) { \
+ klass self = (klass)malloc(sizeof(TYPE(klass))); \
+ if(self != NULL) body; \
+ return self; \
+ }
+
+# define Del(klass, body, ...) \
+ void klass ## _delete(klass self, ## __VA_ARGS__) \
+ body
+
+# define Prop(klass, name) \
+ (self->get_ ## name) = (& klass ## _ ## get_ ## name), \
+ (self->set_ ## name) = (& klass ## _ ## set_ ## name)
+
+# define Func(klass, name) \
+ (self->name) = (& klass ## _ ## name)
+# define DelF(klass) \
+ (self->destroy) = (&klass ## _delete)
+
+# define Self(name) \
+ self->_property_ ## name
+# define Attr(instance, name) \
+ (instance)->_property_ ## name
+# define Call(instance, fun, ...) \
+ (instance)->fun(instance, ## __VA_ARGS__)
+# define DefaultDelete { \
+ if(self != NULL) free(self); \
+ }
#endif