a simple lua c++ helper library
C++ Python Lua C
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
include
src
MIT-LICENSE
README

README

                                README
                                ======

Table of Contents
=================
1 LuaH 
    1.1 What is LuaH? 
    1.2 Tutor 
        1.2.1 Import a function to Lua 
        1.2.2 Call a lua function 
        1.2.3 Import a class to Lua 


1 LuaH 
~~~~~~~

1.1 What is LuaH? 
==================
When using lua in C++, the lua's native api is well designed, 
but import functions to lua become a repetitive work. 
So I wrote this library to help me to do this work. Compare 
to other bind librarys, it keeps very lightweight. I'm trying 
to keep the API simple.

1.2 Tutor 
==========

1.2.1 Import a function to Lua 
-------------------------------
in C++:
You can import a function to lua as follow:


  // function:
  int function_to_export(int x) {
      return x + 1;
  }
  // import:
  // ...
    luah::add_func(L, "my_func", function_to_export);
  // ...

then, use it in lua:


  print(my_func(3))

You can also import a function to a table:


  // following code import the function_to_export to table
  lua_newtable(L);
  luah::add_func_table(L, -1, "my_func", function_to_export);
  lua_setglobal(L, "my_table");

1.2.2 Call a lua function 
--------------------------
if we have a lua function:


  function double(x)
      return x * 2
  end

then we can call it in C++:


  int ret = luah::call_func<int>(L, "double", 6);

1.2.3 Import a class to Lua 
----------------------------
To make import a class to lua, it can be do as follow:

  // a example class
  class MyClass {
  public:
      MyClass(int x, int y) : 
          x_(x), y_(y) {}
      int get_sum() {
          return x_ + y_;
      }
      void set_x(int x) {
          x_ = x;
      }
      void set_y(int y) {
          y_ = y;
      }
  private:
      int x_, y_;
  };
  
  // import code:
      luah::add_class<MyClass>("myclass")[luah::ctor<int, int>()]
          << luah::method("get_sum", &MyClass::get_sum)
          << luah::method("set_x", &MyClass::set_x)
          << luah::method("set_y", &MyClass::set_y);

then use in lua:


  a = myclass(2, 3)
  print(a.get_sum())
  a.set_x(4)
  print(a.get_sum())

hint: 
  - the [luah::ctor<int, int>] is the to set the construct function of the class,
      it means new T(int, int) will be called to create a new instance,
      if you just want to call new T(), this part could be ignored.