forked from luabind/luabind
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intrusive_ptr_converter can be used for all smart pointer types where construction of multiple smart pointers from a single raw pointer is not a problem, such as boost::intrusive_ptr (for which specializations of luabind::default_converter are provided). Includes test.
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright Chsritian Neumüller 2013. Use, modification and distribution is | ||
// subject to the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef LUABIND_INTRUSIVE_PTR_CONVERTER_HPP_INCLUDED | ||
#define LUABIND_INTRUSIVE_PTR_CONVERTER_HPP_INCLUDED | ||
|
||
#include <luabind/config.hpp> | ||
#include <luabind/detail/decorate_type.hpp> | ||
#include <luabind/detail/policy.hpp> | ||
|
||
#include <boost/mpl/bool.hpp> | ||
#include <boost/smart_ptr/intrusive_ptr.hpp> | ||
|
||
namespace luabind { | ||
namespace detail { | ||
template <class P> | ||
struct intrusive_ptr_converter | ||
: default_converter<typename P::element_type*> | ||
{ | ||
private: | ||
typedef P ptr_t; | ||
typedef typename ptr_t::element_type* rawptr_t; | ||
|
||
public: | ||
typedef mpl::false_ is_native; | ||
|
||
template <class U> | ||
int match(lua_State* L, U, int index) | ||
{ | ||
return default_converter<rawptr_t>::match( | ||
L, LUABIND_DECORATE_TYPE(rawptr_t), index); | ||
} | ||
|
||
template <class U> | ||
ptr_t apply(lua_State* L, U, int index) | ||
{ | ||
rawptr_t raw_ptr = default_converter<rawptr_t>::apply( | ||
L, LUABIND_DECORATE_TYPE(rawptr_t), index); | ||
return ptr_t(raw_ptr); | ||
} | ||
|
||
void apply(lua_State* L, ptr_t const& p) | ||
{ | ||
detail::value_converter().apply(L, p); | ||
} | ||
|
||
template <class U> | ||
void converter_postcall(lua_State*, U const&, int) | ||
{} | ||
}; | ||
} // namespace detail | ||
|
||
template <typename T> | ||
struct default_converter<boost::intrusive_ptr<T> >: | ||
detail::intrusive_ptr_converter<boost::intrusive_ptr<T> > {}; | ||
|
||
template <typename T> | ||
struct default_converter<boost::intrusive_ptr<T> const&>: | ||
detail::intrusive_ptr_converter<boost::intrusive_ptr<T> > {}; | ||
|
||
} // namespace luabind | ||
|
||
#endif // LUABIND_INTRUSIVE_PTR_CONVERTER_HPP_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright Chsritian Neumüller 2013. Use, modification and distribution is | ||
// subject to the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include "test.hpp" | ||
|
||
#include <luabind/intrusive_ptr_converter.hpp> | ||
#include <luabind/luabind.hpp> | ||
|
||
#include <boost/noncopyable.hpp> | ||
|
||
using boost::intrusive_ptr; | ||
|
||
namespace { | ||
|
||
struct B: private boost::noncopyable, counted_type<B> | ||
{ | ||
B(int val_): ref_cnt(0), val(val_) {} | ||
virtual ~B() { | ||
std::cout << "~B\n"; | ||
TEST_CHECK(ref_cnt == 0); | ||
} | ||
|
||
int ref_cnt; | ||
int val; | ||
}; | ||
|
||
COUNTER_GUARD(B); | ||
|
||
struct D: B | ||
{ | ||
D(int val_): B(val_) {} | ||
}; | ||
|
||
void intrusive_ptr_add_ref(B* b) | ||
{ | ||
++b->ref_cnt; | ||
} | ||
|
||
void intrusive_ptr_release(B* b) | ||
{ | ||
if (!--b->ref_cnt) | ||
delete b; | ||
} | ||
|
||
void needs_b(intrusive_ptr<B> pb, int expected_rc) | ||
{ | ||
TEST_CHECK(pb->ref_cnt == expected_rc); | ||
} | ||
|
||
void needs_d(intrusive_ptr<D> pd) | ||
{ | ||
TEST_CHECK(pd->ref_cnt == 2); | ||
} | ||
|
||
intrusive_ptr<B> filter_b(intrusive_ptr<B> pb) | ||
{ | ||
TEST_CHECK(pb->ref_cnt == 2); | ||
return pb; | ||
} | ||
|
||
} // namespace unnamed | ||
|
||
void test_main(lua_State* L) | ||
{ | ||
using namespace luabind; | ||
|
||
module(L) [ | ||
class_<B, intrusive_ptr<B> >("B") | ||
.def(constructor<int>()) | ||
.def_readonly("val", &B::val) | ||
.def_readonly("ref_cnt", &B::ref_cnt), | ||
class_<D, B, intrusive_ptr<D> >("D") | ||
.def(constructor<int>()), | ||
def("needs_b", &needs_b), | ||
def("needs_d", &needs_d), | ||
def("filter_b", &filter_b) | ||
]; | ||
|
||
DOSTRING(L, | ||
"d = D(1)\n" | ||
"print(d.val, d.ref_cnt)\n" | ||
"assert(d.val == 1)\n" | ||
"assert(d.ref_cnt == 1)\n" | ||
); | ||
|
||
DOSTRING(L, | ||
"needs_d(d)\n" | ||
"needs_b(d, 2)\n" | ||
); | ||
|
||
DOSTRING(L, | ||
"bd = filter_b(d)\n" | ||
"assert(bd.val == 1)\n" | ||
"assert(bd.ref_cnt == 2)\n" | ||
"needs_b(bd, 3)\n" | ||
"assert(bd.ref_cnt == 2)\n" | ||
); | ||
|
||
DOSTRING(L, | ||
"d = nil\n" | ||
"collectgarbage()\n" | ||
"collectgarbage()\n" | ||
"assert(bd.ref_cnt == 1)\n" | ||
); | ||
} |