Advertisement
Guest User

@makiolo Luabind test

a guest
Jun 28th, 2011
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. #include <lua.hpp>
  5. #include <luabind/luabind.hpp>
  6.  
  7. typedef std::string String;
  8. typedef float Real;
  9. typedef int Entero;
  10.  
  11. // lo defino estatico, para facilitar la prueba
  12. lua_State* pL;
  13.  
  14. class Vector2
  15. {
  16. public:
  17.     Vector2(Real _x, Real _y)
  18.     {
  19.         x = _x;
  20.         y = _y;
  21.     }
  22.     virtual ~Vector2(){}
  23.    
  24.     inline Real length () const
  25.     {
  26.         return sqrtf( x * x + y * y );
  27.     }
  28.    
  29.     void polimorfico_test()
  30.     {
  31.         std::cout << "[C++] Soy Vector2::polimorfico_test SIN parametros" << std::endl;
  32.     }
  33.    
  34.     void polimorfico_test(Entero var)
  35.     {
  36.         std::cout << "[C++] Soy Vector2::polimorfico_test CON parametros " << var << std::endl;
  37.     }
  38.  
  39. public:
  40.     Real x, y;
  41.  
  42. };
  43.  
  44. // [static]
  45. void hola_mundo_cpp()
  46. {
  47.     std::cout << "[C++] Ejecutando hola_mundo_cpp()" << std::endl;
  48. }
  49.  
  50. // [static]
  51. Entero suma_cpp(Entero a, Entero b)
  52. {
  53.     std::cout << "[C++] Ejecutando suma_cpp(Entero, Entero) con parametros " << a << " y " << b << std::endl;
  54.     return a + b;
  55. }
  56.  
  57. // auxiliares para manejar Lua + Luabind
  58. luabind::object getGlobals()
  59. {
  60.     return luabind::globals(pL);
  61. }
  62.  
  63. Real getReal(String nombre_var)
  64. {
  65.     return luabind::object_cast<Real>(getGlobals()[nombre_var]);
  66. }
  67.  
  68. Entero getEntero(String nombre_var)
  69. {
  70.     return luabind::object_cast<Entero>(getGlobals()[nombre_var]);
  71. }
  72.  
  73. void callFunctionStatic0parms(String nombre)
  74. {
  75.     luabind::call_function<void>(pL, nombre.c_str());
  76. }
  77.  
  78. //////////////////////////////////////////////////////////////////////////////////////////
  79.  
  80. int main()
  81. {
  82.     //create a lua state
  83.     pL = luaL_newstate();
  84.    
  85.     // abre las librerias de lua
  86.     luaL_openlibs(pL);
  87.    
  88.     // abre luabind
  89.     luabind::open(pL);
  90.  
  91.     // ahora bindeamos las clases y funciones
  92.     luabind::module(pL)
  93.     [
  94.         // registramos clases
  95.         // Esta clase no tiene herencia, supongamos que hereda de la clase Tupla, se definiría así:
  96.         //luabind::class_<Vector2, luabind::bases<Tupla> >("Vector2")
  97.         // Con la herencia en Lua -> los beneficios son evidentes. Soporte herencia múltiple, es la polla xD
  98.         luabind::class_<Vector2>("Vector2")
  99.             // constructor
  100.             .def(luabind::constructor<Real, Real>())
  101.             // metodos
  102.             .def("length", &Vector2::length)
  103.             // Para hacer polimorfismo se deben definir las cláusuras a mano
  104.             // si no hay ambigüedad, luabind las saca magicamente
  105.             .def("polimorfico_test", (void (Vector2::*)(Entero))&Vector2::polimorfico_test)
  106.             .def("polimorfico_test", (void (Vector2::*)())&Vector2::polimorfico_test)
  107.             // se puede sobrecarga de operadores ...
  108.             // miembros
  109.             .def_readwrite("x", &Vector2::x)
  110.             .def_readwrite("y", &Vector2::y)
  111.             // si tuvieramos getX / setX y getX y setX se puede hacer esto:
  112.             //.property("x", &Personaje::getX, &Personaje::setX)
  113.             //.property("y", &Personaje::getY, &Personaje::setY)
  114.             // tambien se puede hacer def_readonly para una var protegida.
  115.         ,
  116.         // registramos funciones estaticas
  117.         luabind::def("hola_mundo_cpp", &hola_mundo_cpp),
  118.         luabind::def("suma_cpp", &suma_cpp)
  119.     ];
  120.    
  121.     // parseamos el script. Se puede pasar en texto plano, o compilado en bytecode.
  122.     // se supone que en bytecode carga más rápido, pero es insignificante
  123.     // se suele usar más para ofuscar / proteger código
  124.     if (int error = luaL_dofile(pL, "test.lua") != 0)
  125.     {
  126.         std::cout << "\n[C++]: ERROR(" << error << "): Problem with lua" << "script file!\n\n" << std::endl;
  127.         return 1;
  128.     }
  129.    
  130.     // llamar métodos estaticos definidos en lua
  131.     luabind::call_function<void>(pL, "hola_mundo_lua");
  132.     callFunctionStatic0parms("hola_mundo_lua");
  133.     luabind::call_function<void>(pL, "suma_lua",  1, 3);
  134.    
  135.     //leo las variables globales / estaticas / singletons de Lua
  136.     std::cout << "[C++] entero_lua = " << getEntero("entero_lua") << std::endl;
  137.     std::cout << "[C++] real_lua = " << getReal("real_lua") << std::endl;
  138.    
  139.     //cerramos lua
  140.     lua_close(pL);
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement