From fb5a5b009056519dcde2762c406c8f23af99825a Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 14 Nov 2016 14:44:01 +0200 Subject: [PATCH] Miscellaneous code cleanup in the MaxmindDB module. --- modules/mmdb/Common.hpp | 3 +- modules/mmdb/Database.cpp | 10 +++---- modules/mmdb/Database.hpp | 27 ++++++++++++++---- modules/mmdb/Description.cpp | 11 ++++++-- modules/mmdb/Description.hpp | 50 +++++++++++++++++++--------------- modules/mmdb/EntryData.cpp | 18 ++++++++++++ modules/mmdb/EntryData.hpp | 36 ++++++++++++------------ modules/mmdb/EntryDataList.cpp | 10 +++++++ modules/mmdb/EntryDataList.hpp | 49 +++++++++++++++++++++------------ modules/mmdb/LookupResult.cpp | 18 ++++++++++++ modules/mmdb/LookupResult.hpp | 34 ++++++++++++----------- modules/mmdb/Metadata.cpp | 10 +++++++ modules/mmdb/Metadata.hpp | 43 ++++++++++++++++------------- 13 files changed, 214 insertions(+), 105 deletions(-) diff --git a/modules/mmdb/Common.hpp b/modules/mmdb/Common.hpp index 4c4ffa82..fdb3f279 100644 --- a/modules/mmdb/Common.hpp +++ b/modules/mmdb/Common.hpp @@ -46,9 +46,10 @@ class SockAddr; class EntryData; class EntryDataList; class LookupResult; +class SearchNode; /* ------------------------------------------------------------------------------------------------ - * Forward declarations. + * Forward handle declarations. */ struct DbHnd; diff --git a/modules/mmdb/Database.cpp b/modules/mmdb/Database.cpp index 9fd09c49..ddd50c4a 100644 --- a/modules/mmdb/Database.cpp +++ b/modules/mmdb/Database.cpp @@ -65,7 +65,7 @@ Object Database::GetMetadataAsEntryDataList() const // Validate the status code if (status != MMDB_SUCCESS) { - STHROWF("Unable to get entry data list [%s]", MMDB_strerror(status)); + STHROWF("Unable to get meta-data entry data list [%s]", MMDB_strerror(status)); } // Return the resulted list return Object(new EntryDataList(m_Handle, entry_data_list)); @@ -116,8 +116,7 @@ LookupResult Database::LookupSockAddr(SockAddr & addr) // Validate the lookup status code if (mmdb_error != MMDB_SUCCESS) { - STHROWF("Unable to lookup address (%s) because [%s]", - addr.GetAddress(), MMDB_strerror(mmdb_error)); + STHROWF("Unable to lookup address (%s) because [%s]", addr.GetAddress(), MMDB_strerror(mmdb_error)); } // Now it's safe to return the lookup result return LookupResult(m_Handle, result); @@ -140,10 +139,11 @@ void Register_Database(Table & mmns) .Prop(_SC("References"), &Database::GetRefCount) .Prop(_SC("Metadata"), &Database::GetMetadata) .Prop(_SC("MetadataAsEntryDataList"), &Database::GetMetadataAsEntryDataList) - // Member Methods + // Member methods + .Func(_SC("Release"), &Database::Release) .Func(_SC("LookupString"), &Database::LookupString) .Func(_SC("LookupSockAddr"), &Database::LookupSockAddr) - // Member Overloads + // Member overloads .Overload< void (Database::*)(CSStr) >(_SC("Open"), &Database::Open) .Overload< void (Database::*)(CSStr, Uint32) >(_SC("Open"), &Database::Open) ); diff --git a/modules/mmdb/Database.hpp b/modules/mmdb/Database.hpp index 991057b4..8b1fe1ca 100644 --- a/modules/mmdb/Database.hpp +++ b/modules/mmdb/Database.hpp @@ -8,7 +8,7 @@ namespace SqMod { /* ------------------------------------------------------------------------------------------------ - * Class that can read/write and alter the contents of INI files. + * Class that can be used to open and query information from MaxMind database files. */ class Database { @@ -37,12 +37,12 @@ private: private: // --------------------------------------------------------------------------------------------- - DbRef m_Handle; /* The main INI document instance. */ + DbRef m_Handle; // The managed database handle. public: /* -------------------------------------------------------------------------------------------- - * Default constructor. + * Default constructor. (null) */ Database() : m_Handle() @@ -51,7 +51,7 @@ public: } /* -------------------------------------------------------------------------------------------- - * Base constructor. + * Base constructor. (default flags) */ Database(CSStr filepath) : m_Handle(new DbHnd(filepath, 0)) @@ -68,6 +68,15 @@ public: /* ... */ } + /* -------------------------------------------------------------------------------------------- + * Explicit handle constructor. + */ + Database(const DbRef & db) + : m_Handle(db) + { + /* ... */ + } + /* -------------------------------------------------------------------------------------------- * Copy constructor. */ @@ -110,7 +119,15 @@ public: } /* -------------------------------------------------------------------------------------------- - * Return the number of active references to this document instance. + * Release the manages handles/pointers and become a null instance. + */ + void Release() + { + m_Handle.Reset(); + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. */ Uint32 GetRefCount() const { diff --git a/modules/mmdb/Description.cpp b/modules/mmdb/Description.cpp index 09865bed..c913b832 100644 --- a/modules/mmdb/Description.cpp +++ b/modules/mmdb/Description.cpp @@ -1,5 +1,6 @@ // ------------------------------------------------------------------------------------------------ #include "Description.hpp" +#include "Database.hpp" // ------------------------------------------------------------------------------------------------ #include @@ -63,8 +64,10 @@ Description::Pointer Description::GetValid() const #endif // _DEBUG // ------------------------------------------------------------------------------------------------ - - +Database Description::GetDatabase() const +{ + return Database(m_Handle); +} // ================================================================================================ void Register_Description(Table & mmns) @@ -79,8 +82,12 @@ void Register_Description(Table & mmns) .Func(_SC("_tostring"), &Description::ToString) // Properties .Prop(_SC("IsValid"), &Description::IsValid) + .Prop(_SC("Database"), &Description::GetDatabase) + .Prop(_SC("References"), &Description::GetRefCount) .Prop(_SC("Value"), &Description::GetDescriptionValue) .Prop(_SC("Language"), &Description::GetDescriptionLanguage) + // Member methods + .Func(_SC("Release"), &Description::Release) ); } diff --git a/modules/mmdb/Description.hpp b/modules/mmdb/Description.hpp index a77bd8ce..d17c749b 100644 --- a/modules/mmdb/Description.hpp +++ b/modules/mmdb/Description.hpp @@ -40,7 +40,7 @@ protected: private: /* -------------------------------------------------------------------------------------------- - * Validate the managed database handle and throw an error if invalid. + * Validate the managed database handle and description pointer and throw an error if invalid. */ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) Pointer GetValid(CCStr file, Int32 line) const; @@ -55,10 +55,10 @@ private: Pointer m_Description; // The inspected meta-data description structure. /* -------------------------------------------------------------------------------------------- - * Construct and with a specific meta-data. + * Construct and with a specific meta-data description. */ - Description(const DbRef & db, Pointer metadata) - : m_Handle(db), m_Description(metadata) + Description(const DbRef & db, Pointer description) + : m_Handle(db), m_Description(description) { /* ... */ } @@ -94,22 +94,6 @@ public: */ Description & operator = (Description &&) = default; - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - Pointer GetHandle() - { - return m_Description; - } - - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - ConstPtr GetHandle() const - { - return m_Description; - } - /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ @@ -124,11 +108,33 @@ public: static SQInteger Typename(HSQUIRRELVM vm); /* -------------------------------------------------------------------------------------------- - * See whether this instance references a valid database and result structure. + * See whether this instance references a valid database and description structure. */ bool IsValid() const { - return m_Handle && m_Description; + return m_Handle; // If there's a database handle then there's a description too + } + + /* -------------------------------------------------------------------------------------------- + * Release the manages handles/pointers and become a null instance. + */ + void Release() + { + m_Handle.Reset(); + m_Description = nullptr; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the database associated with the managed handle/pointer. + */ + Database GetDatabase() const; + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. + */ + Uint32 GetRefCount() const + { + return m_Handle.Count(); } /* -------------------------------------------------------------------------------------------- diff --git a/modules/mmdb/EntryData.cpp b/modules/mmdb/EntryData.cpp index 5d6a33e6..fc8884f2 100644 --- a/modules/mmdb/EntryData.cpp +++ b/modules/mmdb/EntryData.cpp @@ -1,5 +1,6 @@ // ------------------------------------------------------------------------------------------------ #include "EntryData.hpp" +#include "Database.hpp" // ------------------------------------------------------------------------------------------------ #include @@ -57,6 +58,19 @@ EntryData::EntryData() std::memset(&m_Entry, 0, sizeof(Type)); } +// ------------------------------------------------------------------------------------------------ +void EntryData::Release() +{ + std::memset(&m_Entry, 0, sizeof(Type)); + m_Handle.Reset(); +} + +// ------------------------------------------------------------------------------------------------ +Database EntryData::GetDatabase() const +{ + return Database(m_Handle); +} + // ================================================================================================ void Register_EntryData(Table & mmns) { @@ -70,6 +84,8 @@ void Register_EntryData(Table & mmns) .Func(_SC("_tostring"), &EntryData::ToString) // Properties .Prop(_SC("IsValid"), &EntryData::IsValid) + .Prop(_SC("Database"), &EntryData::GetDatabase) + .Prop(_SC("References"), &EntryData::GetRefCount) .Prop(_SC("TypeName"), &EntryData::TypeName) .Prop(_SC("HasData"), &EntryData::HasData) .Prop(_SC("Type"), &EntryData::GetType) @@ -81,6 +97,8 @@ void Register_EntryData(Table & mmns) .Prop(_SC("Long"), &EntryData::GetLong) .Prop(_SC("Bool"), &EntryData::GetBool) .Prop(_SC("Bytes"), &EntryData::GetBytes) + // Member methods + .Func(_SC("Release"), &EntryData::Release) ); } diff --git a/modules/mmdb/EntryData.hpp b/modules/mmdb/EntryData.hpp index a0dcc06a..6bb81e04 100644 --- a/modules/mmdb/EntryData.hpp +++ b/modules/mmdb/EntryData.hpp @@ -90,22 +90,6 @@ public: */ EntryData & operator = (EntryData &&) = default; - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - Reference GetHandle() - { - return m_Entry; - } - - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - ConstRef GetHandle() const - { - return m_Entry; - } - /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ @@ -120,13 +104,31 @@ public: static SQInteger Typename(HSQUIRRELVM vm); /* -------------------------------------------------------------------------------------------- - * See whether this instance references a valid database and result structure. + * See whether this instance references a valid database and entry structure. */ bool IsValid() const { return m_Handle && m_Entry.has_data; } + /* -------------------------------------------------------------------------------------------- + * Release the manages handles/pointers and become a null instance. + */ + void Release(); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the database associated with the managed handle/pointer. + */ + Database GetDatabase() const; + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. + */ + Uint32 GetRefCount() const + { + return m_Handle.Count(); + } + /* -------------------------------------------------------------------------------------------- * Used to retrieve the type of the current element as a string. */ diff --git a/modules/mmdb/EntryDataList.cpp b/modules/mmdb/EntryDataList.cpp index 6e9461e4..71b678bf 100644 --- a/modules/mmdb/EntryDataList.cpp +++ b/modules/mmdb/EntryDataList.cpp @@ -1,5 +1,6 @@ // ------------------------------------------------------------------------------------------------ #include "EntryDataList.hpp" +#include "Database.hpp" // ------------------------------------------------------------------------------------------------ #include @@ -90,6 +91,12 @@ EntryDataList::Pointer EntryDataList::GetValidElem() const } #endif // _DEBUG +// ------------------------------------------------------------------------------------------------ +Database EntryDataList::GetDatabase() const +{ + return Database(m_Handle); +} + // ------------------------------------------------------------------------------------------------ Uint32 EntryDataList::GetCount() const { @@ -175,6 +182,8 @@ void Register_EntryDataList(Table & mmns) .Func(_SC("_tostring"), &EntryDataList::ToString) // Properties .Prop(_SC("IsValid"), &EntryDataList::IsValid) + .Prop(_SC("Database"), &EntryDataList::GetDatabase) + .Prop(_SC("References"), &EntryDataList::GetRefCount) .Prop(_SC("HaveElement"), &EntryDataList::HaveElement) .Prop(_SC("TypeName"), &EntryDataList::TypeName) .Prop(_SC("Count"), &EntryDataList::GetCount) @@ -189,6 +198,7 @@ void Register_EntryDataList(Table & mmns) .Prop(_SC("Bool"), &EntryDataList::GetBool) .Prop(_SC("Bytes"), &EntryDataList::GetBytes) // Member methods + .Func(_SC("Release"), &EntryDataList::Release) .Func(_SC("Next"), &EntryDataList::Next) .Func(_SC("Advance"), &EntryDataList::Advance) .Func(_SC("Reset"), &EntryDataList::Reset) diff --git a/modules/mmdb/EntryDataList.hpp b/modules/mmdb/EntryDataList.hpp index b0f1e1df..082a885a 100644 --- a/modules/mmdb/EntryDataList.hpp +++ b/modules/mmdb/EntryDataList.hpp @@ -135,22 +135,6 @@ public: return *this; } - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - Pointer GetHandle() - { - return m_List; - } - - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - ConstPtr GetHandle() const - { - return m_List; - } - /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ @@ -165,11 +149,40 @@ public: static SQInteger Typename(HSQUIRRELVM vm); /* -------------------------------------------------------------------------------------------- - * See whether this instance references a valid database and result structure. + * See whether this instance references a valid database and element pointer. */ bool IsValid() const { - return m_Handle && m_List; + return m_Handle && m_Elem; + } + + /* -------------------------------------------------------------------------------------------- + * Release the manages handles/pointers and become a null instance. + */ + void Release() + { + m_Handle.Reset(); + // Do we have to free any list? + if (m_List) + { + MMDB_free_entry_data_list(m_List); + } + // Finally, release those as well + m_List = nullptr; + m_Elem = nullptr; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the database associated with the managed handle/pointer. + */ + Database GetDatabase() const; + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. + */ + Uint32 GetRefCount() const + { + return m_Handle.Count(); } /* -------------------------------------------------------------------------------------------- diff --git a/modules/mmdb/LookupResult.cpp b/modules/mmdb/LookupResult.cpp index 62564246..38a2226c 100644 --- a/modules/mmdb/LookupResult.cpp +++ b/modules/mmdb/LookupResult.cpp @@ -2,6 +2,7 @@ #include "LookupResult.hpp" #include "EntryData.hpp" #include "EntryDataList.hpp" +#include "Database.hpp" // ------------------------------------------------------------------------------------------------ #include @@ -60,6 +61,19 @@ LookupResult::LookupResult() std::memset(&m_Result, 0, sizeof(Type)); } +// ------------------------------------------------------------------------------------------------ +void LookupResult::Release() +{ + std::memset(&m_Result, 0, sizeof(Type)); + m_Handle.Reset(); +} + +// ------------------------------------------------------------------------------------------------ +Database LookupResult::GetDatabase() const +{ + return Database(m_Handle); +} + // ------------------------------------------------------------------------------------------------ Object LookupResult::GetEntryDataList() { @@ -166,9 +180,13 @@ void Register_LookupResult(Table & mmns) .Func(_SC("_tostring"), &LookupResult::ToString) // Properties .Prop(_SC("IsValid"), &LookupResult::IsValid) + .Prop(_SC("Database"), &LookupResult::GetDatabase) + .Prop(_SC("References"), &LookupResult::GetRefCount) .Prop(_SC("FoundEntry"), &LookupResult::FoundEntry) .Prop(_SC("NetMask"), &LookupResult::GetNetMask) .Prop(_SC("EntryDataList"), &LookupResult::GetEntryDataList) + // Member methods + .Func(_SC("Release"), &LookupResult::Release) // Squirrel functions .SquirrelFunc(_SC("GetValue"), &LookupResult::GetValue) ); diff --git a/modules/mmdb/LookupResult.hpp b/modules/mmdb/LookupResult.hpp index 8940553b..16099ac3 100644 --- a/modules/mmdb/LookupResult.hpp +++ b/modules/mmdb/LookupResult.hpp @@ -98,22 +98,6 @@ public: */ LookupResult & operator = (LookupResult &&) = default; - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - Reference GetHandle() - { - return m_Result; - } - - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - ConstRef GetHandle() const - { - return m_Result; - } - /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ @@ -135,6 +119,24 @@ public: return m_Handle && m_Result.found_entry; } + /* -------------------------------------------------------------------------------------------- + * Release the manages handles/pointers and become a null instance. + */ + void Release(); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the database associated with the managed handle/pointer. + */ + Database GetDatabase() const; + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. + */ + Uint32 GetRefCount() const + { + return m_Handle.Count(); + } + /* -------------------------------------------------------------------------------------------- * See whether the result contains a valid entry in the associated database. */ diff --git a/modules/mmdb/Metadata.cpp b/modules/mmdb/Metadata.cpp index 93a73228..631efa90 100644 --- a/modules/mmdb/Metadata.cpp +++ b/modules/mmdb/Metadata.cpp @@ -1,6 +1,7 @@ // ------------------------------------------------------------------------------------------------ #include "Metadata.hpp" #include "Description.hpp" +#include "Database.hpp" // ------------------------------------------------------------------------------------------------ #include @@ -63,6 +64,12 @@ Metadata::Pointer Metadata::GetValid() const } #endif // _DEBUG +// ------------------------------------------------------------------------------------------------ +Database Metadata::GetDatabase() const +{ + return Database(m_Handle); +} + // ------------------------------------------------------------------------------------------------ Description Metadata::GetDescriptionHandle(Uint32 idx) const { @@ -88,6 +95,8 @@ void Register_Metadata(Table & mmns) .Func(_SC("_tostring"), &Metadata::ToString) // Properties .Prop(_SC("IsValid"), &Metadata::IsValid) + .Prop(_SC("Database"), &Metadata::GetDatabase) + .Prop(_SC("References"), &Metadata::GetRefCount) .Prop(_SC("NodeCount"), &Metadata::GetNodeCount) .Prop(_SC("RecordSize"), &Metadata::GetRecordSize) .Prop(_SC("IpVersion"), &Metadata::GetIpVersion) @@ -98,6 +107,7 @@ void Register_Metadata(Table & mmns) .Prop(_SC("BuildEpoch"), &Metadata::GetBuildEpoch) .Prop(_SC("DescriptionCount"), &Metadata::GetDescriptionCount) // Member methods + .Func(_SC("Release"), &Metadata::Release) .Func(_SC("GetLanguageName"), &Metadata::GetLanguageName) .Func(_SC("GetDescriptionHandle"), &Metadata::GetDescriptionHandle) .Func(_SC("GetDescriptionValue"), &Metadata::GetDescriptionValue) diff --git a/modules/mmdb/Metadata.hpp b/modules/mmdb/Metadata.hpp index b8728fc2..e102b00f 100644 --- a/modules/mmdb/Metadata.hpp +++ b/modules/mmdb/Metadata.hpp @@ -94,22 +94,6 @@ public: */ Metadata & operator = (Metadata &&) = default; - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - Pointer GetHandle() - { - return m_Metadata; - } - - /* -------------------------------------------------------------------------------------------- - * Retrieve the internal result structure reference. - */ - ConstPtr GetHandle() const - { - return m_Metadata; - } - /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ @@ -124,11 +108,33 @@ public: static SQInteger Typename(HSQUIRRELVM vm); /* -------------------------------------------------------------------------------------------- - * See whether this instance references a valid database and result structure. + * See whether this instance references a valid database and meta-data pointer. */ bool IsValid() const { - return m_Handle && m_Metadata; + return m_Handle; // If there's a database handle then there's a meta-data too + } + + /* -------------------------------------------------------------------------------------------- + * Release the manages handles/pointers and become a null instance. + */ + void Release() + { + m_Handle.Reset(); + m_Metadata = nullptr; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the database associated with the managed handle/pointer. + */ + Database GetDatabase() const; + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to the managed database instance. + */ + Uint32 GetRefCount() const + { + return m_Handle.Count(); } /* -------------------------------------------------------------------------------------------- @@ -254,7 +260,6 @@ public: // Return the requested description language return m_Metadata->description.descriptions[idx]->language; } - }; } // Namespace:: SqMod