Lua add an Empty/Nil shared-pointer constructor
authorRobin Gareus <robin@gareus.org>
Sat, 21 Jan 2017 19:07:54 +0000 (20:07 +0100)
committerRobin Gareus <robin@gareus.org>
Sat, 21 Jan 2017 19:18:27 +0000 (20:18 +0100)
libs/lua/LuaBridge/detail/Namespace.h

index 7f8d44859cc9de40dd5bbe4b442e09221bd5374f..9a9b3c44cb006fadf2caf6d7b4ca6ca0b266029e 100644 (file)
@@ -482,6 +482,14 @@ private:
       return 1;
     }
 
+    template <class T>
+    static int ctorNilPtrPlacementProxy (lua_State* L)
+    {
+      const T* newobject = new T ();
+      Stack<T>::push (L, *newobject);
+      return 1;
+    }
+
     //--------------------------------------------------------------------------
     /**
       Pop the Lua stack.
@@ -1279,6 +1287,24 @@ private:
       return addConstructor <void (*) ()> ();
     }
 
+    WSPtrClass <T>& addNilPtrConstructor ()
+    {
+      FUNDOC ("Weak/Shared Pointer Constructor", "", MemFn)
+      set_shared_class ();
+      lua_pushcclosure (L,
+          &shared. template ctorNilPtrPlacementProxy <boost::shared_ptr<T> >, 0);
+      rawsetfield(L, -2, "__call");
+
+      set_weak_class ();
+      // NOTE: this constructs an empty weak-ptr,
+      // ideally we'd construct a weak-ptr from a referenced shared-ptr
+      lua_pushcclosure (L,
+          &weak. template ctorNilPtrPlacementProxy <boost::weak_ptr<T> >, 0);
+      rawsetfield(L, -2, "__call");
+
+      return *this;
+    }
+
     WSPtrClass <T>& addExtCFunction (char const* name, int (*const fp)(lua_State*))
     {
       DATADOC ("Weak/Shared Ext C Function", name, fp)