]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
oo: destructors
authorTimePath <andrew.hardaker1995@gmail.com>
Thu, 17 Mar 2016 00:59:19 +0000 (11:59 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Thu, 17 Mar 2016 01:17:09 +0000 (12:17 +1100)
qcsrc/lib/oo.qh

index 49147765e4f1d28ca943f975a359090d5a9d5981..0638c932b3e13fb11868a78568ebeba598b8188c 100644 (file)
@@ -77,6 +77,13 @@ entity __spawn(string _classname, string _sourceLoc, bool pure)
 #define new_pure(class) _new(class, true)
 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
 
+#define delete(this) MACRO_BEGIN { \
+    entity _this = (this); \
+    void(entity) _dtor = _this.dtor; \
+    if (_dtor) _dtor(_this); else remove(_this); \
+    /* this = NULL; */  \
+} MACRO_END
+
 entity _clearentity_ent;
 STATIC_INIT(clearentity)
 {
@@ -187,6 +194,16 @@ STATIC_INIT(RegisterClasses)
        } \
        STATIC_METHOD(cname, name, prototype)
 
+#define DESTRUCTOR(cname) \
+       STATIC_METHOD(cname, dtorimpl, void(cname this)); \
+    METHOD(cname, dtor, void(cname this)) \
+    { \
+        METHOD_REFERENCE(cname, dtorimpl)(this); \
+        entity super = SUPER(cname); \
+        if (super != cname##_vtbl) super.dtor(this); \
+    } \
+       STATIC_METHOD(cname, dtorimpl, void(cname this))
+
 #define ATTRIB(cname, name, type, val)      \
        class(cname).type name;                \
        INIT(cname) \
@@ -219,7 +236,9 @@ STATIC_INIT(RegisterClasses)
 #define spawn_1(this)
 #define _vtbl NULL
 CLASS(Object, );
-       METHOD(Object, describe, string(entity this))
+    DESTRUCTOR(Object) { remove(this); }
+    #define remove(this) delete(this)
+       METHOD(Object, describe, string(Object this))
        {
                string s = _("No description");
                if (cvar("developer"))
@@ -232,7 +251,7 @@ CLASS(Object, );
                }
                return s;
        }
-       METHOD(Object, display, void(entity this, void(string name, string icon) returns))
+       METHOD(Object, display, void(Object this, void(string name, string icon) returns))
        {
                returns(sprintf("entity %i", this), "nopreview_map");
        }