AdUki / LuaState

Lua51-52 binding library
Apache License 2.0
77 stars 14 forks source link

How to bind class like LuaBridge? #15

Closed leaker closed 9 years ago

leaker commented 10 years ago

In LuaBridge, can register class like this:

struct A {
  static int staticData;
  static float staticProperty;

  static float getStaticProperty () { return staticProperty; }
  static void setStaticProperty (float f) { staticProperty = f; }
  static void staticFunc () { }

  static int staticCFunc () { return 0; }

  std::string dataMember;

  char dataProperty;
  char getProperty () const { return dataProperty; }
  void setProperty (char v) { dataProperty = v; }

  void func1 () { }
  virtual void virtualFunc () { }

  int cfunc (lua_State* L) { return 0; }
};

struct B : public A {
  double dataMember2;

  void func1 () { }
  void func2 () { }
  void virtualFunc () { }
};

int A::staticData;
float A::staticProperty;

are registered using:

getGlobalNamespace (L)
  .beginNamespace ("test")
    .beginClass <A> ("A")
      .addConstructor <void (*) (void)> ()
      .addStaticData ("staticData", &A::staticData)
      .addStaticProperty ("staticProperty", &A::staticProperty)
      .addStaticFunction ("staticFunc", &A::staticFunc)
      .addStaticCFunction ("staticCFunc", &A::staticCFunc)
      .addData ("data", &A::dataMember)
      .addProperty ("prop", &A::getProperty, &A::setProperty)
      .addFunction ("func1", &A::func1)
      .addFunction ("virtualFunc", &A::virtualFunc)
      .addCFunction ("cfunc", &A::cfunc)
    .endClass ()
    .deriveClass <B, A> ("B")
      .addConstructor <void (*) (void)> ()
      .addData ("data", &B::dataMember2)
      .addFunction ("func1", &B::func1)
      .addFunction ("func2", &B::func2)
    .endClass ()
  .endNameSpace ();

Use in lua:

local obj_a = A() -- create object obj_a from new A

How to in LuaState?

AdUki commented 10 years ago

You can't bind whole classes in LuaState. But you can still use LuaBridge with LuaState if you want to use this feature with no problem...

leaker commented 9 years ago

Thx AdUki. and I like C++11 style code. it's looks cool. :)

AdUki commented 9 years ago

Also it is kind of duplicate of #5