binding protected constructors

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

binding protected constructors

"Michael Müller"
Hello,

I have a question about protected constructors.

I have a class:
sub {
public:
   int getValue(void) { return mValue; };

protected:
   sub(int Value) { mValue = Value; };

private:
   int mValue;
};

And I have another class:
base {
   ...
public:
   sub* getSub(int Index);

private:
   std::vector<sub*> mList;
}

With getSub returning sub* on a given index.

And now I want to do something like the following C++ code in Lua.
base* mBase = new base();
...
int x = mBase->getSub(2)->getValue();


Should look like this in Lua:
mBase = base()
...
x = mBase:getSub(2):getValue()

The binding:
luabind::module(LuaState)
[
   class_<sub>("sub")
];

My Problem is, that I can't bind the class sub, as the constructor cannot be accessed by Luabind because it is protected. Any suggestions?


***********************************************************


And perhaps something that should go in the docu as a warning or "how not to do" as it made my hairs go gray.
class a{
   ...
};

class b{
   ...
   a* mA;
}

struct b_wrap : b, luabind::wrap_base
{
   b_wrap() : b() {};
   ...
};


luabind::module(LuaState)
[
   class_<a>("a")
   .def(constructor<>()),

   class_<b, b_wrap>("b")
   .def(constructor<>())
   .def_readwrite("mA", &b::mA)
];


When you are doing this in Lua:
B = b()
b.mA = a()
and then pass b to C++ via adopt-policy the pointer mA in C++ is empty and   the application crashes.

Solution:
class b{
   ...
   void addA(void) { mA = new a(); };
   a* mA;
};

Binding:
class_<b, b_wrap>("b")
.def(constructor<>())
.def("addA", &b::addA)
.def_readwrite("mA", &b::mA)

Lua:
B = b()
b:addA()

Now you can access mA from Lua and from C++ and delete B from C++ without a crash because mA gets lost. Perhaps this prevents somebody from the stupidy I was doing.















   

--


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: binding protected constructors

Art Tevs
Hi, Michael!

My solution was to derive new class from the class with the protected
constructor. Then register the newly derived class with the function pointers
to the base class, i.e.:

class A{
public:
        do_magic();

protected:
        A();
}

class Derived : public A {
public:
        Derived();
}

then

class_<Derived>("A")
.def(constructor<>())
.def("doMagic", &A::do_magic);

This works for my application. Note: You do not have to write class_<Derived,
A>, because A was not registered before by luabind in this example.
Documentation does not mention this. The example in the doc is probably for
derived classes which base was already registered, I think ;)


MfG, Art


On Tuesday 29 August 2006 14:51, Michael Müller wrote:

> Hello,
>
> I have a question about protected constructors.
>
> I have a class:
> sub {
> public:
>    int getValue(void) { return mValue; };
>
> protected:
>    sub(int Value) { mValue = Value; };
>
> private:
>    int mValue;
> };
>
> And I have another class:
> base {
>    ...
> public:
>    sub* getSub(int Index);
>
> private:
>    std::vector<sub*> mList;
> }
>
> With getSub returning sub* on a given index.
>
> And now I want to do something like the following C++ code in Lua.
> base* mBase = new base();
> ...
> int x = mBase->getSub(2)->getValue();
>
>
> Should look like this in Lua:
> mBase = base()
> ...
> x = mBase:getSub(2):getValue()
>
> The binding:
> luabind::module(LuaState)
> [
>    class_<sub>("sub")
> ];
>
> My Problem is, that I can't bind the class sub, as the constructor cannot
> be accessed by Luabind because it is protected. Any suggestions?
>
>
> ***********************************************************
>
>
> And perhaps something that should go in the docu as a warning or "how not
> to do" as it made my hairs go gray. class a{
>    ...
> };
>
> class b{
>    ...
>    a* mA;
> }
>
> struct b_wrap : b, luabind::wrap_base
> {
>    b_wrap() : b() {};
>    ...
> };
>
>
> luabind::module(LuaState)
> [
>    class_<a>("a")
>    .def(constructor<>()),
>
>    class_<b, b_wrap>("b")
>    .def(constructor<>())
>    .def_readwrite("mA", &b::mA)
> ];
>
>
> When you are doing this in Lua:
> B = b()
> b.mA = a()
> and then pass b to C++ via adopt-policy the pointer mA in C++ is empty and
>  the application crashes.
>
> Solution:
> class b{
>    ...
>    void addA(void) { mA = new a(); };
>    a* mA;
> };
>
> Binding:
> class_<b, b_wrap>("b")
> .def(constructor<>())
> .def("addA", &b::addA)
> .def_readwrite("mA", &b::mA)
>
> Lua:
> B = b()
> b:addA()
>
> Now you can access mA from Lua and from C++ and delete B from C++ without a
> crash because mA gets lost. Perhaps this prevents somebody from the stupidy
> I was doing.

               
___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user