1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-21 20:27:13 +01:00

Miscellaneous code cleanup in the MaxmindDB module.

This commit is contained in:
Sandu Liviu Catalin 2016-11-14 14:44:01 +02:00
parent b9bc8ce2ad
commit fb5a5b0090
13 changed files with 214 additions and 105 deletions

View File

@ -46,9 +46,10 @@ class SockAddr;
class EntryData;
class EntryDataList;
class LookupResult;
class SearchNode;
/* ------------------------------------------------------------------------------------------------
* Forward declarations.
* Forward handle declarations.
*/
struct DbHnd;

View File

@ -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)
);

View File

@ -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
{

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------
#include "Description.hpp"
#include "Database.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
@ -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)
);
}

View File

@ -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();
}
/* --------------------------------------------------------------------------------------------

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------
#include "EntryData.hpp"
#include "Database.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
@ -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)
);
}

View File

@ -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.
*/

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------
#include "EntryDataList.hpp"
#include "Database.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
@ -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)

View File

@ -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();
}
/* --------------------------------------------------------------------------------------------

View File

@ -2,6 +2,7 @@
#include "LookupResult.hpp"
#include "EntryData.hpp"
#include "EntryDataList.hpp"
#include "Database.hpp"
// ------------------------------------------------------------------------------------------------
#include <vector>
@ -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)
);

View File

@ -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.
*/

View File

@ -1,6 +1,7 @@
// ------------------------------------------------------------------------------------------------
#include "Metadata.hpp"
#include "Description.hpp"
#include "Database.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
@ -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)

View File

@ -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