From 78de7bdf3c7426e5cbfad481e8d0ea022ef65c02 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Sun, 19 Feb 2023 19:52:07 -0400 Subject: [PATCH] Move ByteData float/int getter methods to Data. --- src/modules/data/wrap_ByteData.cpp | 81 ------------------------------ src/modules/data/wrap_Data.cpp | 81 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/modules/data/wrap_ByteData.cpp b/src/modules/data/wrap_ByteData.cpp index 8ca6f48e2..f34a48d9e 100644 --- a/src/modules/data/wrap_ByteData.cpp +++ b/src/modules/data/wrap_ByteData.cpp @@ -76,27 +76,6 @@ int w_ByteData_setT(lua_State *L) return 0; } -template -int w_ByteData_getT(lua_State *L) -{ - ByteData *t = luax_checkbytedata(L, 1); - int64 offset = (int64) luaL_checknumber(L, 2); - int count = (int) luaL_optinteger(L, 3, 1); - - if (count <= 0) - return luaL_error(L, "Invalid count parameter (must be greater than 0)"); - - if (offset < 0 || offset + sizeof(T) * count > t->getSize()) - return luaL_error(L, "The given offset and count parameters don't fit within the ByteData's size."); - - auto data = (const T *)((uint8 *) t->getData() + offset); - - for (int i = 0; i < count; i++) - lua_pushnumber(L, (lua_Number) data[i]); - - return count; -} - int w_ByteData_setFloat(lua_State *L) { return w_ByteData_setT(L); @@ -147,56 +126,6 @@ int w_ByteData_setUInt64(lua_State *L) return w_ByteData_setT(L); } -int w_ByteData_getFloat(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getDouble(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getInt8(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getUInt8(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getInt16(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getUInt16(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getInt32(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getUInt32(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getInt64(lua_State *L) -{ - return w_ByteData_getT(L); -} - -int w_ByteData_getUInt64(lua_State *L) -{ - return w_ByteData_getT(L); -} - static const luaL_Reg w_ByteData_functions[] = { { "clone", w_ByteData_clone }, @@ -210,16 +139,6 @@ static const luaL_Reg w_ByteData_functions[] = { "setUInt32", w_ByteData_setUInt32 }, { "setInt64", w_ByteData_setInt64 }, { "setUInt64", w_ByteData_setUInt64 }, - { "getFloat", w_ByteData_getFloat }, - { "getDouble", w_ByteData_getDouble }, - { "getInt8", w_ByteData_getInt8 }, - { "getUInt8", w_ByteData_getUInt8 }, - { "getInt16", w_ByteData_getInt16 }, - { "getUInt16", w_ByteData_getUInt16 }, - { "getInt32", w_ByteData_getInt32 }, - { "getUInt32", w_ByteData_getUInt32 }, - { "getInt64", w_ByteData_getInt64 }, - { "getUInt64", w_ByteData_getUInt64 }, { 0, 0 } }; diff --git a/src/modules/data/wrap_Data.cpp b/src/modules/data/wrap_Data.cpp index 48a6570a4..ceab57242 100644 --- a/src/modules/data/wrap_Data.cpp +++ b/src/modules/data/wrap_Data.cpp @@ -78,6 +78,77 @@ int w_Data_getSize(lua_State *L) return 1; } +template +static int w_Data_getT(lua_State* L) +{ + Data* t = luax_checkdata(L, 1); + int64 offset = (int64)luaL_checknumber(L, 2); + int count = (int)luaL_optinteger(L, 3, 1); + + if (count <= 0) + return luaL_error(L, "Invalid count parameter (must be greater than 0)"); + + if (offset < 0 || offset + sizeof(T) * count > t->getSize()) + return luaL_error(L, "The given offset and count parameters don't fit within the Data's size."); + + auto data = (const T*)((uint8*)t->getData() + offset); + + for (int i = 0; i < count; i++) + lua_pushnumber(L, (lua_Number)data[i]); + + return count; +} + +int w_Data_getFloat(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getDouble(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getInt8(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getUInt8(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getInt16(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getUInt16(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getInt32(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getUInt32(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getInt64(lua_State* L) +{ + return w_Data_getT(L); +} + +int w_Data_getUInt64(lua_State* L) +{ + return w_Data_getT(L); +} + // C functions in a struct, necessary for the FFI versions of Data methods. struct FFI_Data { @@ -99,6 +170,16 @@ const luaL_Reg w_Data_functions[] = { "getPointer", w_Data_getPointer }, { "getFFIPointer", w_Data_getFFIPointer }, { "getSize", w_Data_getSize }, + { "getFloat", w_Data_getFloat }, + { "getDouble", w_Data_getDouble }, + { "getInt8", w_Data_getInt8 }, + { "getUInt8", w_Data_getUInt8 }, + { "getInt16", w_Data_getInt16 }, + { "getUInt16", w_Data_getUInt16 }, + { "getInt32", w_Data_getInt32 }, + { "getUInt32", w_Data_getUInt32 }, + { "getInt64", w_Data_getInt64 }, + { "getUInt64", w_Data_getUInt64 }, { 0, 0 } };