[C++] [SFML] [LUA] Funkcja zawarta w klasie nie nadpisuje jej składowej
Ostatnio zmodyfikowano 2015-12-27 19:32
Gibas11 Temat założony przez niniejszego użytkownika |
» 2015-12-27 19:32:18 W takim razie dlaczego wskaźnik z kontruktora po wyświetleniu ma 12 znaków a z funkcji tylko 6? To dużo wyjaśnia, dzięki za link, przyjrzę się. EDIT: Ok, załatwiłem sprawę sam - funkcję wyjąłem z klasy i teraz przyjmuje jako parametr adres obiektu luaVirtualMachine zdefiniowany stałą ID. Kod w lua wygląda tak: a = read(ID) print(a)
I działa. #include <SFML/System.hpp> #include <iostream> #include <cstring> #include <string> #include <atomic>
using namespace std;
extern "C" { #include "lua5.2/lua.h" #include "lua5.2/lualib.h" #include "lua5.2/lauxlib.h" }
int api_read( lua_State * L );
class luaVirtualMachine { private: sf::Thread luaScript; lua_State * state; char * _file; void execute(); bool ready; bool running; atomic < bool > _reguiresData; public: string output; string input; luaVirtualMachine(); luaVirtualMachine( const char * file ); ~luaVirtualMachine(); bool open( const char * file ); bool close(); bool launch(); bool isRunning(); bool reguiresData(); void reguiresData( bool opt ); void putstring( string c ); string getstring(); };
void luaVirtualMachine::execute() { lua_pushinteger( state,( uint64_t ) this ); lua_setglobal( state, "ID" ); luaL_dofile( state, _file ); running = false; }
luaVirtualMachine::luaVirtualMachine() : luaScript( & luaVirtualMachine::execute, this ) { ready = false; running = false; reguiresData( false ); } luaVirtualMachine::luaVirtualMachine( const char * file ) : luaScript( & luaVirtualMachine::execute, this ) { ready = false; open( file ); running = false; reguiresData( false ); } luaVirtualMachine::~luaVirtualMachine() { close(); }
bool luaVirtualMachine::open( const char * file ) { if( ready ) return false; state = luaL_newstate(); luaL_openlibs( state ); lua_register( state, "read",( lua_CFunction ) & api_read ); _file = new char[ strlen( file ) + 1 ]; strcpy( _file, file ); ready = true; return true; } bool luaVirtualMachine::close() { if( !ready ) return false; lua_close( state ); ready = false; delete _file; return true; } bool luaVirtualMachine::launch() { if( !ready ) return false; running = true; luaScript.launch(); return true; }
bool luaVirtualMachine::isRunning() { return running; } bool luaVirtualMachine::reguiresData() { return _reguiresData; } void luaVirtualMachine::reguiresData( bool opt ) { _reguiresData = opt; }
void luaVirtualMachine::putstring( string s ) { input = s; reguiresData( false ); } string luaVirtualMachine::getstring() { return output; }
int main() { luaVirtualMachine lvm1( "example.lua" ); lvm1.launch(); while( lvm1.isRunning() ) { if( lvm1.reguiresData() ) { string str; cin >> str; lvm1.putstring( str ); } } return 0; }
int api_read( lua_State * L ) { luaVirtualMachine * LVM =( luaVirtualMachine * ) lua_tointeger( L, 1 ); LVM->reguiresData( true ); for(;; ) { if( !( LVM->reguiresData() ) ) { lua_pushstring( L, LVM->input.c_str() ); break; } } return 1; }
|
|
1 « 2 » |