]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/oo.qh
Merge branch 'master' into TimePath/scrollpanel
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
index 45fbb2627dd1f6d947c0aa0b61c2c1ad83ee9715..f57bf8e909c4e7d1a594a0e36f2415b715483fda 100644 (file)
@@ -4,12 +4,6 @@
 #include "nil.qh"
 #include "static.qh"
 
-#ifdef MENUQC
-       #define NULL (0, null_entity)
-#else
-       #define NULL (0, world)
-#endif
-
 .vector origin;
 .bool pure_data;
 /** @deprecated use new_pure or NEW(class) */
@@ -49,10 +43,10 @@ entity __spawn(string _classname, string _sourceLoc, bool pure)
        if (pure) {
                make_pure(this);
                #ifdef CSQC
-               setorigin(this, '0 0 10000');
+               setorigin(this, (world.mins + world.maxs) * 0.5);
                #endif
                #ifdef SVQC
-        setorigin(this, '0 0 -10000');
+        setorigin(this, (world.mins + world.maxs) * 0.5);
         #endif
        }
        return this;
@@ -77,10 +71,17 @@ entity __spawn(string _classname, string _sourceLoc, bool pure)
 #define new_pure(class) _new(class, true)
 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
 
+[[accumulate]] void ONREMOVE(entity this) {}
+
+#ifndef SVQC
+       #define delete_fn builtin_remove
+#endif
+
 #define delete(this) MACRO_BEGIN { \
     entity _this = (this); \
     void(entity) _dtor = _this.dtor; \
-    if (_dtor) _dtor(_this); else remove(_this); \
+    ONREMOVE(this); \
+    if (_dtor) _dtor(_this); else delete_fn(_this); \
     /* this = NULL; */  \
 } MACRO_END
 
@@ -105,20 +106,38 @@ void clearentity(entity e)
 // Classes have a `spawn##cname(entity)` constructor
 // The parameter is used across [[accumulate]] functions
 
+.bool transmute;
+
 // Macros to hide this implementation detail:
 #ifdef __STDC__
        #define NEW(cname, ...) \
                OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
+
+    #define _TRANSMUTE(cname, this, ...) \
+        OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
+
        #define CONSTRUCT(cname, ...) \
                OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
 #else
        #define NEW(cname, ...) \
                OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
 
+    #define _TRANSMUTE(cname, this, ...) \
+        OVERLOAD(spawn##cname, this,##__VA_ARGS__)
+
        #define CONSTRUCT(cname, ...) \
                OVERLOAD(spawn##cname, this,##__VA_ARGS__)
 #endif
 
+#define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
+    entity _e = (this); \
+    if (_e.vtblbase != cname##_vtbl) { \
+        _e.transmute = true; \
+        _e.classname = #cname; \
+        _TRANSMUTE(cname, _e, __VA_ARGS__); \
+    } \
+    MACRO_END
+
 #define CONSTRUCTOR(cname, ...) \
        cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
        { \
@@ -152,27 +171,37 @@ STATIC_INIT(RegisterClasses)
 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
 
+#if NDEBUG
+       #define DEBUG_STUFF(cname)
+#else
+       #define DEBUG_STUFF(cname) \
+               bool is_##cname(entity e) { return e.instanceOf##cname; } \
+               void isnt_##cname(entity e) { eprint(e); }
+#endif
+
+
 #define CLASS(cname, base)                  \
        entityclass(cname, base);               \
-       class(cname).bool instanceOf##cname;   \
-    bool is_##cname(entity e) { return e.instanceOf##cname; } \
+       class(cname).bool instanceOf##cname;    \
+       DEBUG_STUFF(cname)                      \
        VTBL(cname, base)                       \
-       _INIT_STATIC(cname) \
-       {                    \
-               if (cname##_vtbl\
-               {                 \
+       _INIT_STATIC(cname)                     \
+       {                                       \
+               if (cname##_vtbl && !this.transmute)\
+               {                                   \
                        copyentity(cname##_vtbl, this); \
                        return;                         \
                }                                   \
                spawn##base##_static(this);         \
                this.instanceOf##cname = true;      \
        }                                       \
-       INIT(cname) \
-       {                           \
+       INIT(cname)                             \
+       {                                       \
                /* Only statically initialize the current class, it contains everything it inherits */ \
                if (cname##_vtbl.vtblname == this.classname) \
-               { \
+               {                                   \
                        spawn##cname##_static(this);    \
+                       this.transmute = false;         \
                        this.classname = #cname;        \
                        this.vtblname = string_null;    \
                        this.vtblbase = cname##_vtbl;   \
@@ -200,18 +229,23 @@ STATIC_INIT(RegisterClasses)
     METHOD(cname, dtor, void(cname this)) \
     { \
         METHOD_REFERENCE(cname, dtorimpl)(this); \
+        this.instanceOf##cname = false; \
         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;                \
+#define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
+#define EVAL_ATTRIB(...) __VA_ARGS__
+#define ATTRIB_3(cname, name, type) INIT(cname) {} class(cname) .type name
+#define ATTRIB_4(cname, name, type, val) \
+       ATTRIB_3(cname, name, type); \
        INIT(cname) \
        { \
                noref bool strzone; /* Error on strzone() calls. */ \
                this.name = val; \
-       }
+       } \
+       ATTRIB_3(cname, name, type)
 
 #define STATIC_ATTRIB(cname, name, type, val) \
        type cname##_##name; \
@@ -242,7 +276,7 @@ STATIC_INIT(RegisterClasses)
        }
 
 #define ATTRIBARRAY(cname, name, type, cnt) \
-       class(cname).type name[cnt];
+       class(cname) .type name[cnt]
 
 #define ENDCLASS(cname) \
        INIT(cname) \
@@ -256,7 +290,7 @@ STATIC_INIT(RegisterClasses)
 #define spawn_1(this)
 #define _vtbl NULL
 CLASS(Object, );
-    DESTRUCTOR(Object) { remove(this); }
+    DESTRUCTOR(Object) { builtin_remove(this); }
     #define remove(this) delete(this)
        METHOD(Object, describe, string(Object this))
        {