Introspection and navel gazing

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Introspection and navel gazing

Thomas Nelson-10
Perhaps I'm going about this in entirely the wrong way, or this may not even
be possible as things are now... or I'm misunderstanding something. So some
pointers here would be appreciated.  

A long winded description:
In the (perhaps oversimplified) code below is a class NavelGazer. An
instance of this class may be created in the C++ and passed to Lua or it may
be created by a Lua script.

It may also be subclassed in lua and have new methods added.

I need to be able to examine an instance of NavelGazer from C++ and see if
an arbitrarily named method has been defined in the lua subclass.

<C++>
class NavelGazer
{
  public:
    NavelGazer( ) {}
    virtual ~NavelGazer() {}
   
    void examineMyself(const std::string &strForWhat)
    {
      luabind::class_info cinfo (luabind::get_class_info(*this));
     
      luabind::object method = cinfo.methods[strForWhat];
     
      /* do something with the method */
    }
   
    static void bindScripting(Lua_state *pScript)
    { using namespace luabind;
     
      module(L)
      [
        class_<NavelGazer>("NavelGazer")
          .def(constructor<>())
          .def("examine", &NavelGazer::examineMyself)
      ];

    }
};
</C++>
<Lua>
class 'ASubClass' (NavelGazer)

function ASubClass:__init()
        NavelGazer.__init();
end

function ASubClass:ALuaOnlyMethod()
        return print("haha can't find me.");
end

------------------
JustBase = NavelGazer();
JustBase:examine("ALuaOnlyMethod"); -- would NOT find anything.

Derived = ASubClass();
Derived:examine("ALuaOnlyMethod"); -- should find it
</Lua>

--
Thomas Nelson  [hidden email]  
--------------------------------------------------------------
"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.)




------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Solved: Introspection and navel gazing

Thomas Nelson-10
It took some digging but I was barking up the wrong tree... for some reason
I kept thinking that I needed to examine the class_info structure as opposed
to the wrapper_base.

The attached diff dose 3 things:
1) Adds weak_ref::is_valid() - inspects a weak_ref and returns true if it
has been initialized.

2) Adds 2 wrapper_base::has_method functions.  One takes the name of the
method to check for, the other takes a lua state and the name of the method.
They both return true if the method has been defined on the lua side.  The
version without the lua state will use the state registered in the wrapper's
weak_ref if any.

bool wrapper_base::has_method(const char *name);
bool wrapper_base::has_method(lua_State *L, const char *name);

3) Added an extra signature to wrapper_base::call.  The new signature takes
a lua_State * and will execute the method on the supplied stack rather than
the one registered in the weak_ref.
wrapper_base::call( const char *name, ... )
wrapper_base::call( lua_State *L, const char *name, ... )


> -----Original Message-----
> From: Thomas Nelson [mailto:[hidden email]]
>
> Perhaps I'm going about this in entirely the wrong way, or this may not
> even
> be possible as things are now... or I'm misunderstanding something. So
> some
> pointers here would be appreciated.
>
> A long winded description:
> In the (perhaps oversimplified) code below is a class NavelGazer. An
> instance of this class may be created in the C++ and passed to Lua or it
> may
> be created by a Lua script.
>
> It may also be subclassed in lua and have new methods added.
>
> I need to be able to examine an instance of NavelGazer from C++ and see if
> an arbitrarily named method has been defined in the lua subclass.

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

luabindudiff.txt (9K) Download Attachment