redux and fix? objects derived from wrap_base

View: New views
2 Messages — Rating Filter:   Alert me  

redux and fix? objects derived from wrap_base

by Thomas Nelson-10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I was running into a problem a while ago with objects derived from
luabind:wrap_base.  I think I have a fix but would like some feedback on
whether or not I'm completely off base and what some of the side effects may
be.

The problem:
I have a class structure that looks like this:

class MyObject: public luabind::wrap_base
{
    public:
        MyObject(): luabind::wrap_base {}
};


MyObject *pObj = new MyObject();
luabind::globals(pLState)["_Object"] = pObj;

In the code, this assignment will assert, wrap_base is constructed with its
default constructor and when push attempts to determine the back reference
it chokes since there is no lua state associated with the object.

The fix?:
In weakref.cpp change the state method to look like this:
lua_State* weak_ref::state() const
{ // removed the assert and added an explicit NULL return
--> if (m_impl)
-->     return m_impl->state;
--> return NULL;
}

In back_reference.hpp add the two marked lines to get_back_reference:
template<class T>
bool get_back_reference(lua_State* L, T const& x)
{
#ifndef LUABIND_NO_RTTI
    if (wrap_base const* w = detail::get_back_reference(x))
    {
-->     if (!detail::wrap_access::ref(*w).state())
-->         return false;
        detail::wrap_access::ref(*w).get(L);
        return true;
    }
#endif
    return false;
}

--
Thomas Nelson  tdark@...  
--------------------------------------------------------------
"If you still have gas, you're not lost".
- French explorer Pierre Frontage.
(M.Frontage was so influential in the exploration of North America many
roads are still named after him.)




------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: redux and fix? objects derived from wrap_base

by Ilia Trendafilov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I will not comment your fix but the way you use luabind. Take
everything I say with a grain of salt as I'm not very experienced
luabind user - I hope the community will correct any mistakes in this
message.

Summary: don't instantiate luabind wrappers from C++, instantiate the
wrapped class instead.

Details:
I vaguely remember being in the situation you are describing. What I
did, however, was to restructure my classes:

class MyObject
{
   // C++ interface goes here
};

// somewhere else, in the lua-specific parts of your application

class MyObjectWrapper : public luabind::wrap_base, public MyObject
{
   // virtual functions that call(), etc
};

From this point, the code:
MyObject *pObj = new MyObject();
luabind::globals(pLState)["_Object"] = pObj;
should just work. You'll still be able to derive this class in Lua as
usual and you'll still get all the benefits from having a wrapper. And
bonus benefit: separating your application code from luabind code -
everyone who #include "MyObject.h" had to include a lot of luabind
headers which are not exactly fast to compile.



On Tue, Aug 4, 2009 at 6:37 AM, Thomas Nelson<tdark@...> wrote:

> I was running into a problem a while ago with objects derived from
> luabind:wrap_base.  I think I have a fix but would like some feedback on
> whether or not I'm completely off base and what some of the side effects may
> be.
>
> The problem:
> I have a class structure that looks like this:
>
> class MyObject: public luabind::wrap_base
> {
>    public:
>        MyObject(): luabind::wrap_base {}
> };
>
>
> MyObject *pObj = new MyObject();
> luabind::globals(pLState)["_Object"] = pObj;
>
> In the code, this assignment will assert, wrap_base is constructed with its
> default constructor and when push attempts to determine the back reference
> it chokes since there is no lua state associated with the object.
>
> The fix?:
> In weakref.cpp change the state method to look like this:
> lua_State* weak_ref::state() const
> { // removed the assert and added an explicit NULL return
> --> if (m_impl)
> -->     return m_impl->state;
> --> return NULL;
> }
>
> In back_reference.hpp add the two marked lines to get_back_reference:
> template<class T>
> bool get_back_reference(lua_State* L, T const& x)
> {
> #ifndef LUABIND_NO_RTTI
>    if (wrap_base const* w = detail::get_back_reference(x))
>    {
> -->     if (!detail::wrap_access::ref(*w).state())
> -->         return false;
>        detail::wrap_access::ref(*w).get(L);
>        return true;
>    }
> #endif
>    return false;
> }
>
> --
> Thomas Nelson  tdark@...
> --------------------------------------------------------------
> "If you still have gas, you're not lost".
> - French explorer Pierre Frontage.
> (M.Frontage was so influential in the exploration of North America many
> roads are still named after him.)
>
>
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> luabind-user mailing list
> luabind-user@...
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user