From 363e9029367786e78a8e58bc0ecfdfacfcf278c6 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Fri, 22 Sep 2023 17:56:42 -0300 Subject: [PATCH] shop to smart pointer --- .../creatures/npc/shop_functions.cpp | 48 +++++-------------- .../creatures/npc/shop_functions.hpp | 6 +-- 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/src/lua/functions/creatures/npc/shop_functions.cpp b/src/lua/functions/creatures/npc/shop_functions.cpp index 61330ba5a63..66de7afd962 100644 --- a/src/lua/functions/creatures/npc/shop_functions.cpp +++ b/src/lua/functions/creatures/npc/shop_functions.cpp @@ -14,30 +14,15 @@ int ShopFunctions::luaCreateShop(lua_State* L) { // Shop() will create a new shop item - Shop* shop = new Shop(); - if (shop) { - pushUserdata(L, shop); - setMetatable(L, -1, "Shop"); - } else { - lua_pushnil(L); - } + pushUserdata(L, std::make_shared()); + setMetatable(L, -1, "Shop"); return 1; } -int ShopFunctions::luaDeleteShop(lua_State* L) { - // shop:delete() shop:__gc() - Shop** shopPtr = getRawUserdata(L, 1); - if (shopPtr && *shopPtr) { - delete *shopPtr; - *shopPtr = nullptr; - } - return 0; -} - int ShopFunctions::luaShopSetId(lua_State* L) { // shop:setId(id) - Shop* shop = getUserdata(L, 1); - if (shop) { + + if (const auto &shop = getUserdataShared(L, 1)) { if (isNumber(L, 2)) { shop->shopBlock.itemId = getNumber(L, 2); pushBoolean(L, true); @@ -54,7 +39,7 @@ int ShopFunctions::luaShopSetId(lua_State* L) { int ShopFunctions::luaShopSetIdFromName(lua_State* L) { // shop:setIdFromName(name) - Shop* shop = getUserdata(L, 1); + const auto &shop = getUserdataShared(L, 1); if (shop && isString(L, 2)) { auto name = getString(L, 2); auto ids = Item::items.nameToItems.equal_range(asLowerCaseString(name)); @@ -87,8 +72,7 @@ int ShopFunctions::luaShopSetIdFromName(lua_State* L) { int ShopFunctions::luaShopSetNameItem(lua_State* L) { // shop:setNameItem(name) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemName = getString(L, 2); pushBoolean(L, true); } else { @@ -99,8 +83,7 @@ int ShopFunctions::luaShopSetNameItem(lua_State* L) { int ShopFunctions::luaShopSetCount(lua_State* L) { // shop:setCount(count) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemSubType = getNumber(L, 2); pushBoolean(L, true); } else { @@ -111,8 +94,7 @@ int ShopFunctions::luaShopSetCount(lua_State* L) { int ShopFunctions::luaShopSetBuyPrice(lua_State* L) { // shop:setBuyPrice(price) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemBuyPrice = getNumber(L, 2); pushBoolean(L, true); } else { @@ -123,8 +105,7 @@ int ShopFunctions::luaShopSetBuyPrice(lua_State* L) { int ShopFunctions::luaShopSetSellPrice(lua_State* L) { // shop:setSellPrice(chance) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemSellPrice = getNumber(L, 2); pushBoolean(L, true); } else { @@ -135,8 +116,7 @@ int ShopFunctions::luaShopSetSellPrice(lua_State* L) { int ShopFunctions::luaShopSetStorageKey(lua_State* L) { // shop:setStorageKey(storage) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemStorageKey = getNumber(L, 2); pushBoolean(L, true); } else { @@ -147,8 +127,7 @@ int ShopFunctions::luaShopSetStorageKey(lua_State* L) { int ShopFunctions::luaShopSetStorageValue(lua_State* L) { // shop:setStorageValue(value) - Shop* shop = getUserdata(L, 1); - if (shop) { + if (const auto &shop = getUserdataShared(L, 1)) { shop->shopBlock.itemStorageValue = getNumber(L, 2); pushBoolean(L, true); } else { @@ -159,9 +138,8 @@ int ShopFunctions::luaShopSetStorageValue(lua_State* L) { int ShopFunctions::luaShopAddChildShop(lua_State* L) { // shop:addChildShop(shop) - Shop* shop = getUserdata(L, 1); - if (shop) { - shop->shopBlock.childShop.push_back(getUserdata(L, 2)->shopBlock); + if (const auto &shop = getUserdataShared(L, 1)) { + shop->shopBlock.childShop.push_back(getUserdataShared(L, 2)->shopBlock); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/npc/shop_functions.hpp b/src/lua/functions/creatures/npc/shop_functions.hpp index 0ec0cefb5c3..1ffb53c92dc 100644 --- a/src/lua/functions/creatures/npc/shop_functions.hpp +++ b/src/lua/functions/creatures/npc/shop_functions.hpp @@ -14,10 +14,7 @@ class ShopFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Shop", "", ShopFunctions::luaCreateShop); - registerMetaMethod(L, "Shop", "__gc", ShopFunctions::luaDeleteShop); - registerMethod(L, "Shop", "delete", ShopFunctions::luaDeleteShop); - + registerSharedClass(L, "Shop", "", ShopFunctions::luaCreateShop); registerMethod(L, "Shop", "setId", ShopFunctions::luaShopSetId); registerMethod(L, "Shop", "setIdFromName", ShopFunctions::luaShopSetIdFromName); registerMethod(L, "Shop", "setNameItem", ShopFunctions::luaShopSetNameItem); @@ -31,7 +28,6 @@ class ShopFunctions final : LuaScriptInterface { private: static int luaCreateShop(lua_State* L); - static int luaDeleteShop(lua_State* L); static int luaShopSetId(lua_State* L); static int luaShopSetIdFromName(lua_State* L); static int luaShopSetNameItem(lua_State* L);