Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <lua.hpp>
- #include <luabind/luabind.hpp>
- typedef std::string String;
- typedef float Real;
- typedef int Entero;
- // lo defino estatico, para facilitar la prueba
- lua_State* pL;
- class Vector2
- {
- public:
- Vector2(Real _x, Real _y)
- {
- x = _x;
- y = _y;
- }
- virtual ~Vector2(){}
- inline Real length () const
- {
- return sqrtf( x * x + y * y );
- }
- void polimorfico_test()
- {
- std::cout << "[C++] Soy Vector2::polimorfico_test SIN parametros" << std::endl;
- }
- void polimorfico_test(Entero var)
- {
- std::cout << "[C++] Soy Vector2::polimorfico_test CON parametros " << var << std::endl;
- }
- public:
- Real x, y;
- };
- // [static]
- void hola_mundo_cpp()
- {
- std::cout << "[C++] Ejecutando hola_mundo_cpp()" << std::endl;
- }
- // [static]
- Entero suma_cpp(Entero a, Entero b)
- {
- std::cout << "[C++] Ejecutando suma_cpp(Entero, Entero) con parametros " << a << " y " << b << std::endl;
- return a + b;
- }
- // auxiliares para manejar Lua + Luabind
- luabind::object getGlobals()
- {
- return luabind::globals(pL);
- }
- Real getReal(String nombre_var)
- {
- return luabind::object_cast<Real>(getGlobals()[nombre_var]);
- }
- Entero getEntero(String nombre_var)
- {
- return luabind::object_cast<Entero>(getGlobals()[nombre_var]);
- }
- void callFunctionStatic0parms(String nombre)
- {
- luabind::call_function<void>(pL, nombre.c_str());
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- int main()
- {
- //create a lua state
- pL = luaL_newstate();
- // abre las librerias de lua
- luaL_openlibs(pL);
- // abre luabind
- luabind::open(pL);
- // ahora bindeamos las clases y funciones
- luabind::module(pL)
- [
- // registramos clases
- // Esta clase no tiene herencia, supongamos que hereda de la clase Tupla, se definiría así:
- //luabind::class_<Vector2, luabind::bases<Tupla> >("Vector2")
- // Con la herencia en Lua -> los beneficios son evidentes. Soporte herencia múltiple, es la polla xD
- luabind::class_<Vector2>("Vector2")
- // constructor
- .def(luabind::constructor<Real, Real>())
- // metodos
- .def("length", &Vector2::length)
- // Para hacer polimorfismo se deben definir las cláusuras a mano
- // si no hay ambigüedad, luabind las saca magicamente
- .def("polimorfico_test", (void (Vector2::*)(Entero))&Vector2::polimorfico_test)
- .def("polimorfico_test", (void (Vector2::*)())&Vector2::polimorfico_test)
- // se puede sobrecarga de operadores ...
- // miembros
- .def_readwrite("x", &Vector2::x)
- .def_readwrite("y", &Vector2::y)
- // si tuvieramos getX / setX y getX y setX se puede hacer esto:
- //.property("x", &Personaje::getX, &Personaje::setX)
- //.property("y", &Personaje::getY, &Personaje::setY)
- // tambien se puede hacer def_readonly para una var protegida.
- ,
- // registramos funciones estaticas
- luabind::def("hola_mundo_cpp", &hola_mundo_cpp),
- luabind::def("suma_cpp", &suma_cpp)
- ];
- // parseamos el script. Se puede pasar en texto plano, o compilado en bytecode.
- // se supone que en bytecode carga más rápido, pero es insignificante
- // se suele usar más para ofuscar / proteger código
- if (int error = luaL_dofile(pL, "test.lua") != 0)
- {
- std::cout << "\n[C++]: ERROR(" << error << "): Problem with lua" << "script file!\n\n" << std::endl;
- return 1;
- }
- // llamar métodos estaticos definidos en lua
- luabind::call_function<void>(pL, "hola_mundo_lua");
- callFunctionStatic0parms("hola_mundo_lua");
- luabind::call_function<void>(pL, "suma_lua", 1, 3);
- //leo las variables globales / estaticas / singletons de Lua
- std::cout << "[C++] entero_lua = " << getEntero("entero_lua") << std::endl;
- std::cout << "[C++] real_lua = " << getReal("real_lua") << std::endl;
- //cerramos lua
- lua_close(pL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement