diff qt/QGlobal.d @ 183:d3f4f14d43a5

fixes with QObjects
author eldar
date Fri, 03 Jul 2009 20:53:07 +0000
parents d66bbb9a5053
children 7d9db724ee1d
line wrap: on
line diff
--- a/qt/QGlobal.d	Fri Jul 03 16:07:26 2009 +0000
+++ b/qt/QGlobal.d	Fri Jul 03 20:53:07 2009 +0000
@@ -585,4 +585,53 @@
 const ushort QT_EDITION_EVALUATION =  QT_EDITION_DESKTOP;
 
 mixin QT_END_NAMESPACE;
+
+package import tango.stdc.stdlib;
+
+template sizeOf(C : Object)
+{
+    const sizeOf = sizeOfImpl!(C);
+}
+
+size_t sizeOfImpl(C)()
+{
+    size_t size;
+
+    foreach (i, _; typeof(C.tupleof))
+    {
+        auto newSize = C.tupleof[i].offsetof + C.tupleof[i].sizeof;
+        if (newSize > size)
+            size = newSize;
+    }
+
+    return size;
+}
+
+scope class StackObject(C)
+{
+    byte[sizeOf!(C)] data;
+    bool constructed;
+
+    C opCall(A...)(A args)
+    {
+        assert(!constructed);
+
+        auto r = new(&data)C(args);
+        r.__stackAllocated = true;
+        constructed = true;
+
+        return r;
+    }
+
+    ~this()
+    {
+        if (constructed)
+        {
+            auto obj = cast(C)&data;
+            delete obj;
+        }
+    }
+}
+
 mixin QT_END_HEADER;
+