Support for "adopt" to use a custom smart pointer, patch included
Ben Wilhelm <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
I recently found I needed to combine the "custom smart pointer" feature for class creation, along with "adopt" for picking up the return value from a function. I couldn't find any way to do this with the current codebase so I implemented a new adopt function named adopt_container. I'm attaching a patch for it - it hasn't yet been extensively tested, but it seems to work. Let me know if there are any problems with it, or any better way to write it. I'd appreciate it if this functionality could make it into luabind-0.10 whenever it shows up, one way or another, even if the actual code behind it is different. :) -Ben ------------------------------------------------------------------------------ ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user
adopt_container.patch
(text/plain, 2.7 KB)
diff --git a/luabind/adopt_policy.hpp b/luabind/adopt_policy.hpp
index 5e81b94..7eb7ba2 100644
--- a/luabind/adopt_policy.hpp
+++ b/luabind/adopt_policy.hpp
@@ -30,6 +30,7 @@
#include <luabind/back_reference_fwd.hpp>
#include <luabind/wrapper_base.hpp>
#include <boost/type_traits/is_polymorphic.hpp>
+#include <boost/type_traits/remove_pointer.hpp>
namespace luabind { namespace detail
{
@@ -47,7 +48,7 @@ namespace luabind { namespace detail
inline void adjust_backref_ownership(void*, mpl::false_)
{}
- template<class Direction = lua_to_cpp>
+ template<typename Container, class Direction = lua_to_cpp>
struct adopt_pointer : pointer_converter
{
typedef adopt_pointer type;
@@ -83,8 +84,8 @@ namespace luabind { namespace detail
void converter_postcall(lua_State*, T, int) {}
};
- template<>
- struct adopt_pointer<cpp_to_lua>
+ template<typename Container>
+ struct adopt_pointer<Container, cpp_to_lua>
{
typedef adopt_pointer type;
@@ -103,7 +104,7 @@ namespace luabind { namespace detail
if (luabind::move_back_reference(L, ptr))
return;
- make_instance(L, std::auto_ptr<T>(ptr));
+ make_instance(L, Container(ptr));
}
};
@@ -122,7 +123,26 @@ namespace luabind { namespace detail
struct apply
{
typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
- typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<Direction>, only_accepts_nonconst_pointers>::type type;
+ typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<std::auto_ptr<typename boost::remove_pointer<T>::type>, Direction>, only_accepts_nonconst_pointers>::type type;
+ };
+ };
+
+ template<typename C, int N>
+// struct adopt_policy : converter_policy_tag
+ struct adopt_policy_container : conversion_policy<N>
+ {
+// BOOST_STATIC_CONSTANT(int, index = N);
+
+ static void precall(lua_State*, const index_map&) {}
+ static void postcall(lua_State*, const index_map&) {}
+
+ struct only_accepts_nonconst_pointers {};
+
+ template<class T, class Direction>
+ struct apply
+ {
+ typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
+ typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<C, Direction>, only_accepts_nonconst_pointers>::type type;
};
};
@@ -136,6 +156,13 @@ namespace luabind
{
return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>();
}
+
+ template<typename C, int N>
+ detail::policy_cons<detail::adopt_policy_container<C, N>, detail::null_type>
+ adopt_container(LUABIND_PLACEHOLDER_ARG(N))
+ {
+ return detail::policy_cons<detail::adopt_policy_container<C, N>, detail::null_type>();
+ }
}
#endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE