Classes that C + + classes turn into LUA calls
The first step: Prepare the C + + classes you need
Examples are as follows:
C + + header file definition
#ifndef __testlua__
#define __testlua__
#include <iostream>
#include <string>
#include <string.h>
using namespace Std;
Class Testlua
{
Private
Static std::string testluatxt;
Public
Static std::string getname ();
static void SetName (string txt);
};
#endif/* Defined (__testlua__) */
C + + CPP file definition
#include "Testlua.h"
String Testlua::getname ()
{
return "Testlua";
}
void Testlua::setname (String txt)
{
Testluatxt =txt;
}
Step two: Prepare the pkg file for compilation
The pkg file is written in the same notation as the C + + header file, except that there are no keywords, as follows:
Class Testlua
{
Static std::string getname ();
static void SetName (std::string value);
};
Step three: Compile the okg file with tolua++
Use the command line to enter the folder where the tolua++ is located, compile with commands, command:./tolua++-O testlua.cpp testlua.pkg. The required testlua.cpp files are generated when the run is complete.
The code in the Testlua.cpp file is as follows:
/*
* * Lua Binding:testlua
* * Generated automatically by tolua++-1.0.92 on Thu Sep 4 14:18:20 2014.
*/
#ifndef __cplusplus
#include "Stdlib.h"
#endif
#include "string.h"
#include "Tolua++.h"
/* Exported function */
Tolua_api int Tolua_testlua_open (lua_state* tolua_s);
/* function to register type */
static void Tolua_reg_types (lua_state* tolua_s)
{
Tolua_usertype (tolua_s, "Testlua");
}
/* Method:getname of Class Testlua */
#ifndef Tolua_disable_tolua_testlua_testlua_ getname00
Static int tolua_testlua_testlua_getname00 (lua_state* tolua_s)
{
#ifndef tolua_release
Tolua _error Tolua_err;
if (
!tolua_isusertype (tolua_s,1, "Testlua", 0,&tolua_err) | |
!tolua_isnoobj (tolua_s,2,&tolua_err)
)
Goto tolua_lerror;
Else
#endif
{
testlua* self = (testlua*) Tolua_tousertype (tolua_s,1,0);
#ifndef tolua_release
if (!self) tolua_error (tolua_s, "Invalid ' self ' in function ' GetName '", NULL);
#endif
{
std::string Tolua_ret = (std::string) self->getname ();
Tolua_pushcppstring (tolua_s, (const char *) Tolua_ret);
}
}
return 1;
#ifndef tolua_release
Tolua_lerror:
Tolua_error (tolua_s, "#ferror in function ' GetName '.", &tolua_err);
return 0;
#endif
}
#endif//#ifndef tolua_disable
/* Method:setname of Class Testlua */
#ifndef Tolua_disable_tolua_testlua_testlua_ setname00
Static int tolua_testlua_testlua_setname00 (lua_state* tolua_s)
{
#ifndef tolua_release
Tolua _error Tolua_err;
if (
!tolua_isusertype (tolua_s,1, "Testlua", 0,&tolua_err) | |
!tolua_iscppstring (tolua_s,2,0,&tolua_err) | |
!tolua_isnoobj (tolua_s,3,&tolua_err)
)
Goto tolua_lerror;
Else
#endif
{
testlua* self = (testlua*) Tolua_tousertype (tolua_s,1,0);
Std::string value = ((std::string) tolua_tocppstring (tolua_s,2,0));
#ifndef tolua_release
if (!self) tolua_error (tolua_s, "Invalid ' self ' in function ' SetName '", NULL);
#endif
{
Self->setname (value);
}
}
return 0;
#ifndef tolua_release
Tolua_lerror:
Tolua_error (tolua_s, "#ferror in function ' SetName '.", &tolua_err);
return 0;
#endif
}
#endif//#ifndef tolua_disable
/* Open function */
Tolua_api int Tolua_testlua_open (lua_state* tolua_s)
{
Tolua_open (tolua_s);
Tolua_reg_types (tolua_s);
Tolua_module (tolua_s,null,0);
Tolua_beginmodule (Tolua_s,null);
Tolua_cclass (tolua_s, "Testlua", "Testlua", "", NULL);
Tolua_beginmodule (tolua_s, "Testlua");
Tolua_function (tolua_s, "GetName", tolua_testlua_testlua_getname00);
Tolua_function (tolua_s, "SetName", tolua_testlua_testlua_setname00);
Tolua_endmodule (tolua_s);
Tolua_endmodule (tolua_s);
return 1;
}
#if defined (lua_version_num) && lua_version_num >= 501
Tolua_api int Luaopen_testlua (lua_state* tolua_s) {
Return Tolua_testlua_open (tolua_s);
};
#endif
The fourth step: the production of good files after processing and put into the project
When adding a testlua.h file, the file contents are as follows:
extern "C"
{
#include "Lua.h"
#include "Tolua++.h"
#include "tolua_fix.h"
}
Tolua_api int Luaopen_testlua (lua_state* tolua_s);
The header file is then introduced in the Testlua.cpp file, which is called by Luaopen_testlua when the project is initialized.
Fifth step: Calling in Lua
This is called in Lua:
If Testlua Then
Local getnametxt = Testlua:getname ()
Print ("Nametxt:"). ToString (Getnametxt): "\ n")
Else
Print ("LUA ERROR: Not acquired")
End
The above is the personal method used in the project, if there is no place to hope the great God, also welcome to study and discuss together. qq:837138108
Share the class library that the C + + class to LUA calls with tolua++