From c5701503185d7e1cb8411c7347abdd31eee75c4e Mon Sep 17 00:00:00 2001 From: Headline Date: Tue, 24 May 2022 02:41:15 -0700 Subject: [PATCH] Lift raw pointers out of DatabaseConfBuilder (#1637) * Lift raw pointers out of DatabaseConfBuilder * Maybe: not --- core/logic/Database.cpp | 38 +++++++++++++----------------- core/logic/Database.h | 2 +- core/logic/DatabaseConfBuilder.cpp | 18 +++++++------- core/logic/DatabaseConfBuilder.h | 24 ++++++++----------- 4 files changed, 36 insertions(+), 46 deletions(-) diff --git a/core/logic/Database.cpp b/core/logic/Database.cpp index 5d3601e1..e9a52049 100644 --- a/core/logic/Database.cpp +++ b/core/logic/Database.cpp @@ -128,8 +128,8 @@ void DBManager::OnHandleDestroy(HandleType_t type, void *object) bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength) { - ConfDbInfoList *list = m_Builder.GetConfigList(); - ke::RefPtr pInfo = list->GetDatabaseConf(name); + ConfDbInfoList &list = m_Builder.GetConfigList(); + ke::RefPtr pInfo = list.GetDatabaseConf(name); if (!pInfo) { @@ -148,7 +148,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool /* Try to assign a real driver pointer */ if (pInfo->info.driver[0] == '\0') { - std::string defaultDriver = list->GetDefaultDriver(); + std::string defaultDriver = list.GetDefaultDriver(); if (!m_pDefault && defaultDriver.length() > 0) { m_pDefault = FindOrLoadDriver(defaultDriver.c_str()); @@ -209,17 +209,14 @@ void DBManager::RemoveDriver(IDBDriver *pDriver) } } - ConfDbInfoList *list = m_Builder.GetConfigList(); - for (size_t i = 0; i < list->size(); i++) - { - ke::RefPtr current = list->at(i); - if (current->realDriver == pDriver) + ConfDbInfoList &list = m_Builder.GetConfigList(); + for (auto conf : list) { + if (conf->realDriver == pDriver) { - current->realDriver = NULL; + conf->realDriver = NULL; } } - /* Someone unloaded the default driver? Silly.. */ if (pDriver == m_pDefault) { @@ -255,8 +252,8 @@ void DBManager::RemoveDriver(IDBDriver *pDriver) IDBDriver *DBManager::GetDefaultDriver() { - ConfDbInfoList *list = m_Builder.GetConfigList(); - std::string defaultDriver = list->GetDefaultDriver(); + ConfDbInfoList &list = m_Builder.GetConfigList(); + std::string defaultDriver = list.GetDefaultDriver(); if (!m_pDefault && defaultDriver.length() > 0) { m_pDefault = FindOrLoadDriver(defaultDriver.c_str()); @@ -322,12 +319,12 @@ IDBDriver *DBManager::GetDriver(unsigned int index) const DatabaseInfo *DBManager::FindDatabaseConf(const char *name) { - ConfDbInfoList *list = m_Builder.GetConfigList(); - ke::RefPtr info = list->GetDatabaseConf(name); + ConfDbInfoList &list = m_Builder.GetConfigList(); + ke::RefPtr info = list.GetDatabaseConf(name); if (!info) { // couldn't find requested conf, return default if exists - info = list->GetDefaultConfiguration(); + info = list.GetDefaultConfiguration(); if (!info) { return NULL; @@ -337,11 +334,10 @@ const DatabaseInfo *DBManager::FindDatabaseConf(const char *name) return &info->info; } -ConfDbInfo *DBManager::GetDatabaseConf(const char *name) +ke::RefPtr DBManager::GetDatabaseConf(const char *name) { - ConfDbInfoList *list = m_Builder.GetConfigList(); - ke::RefPtr info(list->GetDatabaseConf(name)); - return info; + ConfDbInfoList &list = m_Builder.GetConfigList(); + return list.GetDatabaseConf(name); } IDBDriver *DBManager::FindOrLoadDriver(const char *name) @@ -582,8 +578,8 @@ void DBManager::OnPluginWillUnload(IPlugin *plugin) std::string DBManager::GetDefaultDriverName() { - ConfDbInfoList *list = m_Builder.GetConfigList(); - return list->GetDefaultDriver(); + ConfDbInfoList &list = m_Builder.GetConfigList(); + return list.GetDefaultDriver(); } void DBManager::AddDependency(IExtension *myself, IDBDriver *driver) diff --git a/core/logic/Database.h b/core/logic/Database.h index 8cb8be5b..cdd06fe0 100644 --- a/core/logic/Database.h +++ b/core/logic/Database.h @@ -71,7 +71,7 @@ public: //IDBManager void AddDriver(IDBDriver *pDrivera); void RemoveDriver(IDBDriver *pDriver); const DatabaseInfo *FindDatabaseConf(const char *name); - ConfDbInfo *GetDatabaseConf(const char *name); + ke::RefPtr GetDatabaseConf(const char *name); bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength); unsigned int GetDriverCount(); IDBDriver *GetDriver(unsigned int index); diff --git a/core/logic/DatabaseConfBuilder.cpp b/core/logic/DatabaseConfBuilder.cpp index c28a5107..81a4aae5 100644 --- a/core/logic/DatabaseConfBuilder.cpp +++ b/core/logic/DatabaseConfBuilder.cpp @@ -36,8 +36,8 @@ #define DBPARSE_LEVEL_DATABASE 2 DatabaseConfBuilder::DatabaseConfBuilder() - : m_ParseList(nullptr), - m_InfoList(new ConfDbInfoList()) + : m_ParseList(), + m_InfoList() { } @@ -50,7 +50,7 @@ DatabaseConfBuilder::~DatabaseConfBuilder() { } -ConfDbInfoList *DatabaseConfBuilder::GetConfigList() +ConfDbInfoList &DatabaseConfBuilder::GetConfigList() { return m_InfoList; } @@ -75,7 +75,7 @@ void DatabaseConfBuilder::ReadSMC_ParseStart() m_ParseLevel = 0; m_ParseState = DBPARSE_LEVEL_NONE; - m_ParseList = new ConfDbInfoList(); + m_ParseList.clear(); } SMCResult DatabaseConfBuilder::ReadSMC_NewSection(const SMCStates *states, const char *name) @@ -116,7 +116,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_KeyValue(const SMCStates *states, const c { if (strcmp(key, "driver_default") == 0) { - m_ParseList->SetDefaultDriver(value); + m_ParseList.SetDefaultDriver(value); } } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) { if (strcmp(key, "driver") == 0) @@ -161,7 +161,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_LeavingSection(const SMCStates *states) /* Save it.. */ m_ParseCurrent->AddRef(); - m_ParseList->push_back(m_ParseCurrent); + m_ParseList.push_back(m_ParseCurrent); m_ParseCurrent = nullptr; /* Go up one level */ @@ -176,9 +176,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_LeavingSection(const SMCStates *states) void DatabaseConfBuilder::ReadSMC_ParseEnd(bool halted, bool failed) { - m_InfoList->ReleaseMembers(); - delete m_InfoList; + m_InfoList.clear(); m_InfoList = m_ParseList; - - m_ParseList = nullptr; + m_ParseList.clear(); } diff --git a/core/logic/DatabaseConfBuilder.h b/core/logic/DatabaseConfBuilder.h index 9d41b926..8c597fb0 100644 --- a/core/logic/DatabaseConfBuilder.h +++ b/core/logic/DatabaseConfBuilder.h @@ -38,6 +38,7 @@ #include #include +#include #include class ConfDbInfo : public ke::RefcountedThreadsafe @@ -56,7 +57,7 @@ public: DatabaseInfo info; }; -class ConfDbInfoList : public std::vector +class ConfDbInfoList : public std::vector> { /* Allow internal usage of ConfDbInfoList */ friend class DBManager; @@ -66,10 +67,10 @@ private: return m_DefDriver; } - ConfDbInfo *GetDatabaseConf(const char *name) { + ke::RefPtr GetDatabaseConf(const char *name) { for (size_t i = 0; i < this->size(); i++) { - ConfDbInfo *current = this->at(i); + ke::RefPtr current = this->at(i); /* If we run into the default configuration, then we'll save it * for the next call to GetDefaultConfiguration */ if (strcmp(current->name.c_str(), "default") == 0) @@ -83,20 +84,15 @@ private: } return nullptr; } - ConfDbInfo *GetDefaultConfiguration() { + + ke::RefPtr GetDefaultConfiguration() { return m_DefaultConfig; } void SetDefaultDriver(const char *input) { m_DefDriver = std::string(input); } - void ReleaseMembers() { - for (size_t i = 0; i < this->size(); i++) { - ConfDbInfo *current = this->at(i); - current->Release(); - } - } private: - ConfDbInfo *m_DefaultConfig; + ke::RefPtr m_DefaultConfig; std::string m_DefDriver; }; @@ -108,7 +104,7 @@ public: ~DatabaseConfBuilder(); void StartParse(); void SetPath(char* path); - ConfDbInfoList *GetConfigList(); + ConfDbInfoList &GetConfigList(); public: //ITextListener_SMC void ReadSMC_ParseStart(); SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name); @@ -120,10 +116,10 @@ private: unsigned int m_ParseLevel; unsigned int m_ParseState; ConfDbInfo *m_ParseCurrent; - ConfDbInfoList *m_ParseList; + ConfDbInfoList m_ParseList; private: std::string m_Filename; - ConfDbInfoList *m_InfoList; + ConfDbInfoList m_InfoList; }; #endif //_INCLUDE_DATABASE_CONF_BUILDER_H_