From b13b5142f1009a71ff06786ac8c8db92891f566a Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 1 Oct 2024 21:03:44 +0200 Subject: Core/Utilities: Extend make_unique_ptr_with_deleter functionality to allow it to create deleters with compile time constant functions (reduces its size to just sizeof(void*)) --- src/common/Collision/Maps/MapTree.cpp | 10 +- src/common/Collision/Models/GameObjectModel.cpp | 2 +- src/common/Cryptography/Ed25519.cpp | 6 +- src/common/Cryptography/RSA.cpp | 8 +- .../Debugging/Windows/WheatyExceptionReport.cpp | 57 ++++---- src/common/Utilities/Memory.h | 143 +++++++++++++++++++-- src/common/Utilities/StartProcess.cpp | 2 +- src/server/bnetserver/Main.cpp | 12 +- src/server/bnetserver/Server/SslContext.cpp | 6 +- src/server/game/AuctionHouse/AuctionHouseMgr.cpp | 11 +- src/server/game/Maps/TerrainMgr.cpp | 4 +- src/server/worldserver/Main.cpp | 24 ++-- src/tools/mmaps_generator/MapBuilder.cpp | 2 +- src/tools/vmap4_assembler/TileAssembler.cpp | 2 +- src/tools/vmap4_extractor/adtfile.cpp | 4 +- src/tools/vmap4_extractor/wdtfile.cpp | 2 +- 16 files changed, 207 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/common/Collision/Maps/MapTree.cpp b/src/common/Collision/Maps/MapTree.cpp index bde07b30065..d828326806c 100644 --- a/src/common/Collision/Maps/MapTree.cpp +++ b/src/common/Collision/Maps/MapTree.cpp @@ -196,11 +196,11 @@ namespace VMAP struct TileFileOpenResult { - using FileHandle = decltype(Trinity::make_unique_ptr_with_deleter(nullptr, &::fclose)); + using FileDeleter = decltype(Trinity::unique_ptr_deleter()); std::string Name; - FileHandle TileFile = { nullptr, &::fclose }; - FileHandle SpawnIndicesFile = { nullptr, &::fclose }; + std::unique_ptr TileFile; + std::unique_ptr SpawnIndicesFile; int32 UsedMapId; explicit operator bool() const { return TileFile && SpawnIndicesFile; } @@ -239,7 +239,7 @@ namespace VMAP basePath.push_back('/'); std::string fullname = basePath + VMapManager2::getMapFileName(mapID); - auto rf = Trinity::make_unique_ptr_with_deleter(fopen(fullname.c_str(), "rb"), &::fclose); + auto rf = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(fullname.c_str(), "rb")); if (!rf) return LoadResult::FileNotFound; @@ -263,7 +263,7 @@ namespace VMAP { TC_LOG_DEBUG("maps", "StaticMapTree::InitMap() : initializing StaticMapTree '{}'", fname); std::string fullname = iBasePath + fname; - auto rf = Trinity::make_unique_ptr_with_deleter(fopen(fullname.c_str(), "rb"), &::fclose); + auto rf = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(fullname.c_str(), "rb")); if (!rf) return LoadResult::FileNotFound; diff --git a/src/common/Collision/Models/GameObjectModel.cpp b/src/common/Collision/Models/GameObjectModel.cpp index b32a5bdd44f..9fcf1b85e20 100644 --- a/src/common/Collision/Models/GameObjectModel.cpp +++ b/src/common/Collision/Models/GameObjectModel.cpp @@ -46,7 +46,7 @@ bool LoadGameObjectModelList(std::string const& dataPath) { uint32 oldMSTime = getMSTime(); - auto model_list_file = Trinity::make_unique_ptr_with_deleter(fopen((dataPath + "vmaps/" + VMAP::GAMEOBJECT_MODELS).c_str(), "rb"), &::fclose); + auto model_list_file = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen((dataPath + "vmaps/" + VMAP::GAMEOBJECT_MODELS).c_str(), "rb")); if (!model_list_file) { TC_LOG_ERROR("misc", "Unable to open '{}' file.", VMAP::GAMEOBJECT_MODELS); diff --git a/src/common/Cryptography/Ed25519.cpp b/src/common/Cryptography/Ed25519.cpp index 6f286c74b3e..febd8237c57 100644 --- a/src/common/Cryptography/Ed25519.cpp +++ b/src/common/Cryptography/Ed25519.cpp @@ -68,7 +68,7 @@ bool Ed25519::LoadFromFile(std::string const& fileName) _key = nullptr; } - auto keyBIO = make_unique_ptr_with_deleter(BIO_new_file(fileName.c_str(), "r"), &BIO_free); + auto keyBIO = make_unique_ptr_with_deleter<&BIO_free>(BIO_new_file(fileName.c_str(), "r")); if (!keyBIO) return false; @@ -87,9 +87,9 @@ bool Ed25519::LoadFromString(std::string const& keyPem) _key = nullptr; } - auto keyBIO = make_unique_ptr_with_deleter(BIO_new_mem_buf( + auto keyBIO = make_unique_ptr_with_deleter<&BIO_free>(BIO_new_mem_buf( const_cast(keyPem.c_str()) /*api hack - this function assumes memory is readonly but lacks const modifier*/, - keyPem.length() + 1), &BIO_free); + keyPem.length() + 1)); if (!keyBIO) return false; diff --git a/src/common/Cryptography/RSA.cpp b/src/common/Cryptography/RSA.cpp index 106eed27374..27a0f750fb0 100644 --- a/src/common/Cryptography/RSA.cpp +++ b/src/common/Cryptography/RSA.cpp @@ -297,7 +297,7 @@ bool RsaSignature::LoadKeyFromFile(std::string const& fileName) _key = nullptr; } - auto keyBIO = make_unique_ptr_with_deleter(BIO_new_file(fileName.c_str(), "r"), &BIO_free); + auto keyBIO = make_unique_ptr_with_deleter<&BIO_free>(BIO_new_file(fileName.c_str(), "r")); if (!keyBIO) return false; @@ -316,9 +316,9 @@ bool RsaSignature::LoadKeyFromString(std::string const& keyPem) _key = nullptr; } - auto keyBIO = make_unique_ptr_with_deleter(BIO_new_mem_buf( + auto keyBIO = make_unique_ptr_with_deleter<&BIO_free>(BIO_new_mem_buf( const_cast(keyPem.c_str()) /*api hack - this function assumes memory is readonly but lacks const modifier*/, - keyPem.length() + 1), &BIO_free); + keyPem.length() + 1)); if (!keyBIO) return false; @@ -333,7 +333,7 @@ bool RsaSignature::Sign(uint8 const* message, std::size_t messageLength, DigestG { std::unique_ptr digestGenerator = generator.GetGenerator(); - auto keyCtx = make_unique_ptr_with_deleter(EVP_PKEY_CTX_new_from_pkey(generator.GetLib(), _key, nullptr), &EVP_PKEY_CTX_free); + auto keyCtx = make_unique_ptr_with_deleter<&EVP_PKEY_CTX_free>(EVP_PKEY_CTX_new_from_pkey(generator.GetLib(), _key, nullptr)); EVP_MD_CTX_set_pkey_ctx(_ctx, keyCtx.get()); std::unique_ptr params = generator.GetParams(); diff --git a/src/common/Debugging/Windows/WheatyExceptionReport.cpp b/src/common/Debugging/Windows/WheatyExceptionReport.cpp index 5cb29c56aae..8bdce95aee5 100644 --- a/src/common/Debugging/Windows/WheatyExceptionReport.cpp +++ b/src/common/Debugging/Windows/WheatyExceptionReport.cpp @@ -6,6 +6,7 @@ #include "WheatyExceptionReport.h" #include "Errors.h" #include "GitRevision.h" +#include "Memory.h" #include #include #include @@ -411,6 +412,12 @@ BOOL WheatyExceptionReport::_GetWindowsVersion(TCHAR* szVersion, DWORD cntMax) return TRUE; } +template T> +using com_unique_ptr_deleter = decltype(Trinity::unique_ptr_deleterRelease(); }>()); + +template T> +using com_unique_ptr = std::unique_ptr>; + BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cntMax) { // Step 1: -------------------------------------------------- @@ -419,10 +426,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn if (FAILED(hres)) return FALSE; - std::shared_ptr com(nullptr, [](void*) - { - CoUninitialize(); - }); + auto com = Trinity::make_unique_ptr_with_deleter<[](void*) { CoUninitialize(); }>(&hres); // Step 2: -------------------------------------------------- // Set general COM security levels -------------------------- @@ -443,7 +447,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- - std::shared_ptr loc = []() -> std::shared_ptr + com_unique_ptr loc([] { IWbemLocator* tmp = nullptr; HRESULT hres = CoCreateInstance( @@ -453,11 +457,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn IID_IWbemLocator, reinterpret_cast(&tmp)); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemLocator* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!loc) return FALSE; @@ -466,7 +467,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. - std::shared_ptr svc = [loc]() ->std::shared_ptr + com_unique_ptr svc([&] { IWbemServices* tmp = nullptr; HRESULT hres = loc->ConnectServer( @@ -480,11 +481,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn &tmp // pointer to IWbemServices proxy ); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemServices* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!svc) return FALSE; @@ -509,7 +507,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Use the IWbemServices pointer to make requests of WMI ---- // For example, get the name of the operating system - std::shared_ptr queryResult = [svc]() -> std::shared_ptr + com_unique_ptr queryResult([&] { IEnumWbemClassObject* tmp = nullptr; HRESULT hres = svc->ExecQuery( @@ -519,11 +517,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn nullptr, &tmp); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IEnumWbemClassObject* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); BOOL result = FALSE; // Step 7: ------------------------------------------------- @@ -532,11 +527,17 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn { do { - IWbemClassObject* fields = nullptr; - - ULONG rows = 0; - queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); - if (!rows) + auto [fields, rows] = [&] + { + IWbemClassObject* fields = nullptr; + ULONG rows = 0; + HRESULT hres = queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); + return SUCCEEDED(hres) && rows + ? std::pair(com_unique_ptr(fields), rows) + : std::pair(com_unique_ptr(), ULONG(0)); + }(); + + if (!fields || !rows) break; VARIANT field; @@ -558,8 +559,6 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn } VariantClear(&field); - fields->Release(); - result = TRUE; } while (true); } diff --git a/src/common/Utilities/Memory.h b/src/common/Utilities/Memory.h index 98622166b22..629099bf3e3 100644 --- a/src/common/Utilities/Memory.h +++ b/src/common/Utilities/Memory.h @@ -18,32 +18,157 @@ #ifndef TRINITY_MEMORY_H #define TRINITY_MEMORY_H +#include "CompilerDefs.h" +#include #include +#if TRINITY_COMPILER == TRINITY_COMPILER_GNU +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wignored-attributes" +#endif + namespace Trinity { namespace Impl { template -struct unique_ptr_deleter +struct stateful_unique_ptr_deleter { - using pointer = T*; - unique_ptr_deleter(Del deleter) : _deleter(std::move(deleter)) { } - - void operator()(pointer ptr) const { _deleter(ptr); } + using pointer = T; + explicit(false) stateful_unique_ptr_deleter(Del deleter) : _deleter(std::move(deleter)) { } + void operator()(pointer ptr) const { (void)_deleter(ptr); } private: Del _deleter; }; + +template +struct stateless_unique_ptr_deleter +{ + using pointer = T; + void operator()(pointer ptr) const + { + if constexpr (std::is_member_function_pointer_v) + (void)(ptr->*Del)(); + else + (void)Del(ptr); + } +}; } -template -auto make_unique_ptr_with_deleter(T* ptr, Del&& deleter) +/** + * Convenience function to construct type aliases for std::unique_ptr stateful deleters (such as lambda with captures) + * @tparam Ptr Type of the pointer + * @tparam Del Type of object freeing function (deduced from argument) + * @param deleter Object deleter + * + * Example use + * @code + * void FreeV1(Resource*); + * void FreeV2(Resource*); + * + * using ResourceDeleter = decltype(Trinity::unique_ptr_deleter(&FreeV1)); + * + * std::unique_ptr resource = Trinity::make_unique_ptr_with_deleter(GetResourceV1(), &FreeV1); + * // do stuff ... + * // ... later get new resource + * resource = Trinity::make_unique_ptr_with_deleter(GetResourceV2(), &FreeV2); + * @endcode + */ +template requires std::invocable && std::is_pointer_v +Impl::stateful_unique_ptr_deleter unique_ptr_deleter(Del deleter) +{ + return Impl::stateful_unique_ptr_deleter(std::move(deleter)); +} + +/** + * Convenience function to construct type aliases for std::unique_ptr stateful deleters (such as lambda with captures) + * + * Main use is for forming struct/class members without the call to make_unique_ptr_with_deleter + * @tparam Ptr Type of the pointer + * @tparam Del The freeing function. This can be either a free function pointer that accepts T* as argument, pointer to a member function of T that accepts no arguments or a lambda with no captures + * + * Example use + * @code + * using FileDeleter = decltype(Trinity::unique_ptr_deleter()); + * + * class Resource + * { + * std::unique_ptr File; + * }; + * @endcode + */ +template requires std::invocable && std::is_pointer_v +Impl::stateless_unique_ptr_deleter unique_ptr_deleter() +{ + return Impl::stateless_unique_ptr_deleter(); +} + +/** + * Utility function to construct a std::unique_ptr object with custom stateful deleter (such as lambda with captures) + * @tparam Ptr Type of the pointer + * @tparam T Type of the pointed-to object (defaults to std::remove_pointer_t) + * @tparam Del Type of object freeing function (deduced from second argument) + * @param ptr Raw pointer to owned object + * @param deleter Object deleter + * + * Example use + * @code + * class Resource + * { + * }; + * class ResourceService + * { + * Resource* Create(); + * void Destroy(Resource*); + * }; + * + * ResourceService* service = GetResourceService(); + * + * // Lambda that captures all required variables for destruction + * auto resource = Trinity::make_unique_ptr_with_deleter(service->Create(), [service](Resource* res){ service->Destroy(res); }); + * @endcode + */ +template, typename Del> requires std::invocable && std::is_pointer_v +std::unique_ptr> make_unique_ptr_with_deleter(Ptr ptr, Del deleter) { - using Deleter_t = Impl::unique_ptr_deleter; + return std::unique_ptr>(ptr, std::move(deleter)); +} - return std::unique_ptr(ptr, Deleter_t(std::forward(deleter))); +/** + * Utility function to construct a std::unique_ptr object with custom stateless deleter (function pointer, captureless lambda) + * @tparam Del The freeing function. This can be either a free function pointer that accepts T* as argument, pointer to a member function of T that accepts no arguments or a lambda with no captures + * @tparam Ptr Type of the pointer + * @tparam T Type of the pointed-to object (defaults to std::remove_pointer_t) + * @param ptr Raw pointer to owned object + * + * Example uses + * @code + * void DestroyResource(Resource*); + * class Resource + * { + * void Destroy(); + * }; + * + * // Free function + * auto free = Trinity::make_unique_ptr_with_deleter<&DestroyResource>(new Resource()); + * + * // Member function + * auto member = Trinity::make_unique_ptr_with_deleter<&Resource::Destroy>(new Resource()); + * + * // Lambda + * auto lambda = Trinity::make_unique_ptr_with_deleter<[](Resource* res){ res->Destroy(); }>(new Resource()); + * @endcode + */ +template> requires std::invocable && std::is_pointer_v +std::unique_ptr> make_unique_ptr_with_deleter(Ptr ptr) +{ + return std::unique_ptr>(ptr); } } +#if TRINITY_COMPILER == TRINITY_COMPILER_GNU +#pragma GCC diagnostic pop +#endif + #endif // TRINITY_MEMORY_H diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp index d0fe3509ae7..9207a3e7a48 100644 --- a/src/common/Utilities/StartProcess.cpp +++ b/src/common/Utilities/StartProcess.cpp @@ -101,7 +101,7 @@ public: } // prepare file with only read permission (boost process opens with read_write) - auto inputFile = Trinity::make_unique_ptr_with_deleter(!input_file.empty() ? fopen(input_file.c_str(), "rb") : nullptr, &::fclose); + auto inputFile = Trinity::make_unique_ptr_with_deleter<&::fclose>(!input_file.empty() ? fopen(input_file.c_str(), "rb") : nullptr); std::error_code ec; diff --git a/src/server/bnetserver/Main.cpp b/src/server/bnetserver/Main.cpp index cc28891ecef..bc838abb5b4 100644 --- a/src/server/bnetserver/Main.cpp +++ b/src/server/bnetserver/Main.cpp @@ -108,7 +108,7 @@ int main(int argc, char** argv) GOOGLE_PROTOBUF_VERIFY_VERSION; - auto protobufHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { google::protobuf::ShutdownProtobufLibrary(); }); + auto protobufHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { google::protobuf::ShutdownProtobufLibrary(); }>(&dummy); #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS Trinity::Service::Init(serviceLongName, serviceName, serviceDescription, &main, &m_ServiceStatus); @@ -166,7 +166,7 @@ int main(int argc, char** argv) OpenSSLCrypto::threadsSetup(boost::dll::program_location().remove_filename()); - auto opensslHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { OpenSSLCrypto::threadsCleanup(); }); + auto opensslHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { OpenSSLCrypto::threadsCleanup(); }>(&dummy); // bnetserver PID file creation std::string pidFile = sConfigMgr->GetStringDefault("PidFile", ""); @@ -191,7 +191,7 @@ int main(int argc, char** argv) if (!StartDB()) return 1; - auto dbHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { StopDB(); }); + auto dbHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { StopDB(); }>(&dummy); if (vm.count("update-databases-only")) return 0; @@ -219,7 +219,7 @@ int main(int argc, char** argv) return 1; } - auto sLoginServiceHandle = Trinity::make_unique_ptr_with_deleter(&sLoginService, [](Battlenet::LoginRESTService* service) { service->StopNetwork(); }); + auto sLoginServiceHandle = Trinity::make_unique_ptr_with_deleter<&Battlenet::LoginRESTService::StopNetwork>(&sLoginService); // Start the listening port (acceptor) for auth connections int32 bnport = sConfigMgr->GetIntDefault("BattlenetPort", 1119); @@ -232,7 +232,7 @@ int main(int argc, char** argv) // Get the list of realms for the server sRealmList->Initialize(*ioContext, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 10)); - auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter(sRealmList, [](RealmList* realmList) { realmList->Close(); }); + auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter<&RealmList::Close>(sRealmList); std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0"); @@ -242,7 +242,7 @@ int main(int argc, char** argv) return 1; } - auto sSessionMgrHandle = Trinity::make_unique_ptr_with_deleter(&sSessionMgr, [](Battlenet::SessionManager* sessMgr) { sessMgr->StopNetwork(); }); + auto sSessionMgrHandle = Trinity::make_unique_ptr_with_deleter<&Battlenet::SessionManager::StopNetwork>(&sSessionMgr); // Set signal handlers boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM); diff --git a/src/server/bnetserver/Server/SslContext.cpp b/src/server/bnetserver/Server/SslContext.cpp index e9192475a62..7920238c16e 100644 --- a/src/server/bnetserver/Server/SslContext.cpp +++ b/src/server/bnetserver/Server/SslContext.cpp @@ -30,7 +30,7 @@ namespace { auto CreatePasswordUiMethodFromPemCallback(::pem_password_cb* callback) { - return Trinity::make_unique_ptr_with_deleter(UI_UTIL_wrap_read_pem_callback(callback, 0), &::UI_destroy_method); + return Trinity::make_unique_ptr_with_deleter<&::UI_destroy_method>(UI_UTIL_wrap_read_pem_callback(callback, 0)); } auto OpenOpenSSLStore(boost::filesystem::path const& storePath, UI_METHOD const* passwordCallback, void* passwordCallbackData) @@ -45,7 +45,7 @@ auto OpenOpenSSLStore(boost::filesystem::path const& storePath, UI_METHOD const* uri += genericPath; - return Trinity::make_unique_ptr_with_deleter(OSSL_STORE_open(uri.c_str(), passwordCallback, passwordCallbackData, nullptr, nullptr), &::OSSL_STORE_close); + return Trinity::make_unique_ptr_with_deleter<&::OSSL_STORE_close>(OSSL_STORE_open(uri.c_str(), passwordCallback, passwordCallbackData, nullptr, nullptr)); } boost::system::error_code GetLastOpenSSLError() @@ -136,7 +136,7 @@ bool Battlenet::SslContext::Initialize() unsigned char* utf8TextRaw = nullptr; if (int utf8Length = ASN1_STRING_to_UTF8(&utf8TextRaw, text); utf8Length >= 0) { - auto utf8Text = Trinity::make_unique_ptr_with_deleter(utf8TextRaw, [](unsigned char* ptr) { ::OPENSSL_free(ptr); }); + auto utf8Text = Trinity::make_unique_ptr_with_deleter<[](unsigned char* ptr) { ::OPENSSL_free(ptr); } >(utf8TextRaw); if (std::string_view(reinterpret_cast(utf8Text.get()), utf8Length) == "*.*") return true; } diff --git a/src/server/game/AuctionHouse/AuctionHouseMgr.cpp b/src/server/game/AuctionHouse/AuctionHouseMgr.cpp index 0cf8f1f9b33..09de6c3b0ef 100644 --- a/src/server/game/AuctionHouse/AuctionHouseMgr.cpp +++ b/src/server/game/AuctionHouse/AuctionHouseMgr.cpp @@ -1532,18 +1532,13 @@ bool AuctionHouseObject::BuyCommodity(CharacterDatabaseTransaction trans, Player return false; } - auto quote = _commodityQuotes.find(player->GetGUID()); - if (quote == _commodityQuotes.end()) + auto quote = _commodityQuotes.extract(player->GetGUID()); + if (!quote) { player->GetSession()->SendAuctionCommandResult(0, AuctionCommand::PlaceBid, AuctionResult::CommodityPurchaseFailed, delayForNextAction); return false; } - std::shared_ptr removeQuote(nullptr, [this, quote](std::nullptr_t) - { - _commodityQuotes.erase(quote); - }); - uint64 totalPrice = 0; uint32 remainingQuantity = quantity; std::vector auctions; @@ -1575,7 +1570,7 @@ bool AuctionHouseObject::BuyCommodity(CharacterDatabaseTransaction trans, Player // something was bought between creating quote and finalizing transaction // but we allow lower price if new items were posted at lower price - if (totalPrice > quote->second.TotalPrice) + if (totalPrice > quote.mapped().TotalPrice) { player->GetSession()->SendAuctionCommandResult(0, AuctionCommand::PlaceBid, AuctionResult::CommodityPurchaseFailed, delayForNextAction); return false; diff --git a/src/server/game/Maps/TerrainMgr.cpp b/src/server/game/Maps/TerrainMgr.cpp index b2faa91a944..a18373dc248 100644 --- a/src/server/game/Maps/TerrainMgr.cpp +++ b/src/server/game/Maps/TerrainMgr.cpp @@ -51,7 +51,7 @@ void TerrainInfo::DiscoverGridMapFiles() { std::string tileListName = Trinity::StringFormat("{}maps/{:04}.tilelist", sWorld->GetDataPath(), GetId()); // tile list is optional - if (auto tileList = Trinity::make_unique_ptr_with_deleter(fopen(tileListName.c_str(), "rb"), &::fclose)) + if (auto tileList = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(tileListName.c_str(), "rb"))) { u_map_magic mapMagic = { }; uint32 versionMagic = { }; @@ -79,7 +79,7 @@ bool TerrainInfo::ExistMap(uint32 mapid, int32 gx, int32 gy, bool log /*= true*/ std::string fileName = Trinity::StringFormat("{}maps/{:04}_{:02}_{:02}.map", sWorld->GetDataPath(), mapid, gx, gy); bool ret = false; - auto file = Trinity::make_unique_ptr_with_deleter(fopen(fileName.c_str(), "rb"), &::fclose); + auto file = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(fileName.c_str(), "rb")); if (!file) { if (log) diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp index 5f846d79f59..870eb3d5b55 100644 --- a/src/server/worldserver/Main.cpp +++ b/src/server/worldserver/Main.cpp @@ -153,7 +153,7 @@ int main(int argc, char** argv) GOOGLE_PROTOBUF_VERIFY_VERSION; - auto protobufHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { google::protobuf::ShutdownProtobufLibrary(); }); + auto protobufHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { google::protobuf::ShutdownProtobufLibrary(); }>(&dummy); #ifdef _WIN32 Trinity::Service::Init(serviceLongName, serviceName, serviceDescription, &main, &m_ServiceStatus); @@ -252,7 +252,7 @@ int main(int argc, char** argv) OpenSSLCrypto::threadsSetup(boost::dll::program_location().remove_filename()); - auto opensslHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { OpenSSLCrypto::threadsCleanup(); }); + auto opensslHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { OpenSSLCrypto::threadsCleanup(); }>(&dummy); // Seed the OpenSSL's PRNG here. // That way it won't auto-seed when calling BigNumber::SetRand and slow down the first world login @@ -289,7 +289,7 @@ int main(int argc, char** argv) for (int i = 0; i < numThreads; ++i) threadPool->PostWork([ioContext]() { ioContext->run(); }); - auto ioContextStopHandle = Trinity::make_unique_ptr_with_deleter(ioContext.get(), [](Trinity::Asio::IoContext* ctx) { ctx->stop(); }); + auto ioContextStopHandle = Trinity::make_unique_ptr_with_deleter<&Trinity::Asio::IoContext::stop>(ioContext.get()); // Set process priority according to configuration settings SetProcessPriority("server.worldserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false)); @@ -298,7 +298,7 @@ int main(int argc, char** argv) if (!StartDB()) return 1; - auto dbHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { StopDB(); }); + auto dbHandle = Trinity::make_unique_ptr_with_deleter<[](void*) { StopDB(); }>(&dummy); if (vm.count("update-databases-only")) return 0; @@ -307,7 +307,7 @@ int main(int argc, char** argv) sRealmList->Initialize(*ioContext, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 10)); - auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter(sRealmList, [](RealmList* realmList) { realmList->Close(); }); + auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter<&RealmList::Close>(sRealmList); ///- Get the realm Id from the configuration file uint32 realmId = sConfigMgr->GetIntDefault("RealmID", 0); @@ -349,27 +349,27 @@ int main(int argc, char** argv) metric->Unload(); }); - auto scriptReloadMgrHandle = Trinity::make_unique_ptr_with_deleter(sScriptReloadMgr, [](ScriptReloadMgr* mgr) { mgr->Unload(); }); + auto scriptReloadMgrHandle = Trinity::make_unique_ptr_with_deleter<&ScriptReloadMgr::Unload>(sScriptReloadMgr); sScriptMgr->SetScriptLoader(AddScripts); - auto sScriptMgrHandle = Trinity::make_unique_ptr_with_deleter(sScriptMgr, [](ScriptMgr* mgr) { mgr->Unload(); }); + auto sScriptMgrHandle = Trinity::make_unique_ptr_with_deleter<&ScriptMgr::Unload>(sScriptMgr); // Initialize the World sSecretMgr->Initialize(SECRET_OWNER_WORLDSERVER); if (!sWorld->SetInitialWorldSettings()) return 1; - auto instanceLockMgrHandle = Trinity::make_unique_ptr_with_deleter(&sInstanceLockMgr, [](InstanceLockMgr* mgr) { mgr->Unload(); }); + auto instanceLockMgrHandle = Trinity::make_unique_ptr_with_deleter<&InstanceLockMgr::Unload>(&sInstanceLockMgr); - auto terrainMgrHandle = Trinity::make_unique_ptr_with_deleter(&sTerrainMgr, [](TerrainMgr* mgr) { mgr->UnloadAll(); }); + auto terrainMgrHandle = Trinity::make_unique_ptr_with_deleter<&TerrainMgr::UnloadAll>(&sTerrainMgr); - auto outdoorPvpMgrHandle = Trinity::make_unique_ptr_with_deleter(sOutdoorPvPMgr, [](OutdoorPvPMgr* mgr) { mgr->Die(); }); + auto outdoorPvpMgrHandle = Trinity::make_unique_ptr_with_deleter<&OutdoorPvPMgr::Die>(sOutdoorPvPMgr); // unload all grids (including locked in memory) - auto mapManagementHandle = Trinity::make_unique_ptr_with_deleter(sMapMgr, [](MapManager* mgr) { mgr->UnloadAll(); }); + auto mapManagementHandle = Trinity::make_unique_ptr_with_deleter<&MapManager::UnloadAll>(sMapMgr); // unload battleground templates before different singletons destroyed - auto battlegroundMgrHandle = Trinity::make_unique_ptr_with_deleter(sBattlegroundMgr, [](BattlegroundMgr* mgr) { mgr->DeleteAllBattlegrounds(); }); + auto battlegroundMgrHandle = Trinity::make_unique_ptr_with_deleter<&BattlegroundMgr::DeleteAllBattlegrounds>(sBattlegroundMgr); // Start the Remote Access port (acceptor) if enabled std::unique_ptr raAcceptor; diff --git a/src/tools/mmaps_generator/MapBuilder.cpp b/src/tools/mmaps_generator/MapBuilder.cpp index c56da2c8af4..6782078b7c2 100644 --- a/src/tools/mmaps_generator/MapBuilder.cpp +++ b/src/tools/mmaps_generator/MapBuilder.cpp @@ -202,7 +202,7 @@ namespace MMAP if (offMeshFilePath == nullptr) return; - auto fp = Trinity::make_unique_ptr_with_deleter(fopen(offMeshFilePath, "rb"), &::fclose); + auto fp = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(offMeshFilePath, "rb")); if (!fp) { printf(" loadOffMeshConnections:: input file %s not found!\n", offMeshFilePath); diff --git a/src/tools/vmap4_assembler/TileAssembler.cpp b/src/tools/vmap4_assembler/TileAssembler.cpp index 2f96d3d4442..fea6f038ad2 100644 --- a/src/tools/vmap4_assembler/TileAssembler.cpp +++ b/src/tools/vmap4_assembler/TileAssembler.cpp @@ -38,7 +38,7 @@ namespace VMAP { static auto OpenFile(boost::filesystem::path const& p, char const* mode) { - return Trinity::make_unique_ptr_with_deleter(fopen(p.string().c_str(), mode), &::fclose); + return Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(p.string().c_str(), mode)); } G3D::Vector3 ModelPosition::transform(G3D::Vector3 const& pIn) const diff --git a/src/tools/vmap4_extractor/adtfile.cpp b/src/tools/vmap4_extractor/adtfile.cpp index 44b1198bbeb..7c09833d1d8 100644 --- a/src/tools/vmap4_extractor/adtfile.cpp +++ b/src/tools/vmap4_extractor/adtfile.cpp @@ -107,7 +107,7 @@ bool ADTFile::init(uint32 map_num, uint32 originalMapId) uint32 size; std::string dirname = Trinity::StringFormat("{}/dir_bin/{:04}", szWorkDirWmo, map_num); - auto dirfile = Trinity::make_unique_ptr_with_deleter(fopen(dirname.c_str(), "ab"), &::fclose); + auto dirfile = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(dirname.c_str(), "ab")); if(!dirfile) { printf("Can't open dirfile!'%s'\n", dirname.c_str()); @@ -239,7 +239,7 @@ bool ADTFile::initFromCache(uint32 map_num, uint32 originalMapId) return true; std::string dirname = Trinity::StringFormat("{}/dir_bin/{:04}", szWorkDirWmo, map_num); - auto dirfile = Trinity::make_unique_ptr_with_deleter(fopen(dirname.c_str(), "ab"), &::fclose); + auto dirfile = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(dirname.c_str(), "ab")); if (!dirfile) { printf("Can't open dirfile!'%s'\n", dirname.c_str()); diff --git a/src/tools/vmap4_extractor/wdtfile.cpp b/src/tools/vmap4_extractor/wdtfile.cpp index dc7e4a33d17..de806159ba0 100644 --- a/src/tools/vmap4_extractor/wdtfile.cpp +++ b/src/tools/vmap4_extractor/wdtfile.cpp @@ -51,7 +51,7 @@ bool WDTFile::init(uint32 mapId) uint32 size; std::string dirname = Trinity::StringFormat("{}/dir_bin/{:04}", szWorkDirWmo, mapId); - auto dirfile = Trinity::make_unique_ptr_with_deleter(fopen(dirname.c_str(), "ab"), &::fclose); + auto dirfile = Trinity::make_unique_ptr_with_deleter<&::fclose>(fopen(dirname.c_str(), "ab")); if (!dirfile) { printf("Can't open dirfile!'%s'\n", dirname.c_str()); -- cgit v1.2.3