Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] Add support for object properties #22

Open
GoogleCodeExporter opened this issue Mar 16, 2015 · 2 comments
Open

[REQ] Add support for object properties #22

GoogleCodeExporter opened this issue Mar 16, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

Could it be possible to add support for properties?

As described there: http://lua-users.org/wiki/ObjectProperties

Original issue reported on code.google.com by [email protected] on 1 Apr 2013 at 10:41

@GoogleCodeExporter
Copy link
Author

Sorry, forgot to mention: in SLB v3.

Original comment by [email protected] on 1 Apr 2013 at 10:42

@GoogleCodeExporter
Copy link
Author

Hi! I did that myself in a lazy way.

for example if you have:

class A
{
public:
    string str;

    string getStr()     { return str; }
    void setStr(const string& str) { this->str = str; }
};

    SLB::Class<A>("Namespace::A")
        .constructor()
        .set("getStr", &A::getStr)
        .set("setStr", &A::setStr)
        .property("str", &A::str)
    ;

then you can use it from lua these ways:

a = A()
a:setStr('123')
print(a:getStr())
a.str = '321'
print(a.str)

with my changes to SLB.cpp you should globally define SLB_OBJECT_PROPERTY so 
that it could be seen from SLB.cpp
and then you can use the following:

a.Str = '456'
print(a.Str)

that is if SLB does not find a property named Str then in searches for 
corresponding functions getStr and setStr

here are my changes to SLB.cpp (changeset:   288:e440976b9f9f)

diff --git a/source/SLB/SLB.cpp b/source/SLB/SLB.cpp
index 5394ab4..63d4a8f 100644
--- a/source/SLB/SLB.cpp
+++ b/source/SLB/SLB.cpp
@@ -342,6 +342,21 @@ namespace SLB {
             prop->get(L,1);
             return 1;
           }
+#ifdef SLB_OBJECT_PROPERTY
+         {
+             auto fkey = String("get") + key;
+             if(getElementsMap().count(fkey))
+             {
+                 auto func = static_cast<const 
FuncCall*>(getElementsMap().at(fkey).get());
+                 if(func)
+                 {
+                     lua_pop(L, 1);
+                     const_cast<FuncCall*>(func)->call(L);
+                     return 1;
+                 }
+             }
+         }
+#endif
         }
       }
       if (result < 1)
@@ -379,6 +394,21 @@ namespace SLB {
         prop->set(L,1);
         return 1;
       }
+#ifdef SLB_OBJECT_PROPERTY
+   {
+       auto fkey = String("set") + key;
+       if(getElementsMap().count(fkey))
+       {
+           auto func = static_cast<const FuncCall*>(getElementsMap().at(fkey).get());
+           if(func)
+           {
+               lua_replace(L, 2);
+               const_cast<FuncCall*>(func)->call(L);
+               return 1;
+           }
+       }
+   }
+#endif
     }
     if (_meta__newindex[type].valid())
     {

Original comment by [email protected] on 7 Aug 2013 at 10:32

Attachments:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant