]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/oo.qh
OO: transmute
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
index 5e0efc218e2f565365984467cd03b309f582b1e0..d0c770ae3160a7f6f32612182929a6452493398e 100644 (file)
@@ -105,16 +105,25 @@ 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.transmute = true, this.classname = #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.transmute = true, this.classname = #cname, this),##__VA_ARGS__)
+
        #define CONSTRUCT(cname, ...) \
                OVERLOAD(spawn##cname, this,##__VA_ARGS__)
 #endif
@@ -136,7 +145,7 @@ STATIC_INIT(RegisterClasses)
 }
 
 #define VTBL(cname, base) \
-       INIT_STATIC(cname); \
+       _INIT_STATIC(cname); \
        entity cname##_vtbl; \
        void cname##_vtbl_init() \
        { \
@@ -149,30 +158,31 @@ STATIC_INIT(RegisterClasses)
        } \
        ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
 
-#define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
+#define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
 
 #define CLASS(cname, base)                  \
        entityclass(cname, base);               \
-       class(cname).bool instanceOf##cname;   \
+       class(cname).bool instanceOf##cname;    \
     bool is_##cname(entity e) { return e.instanceOf##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;   \
@@ -189,7 +199,7 @@ STATIC_INIT(RegisterClasses)
 #define METHOD(cname, name, prototype) \
        STATIC_METHOD(cname, name, prototype); \
        class(cname) .prototype name; \
-       INIT_STATIC(cname) \
+       _INIT_STATIC(cname) \
        { \
                this.name = METHOD_REFERENCE(cname, name); \
        } \
@@ -213,6 +223,16 @@ STATIC_INIT(RegisterClasses)
                this.name = val; \
        }
 
+#define STATIC_ATTRIB(cname, name, type, val) \
+       type cname##_##name; \
+       _INIT_STATIC(cname) \
+       { \
+               noref bool strzone; /* Error on strzone() calls. */ \
+               cname##_##name = val; \
+       }
+
+// cleanup potentially zoned strings from base classes
+
 #define ATTRIB_STRZONE(cname, name, type, val)      \
        class(cname).type name;                \
        INIT(cname) \
@@ -222,6 +242,15 @@ STATIC_INIT(RegisterClasses)
                this.name = strzone(val); \
        }
 
+#define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
+       type cname##_##name; \
+       _INIT_STATIC(cname) \
+       { \
+        if (cname##_##name) \
+            strunzone(cname##_##name); \
+               cname##_##name = val; \
+       }
+
 #define ATTRIBARRAY(cname, name, type, cnt) \
        class(cname).type name[cnt];