]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/oo.qh
Type check class methods
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
index 0a945109709a232ecbb689b5d5f3063ca17e4089..4615ea970cd7b64bb210f03ad2218f6b03cda8a4 100644 (file)
@@ -1,18 +1,18 @@
-#ifndef OO_H
-#define OO_H
+#pragma once
 
 #include "misc.qh"
 #include "nil.qh"
 #include "static.qh"
 
 #ifdef MENUQC
-       #define NULL (null_entity)
+       #define NULL (0, null_entity)
 #else
-       #define NULL (world)
+       #define NULL (0, world)
 #endif
 
 .vector origin;
 .bool pure_data;
+/** @deprecated, use new_pure or NEW(class) */
 #define make_pure(e) \
        MACRO_BEGIN \
        { \
 
 .string classname;
 /** Location entity was spawned from in source */
-.string sourceLocFile;
-.int sourceLocLine;
+.string sourceLoc;
 entity _spawn();
-entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
+
+#ifndef SPAWN_PURE
+#define SPAWN_PURE 0
+#endif
+
+// pure entities: need no .origin
+#if SPAWN_PURE
+entity spawn_pure() = #600;
+#else
+#define spawn_pure() _spawn()
+#endif
+
+entity __spawn(string _classname, string _sourceLoc, bool pure)
 {
-       entity this = _spawn();
+       entity this = pure ? spawn_pure() : _spawn();
        this.classname = _classname;
-       this.sourceLocFile = _sourceFile;
-       this.sourceLocLine = _sourceLine;
-       if (pure) make_pure(this);
+       this.sourceLoc = _sourceLoc;
+       if (pure) {
+               make_pure(this);
+               #ifdef CSQC
+               setorigin(this, '0 0 10000');
+               #endif
+               #ifdef SVQC
+        setorigin(this, '0 0 -10000');
+        #endif
+       }
        return this;
 }
 
 
-#define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
+#define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
+#define EVAL_entityclass(...) __VA_ARGS__
 #define entityclass_1(name) entityclass_2(name, Object)
 #ifndef QCC_SUPPORT_ENTITYCLASS
        #define entityclass_2(name, base) typedef entity name
        #define class(name)
-       #define new(class) __spawn( #class, __FILE__, __LINE__, false)
+       #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
 #else
        #define entityclass_2(name, base) entityclass name : base {}
        #define class(name) [[class(name)]]
-       #define new(class) ((class) __spawn( #class, __FILE__, __LINE__, false))
+       #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
 #endif
-#define spawn() __spawn("entity", __FILE__, __LINE__, false)
+/** entities you care about seeing (.origin works) */
+#define new(class) _new(class, false)
+/** purely logical entities (.origin doesn't work) */
+#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)
 {
-       _clearentity_ent = new(clearentity);
-       make_pure(_clearentity_ent);
+       _clearentity_ent = new_pure(clearentity);
 }
 void clearentity(entity e)
 {
@@ -77,22 +106,17 @@ void clearentity(entity e)
 // The parameter is used across [[accumulate]] functions
 
 // Macros to hide this implementation detail:
-#ifdef GMQCC
+#ifdef __STDC__
        #define NEW(cname, ...) \
-               OVERLOAD(spawn##cname, new(cname),##__VA_ARGS__)
-
+               OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
        #define CONSTRUCT(cname, ...) \
-               OVERLOAD(spawn##cname, this,##__VA_ARGS__)
+               OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
 #else
-       #define NEW_(cname, ...) \
-               OVERLOAD_(spawn##cname, __VA_ARGS__)
        #define NEW(cname, ...) \
-               NEW_(cname, new(cname),##__VA_ARGS__)(new(cname),##__VA_ARGS__)
+               OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
 
-       #define CONSTRUCT_(cname, ...) \
-               OVERLOAD_(spawn##cname, __VA_ARGS__)
        #define CONSTRUCT(cname, ...) \
-               CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
+               OVERLOAD(spawn##cname, this,##__VA_ARGS__)
 #endif
 
 #define CONSTRUCTOR(cname, ...) \
@@ -116,8 +140,7 @@ STATIC_INIT(RegisterClasses)
        entity cname##_vtbl; \
        void cname##_vtbl_init() \
        { \
-               cname e = new(vtbl); \
-               make_pure(e); \
+               cname e = new_pure(vtbl); \
                spawn##cname##_static(e); \
                e.vtblname = #cname; \
                /* Top level objects refer to themselves */ \
@@ -132,6 +155,7 @@ STATIC_INIT(RegisterClasses)
 #define CLASS(cname, base)                  \
        entityclass(cname, base);               \
        class(cname).bool instanceOf##cname;   \
+    bool is_##cname(entity e) { return e.instanceOf##cname; } \
        VTBL(cname, base)                       \
        INIT_STATIC(cname) \
        {                    \
@@ -171,6 +195,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) \
@@ -203,8 +237,11 @@ 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))
        {
+           TC(Object, this);
                string s = _("No description");
                if (cvar("developer"))
                {
@@ -216,13 +253,12 @@ 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))
        {
+           TC(Object, this);
                returns(sprintf("entity %i", this), "nopreview_map");
        }
 ENDCLASS(Object)
 #undef spawn_static
 #undef spawn_1
 #undef _vtbl
-
-#endif