[Patch] Null pointer return support

Georgi Chulkov <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Hello,

I have written an enhancement to luabind which allows for returning nil 
from LUA to C++. If C++ expects a pointer (raw or smart) as a result of 
the function, it will receive NULL (or smart_pointer(NULL)) rather than 
get a cast_exception.

The patch is against luabind-0.9. It would be great if you would 
consider integrating it in future releases.

(Please include me in any replies because I am not subscribed to the list.)

Best,

Georgi Chulkov
Masthead Studios

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
null_pointer_return.patch (text/plain, 1.3 KB)
Index: luabind/detail/make_instance.hpp
===================================================================
--- luabind/detail/make_instance.hpp
+++ luabind/detail/make_instance.hpp
@@ -39,6 +39,11 @@
 template <class T>
 std::pair<class_id, void*> get_dynamic_class(lua_State* L, T* p)
 {
+    if (!p)
+    {
+        return get_dynamic_class_aux(L, p, mpl::false_());
+    }
+
     return get_dynamic_class_aux(L, p, boost::is_polymorphic<T>());
 }
 
Index: luabind/detail/policy.hpp
===================================================================
--- luabind/detail/policy.hpp
+++ luabind/detail/policy.hpp
@@ -288,10 +288,27 @@
 		}
 
 		template<class T>
-		int match(lua_State* L, by_value<T>, int index)
+		int match(lua_State* L, by_value<T> bv, int index)
 		{
-			// special case if we get nil in, try to match the holder type
+			return match(L, bv, index, has_get_pointer<T>());
+		}
+
+		template<class T>
+		int match(lua_State* L, by_value<T> bv, int index, mpl::true_)
+		{
 			if (lua_isnil(L, index))
+			{
+				make_instance(L, T());
+				lua_replace(L, index - 1);
+			}
+
+			return match(L, bv, index, mpl::false_());
+		}
+
+		template<class T>
+		int match(lua_State* L, by_value<T>, int index, mpl::false_)
+		{
+			if (lua_isnil(L, index))
 				return -1;
 
 			object_rep* obj = get_instance(L, index);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.