Lift raw pointers out of DatabaseConfBuilder (#1637)

* Lift raw pointers out of DatabaseConfBuilder

* Maybe: not
This commit is contained in:
Headline 2022-05-24 02:41:15 -07:00 committed by GitHub
parent 3bafc1e2f4
commit c570150318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 46 deletions

View File

@ -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) bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength)
{ {
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> pInfo = list->GetDatabaseConf(name); ke::RefPtr<ConfDbInfo> pInfo = list.GetDatabaseConf(name);
if (!pInfo) if (!pInfo)
{ {
@ -148,7 +148,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool
/* Try to assign a real driver pointer */ /* Try to assign a real driver pointer */
if (pInfo->info.driver[0] == '\0') if (pInfo->info.driver[0] == '\0')
{ {
std::string defaultDriver = list->GetDefaultDriver(); std::string defaultDriver = list.GetDefaultDriver();
if (!m_pDefault && defaultDriver.length() > 0) if (!m_pDefault && defaultDriver.length() > 0)
{ {
m_pDefault = FindOrLoadDriver(defaultDriver.c_str()); m_pDefault = FindOrLoadDriver(defaultDriver.c_str());
@ -209,17 +209,14 @@ void DBManager::RemoveDriver(IDBDriver *pDriver)
} }
} }
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
for (size_t i = 0; i < list->size(); i++) for (auto conf : list) {
if (conf->realDriver == pDriver)
{ {
ke::RefPtr<ConfDbInfo> current = list->at(i); conf->realDriver = NULL;
if (current->realDriver == pDriver)
{
current->realDriver = NULL;
} }
} }
/* Someone unloaded the default driver? Silly.. */ /* Someone unloaded the default driver? Silly.. */
if (pDriver == m_pDefault) if (pDriver == m_pDefault)
{ {
@ -255,8 +252,8 @@ void DBManager::RemoveDriver(IDBDriver *pDriver)
IDBDriver *DBManager::GetDefaultDriver() IDBDriver *DBManager::GetDefaultDriver()
{ {
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
std::string defaultDriver = list->GetDefaultDriver(); std::string defaultDriver = list.GetDefaultDriver();
if (!m_pDefault && defaultDriver.length() > 0) if (!m_pDefault && defaultDriver.length() > 0)
{ {
m_pDefault = FindOrLoadDriver(defaultDriver.c_str()); m_pDefault = FindOrLoadDriver(defaultDriver.c_str());
@ -322,12 +319,12 @@ IDBDriver *DBManager::GetDriver(unsigned int index)
const DatabaseInfo *DBManager::FindDatabaseConf(const char *name) const DatabaseInfo *DBManager::FindDatabaseConf(const char *name)
{ {
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> info = list->GetDatabaseConf(name); ke::RefPtr<ConfDbInfo> info = list.GetDatabaseConf(name);
if (!info) if (!info)
{ {
// couldn't find requested conf, return default if exists // couldn't find requested conf, return default if exists
info = list->GetDefaultConfiguration(); info = list.GetDefaultConfiguration();
if (!info) if (!info)
{ {
return NULL; return NULL;
@ -337,11 +334,10 @@ const DatabaseInfo *DBManager::FindDatabaseConf(const char *name)
return &info->info; return &info->info;
} }
ConfDbInfo *DBManager::GetDatabaseConf(const char *name) ke::RefPtr<ConfDbInfo> DBManager::GetDatabaseConf(const char *name)
{ {
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> info(list->GetDatabaseConf(name)); return list.GetDatabaseConf(name);
return info;
} }
IDBDriver *DBManager::FindOrLoadDriver(const char *name) IDBDriver *DBManager::FindOrLoadDriver(const char *name)
@ -582,8 +578,8 @@ void DBManager::OnPluginWillUnload(IPlugin *plugin)
std::string DBManager::GetDefaultDriverName() std::string DBManager::GetDefaultDriverName()
{ {
ConfDbInfoList *list = m_Builder.GetConfigList(); ConfDbInfoList &list = m_Builder.GetConfigList();
return list->GetDefaultDriver(); return list.GetDefaultDriver();
} }
void DBManager::AddDependency(IExtension *myself, IDBDriver *driver) void DBManager::AddDependency(IExtension *myself, IDBDriver *driver)

View File

@ -71,7 +71,7 @@ public: //IDBManager
void AddDriver(IDBDriver *pDrivera); void AddDriver(IDBDriver *pDrivera);
void RemoveDriver(IDBDriver *pDriver); void RemoveDriver(IDBDriver *pDriver);
const DatabaseInfo *FindDatabaseConf(const char *name); const DatabaseInfo *FindDatabaseConf(const char *name);
ConfDbInfo *GetDatabaseConf(const char *name); ke::RefPtr<ConfDbInfo> GetDatabaseConf(const char *name);
bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength); bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
unsigned int GetDriverCount(); unsigned int GetDriverCount();
IDBDriver *GetDriver(unsigned int index); IDBDriver *GetDriver(unsigned int index);

View File

@ -36,8 +36,8 @@
#define DBPARSE_LEVEL_DATABASE 2 #define DBPARSE_LEVEL_DATABASE 2
DatabaseConfBuilder::DatabaseConfBuilder() DatabaseConfBuilder::DatabaseConfBuilder()
: m_ParseList(nullptr), : m_ParseList(),
m_InfoList(new ConfDbInfoList()) m_InfoList()
{ {
} }
@ -50,7 +50,7 @@ DatabaseConfBuilder::~DatabaseConfBuilder()
{ {
} }
ConfDbInfoList *DatabaseConfBuilder::GetConfigList() ConfDbInfoList &DatabaseConfBuilder::GetConfigList()
{ {
return m_InfoList; return m_InfoList;
} }
@ -75,7 +75,7 @@ void DatabaseConfBuilder::ReadSMC_ParseStart()
m_ParseLevel = 0; m_ParseLevel = 0;
m_ParseState = DBPARSE_LEVEL_NONE; m_ParseState = DBPARSE_LEVEL_NONE;
m_ParseList = new ConfDbInfoList(); m_ParseList.clear();
} }
SMCResult DatabaseConfBuilder::ReadSMC_NewSection(const SMCStates *states, const char *name) 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) if (strcmp(key, "driver_default") == 0)
{ {
m_ParseList->SetDefaultDriver(value); m_ParseList.SetDefaultDriver(value);
} }
} else if (m_ParseState == DBPARSE_LEVEL_DATABASE) { } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
if (strcmp(key, "driver") == 0) if (strcmp(key, "driver") == 0)
@ -161,7 +161,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_LeavingSection(const SMCStates *states)
/* Save it.. */ /* Save it.. */
m_ParseCurrent->AddRef(); m_ParseCurrent->AddRef();
m_ParseList->push_back(m_ParseCurrent); m_ParseList.push_back(m_ParseCurrent);
m_ParseCurrent = nullptr; m_ParseCurrent = nullptr;
/* Go up one level */ /* Go up one level */
@ -176,9 +176,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_LeavingSection(const SMCStates *states)
void DatabaseConfBuilder::ReadSMC_ParseEnd(bool halted, bool failed) void DatabaseConfBuilder::ReadSMC_ParseEnd(bool halted, bool failed)
{ {
m_InfoList->ReleaseMembers(); m_InfoList.clear();
delete m_InfoList;
m_InfoList = m_ParseList; m_InfoList = m_ParseList;
m_ParseList.clear();
m_ParseList = nullptr;
} }

View File

@ -38,6 +38,7 @@
#include <am-vector.h> #include <am-vector.h>
#include <am-string.h> #include <am-string.h>
#include <am-refcounting.h>
#include <am-refcounting-threadsafe.h> #include <am-refcounting-threadsafe.h>
class ConfDbInfo : public ke::RefcountedThreadsafe<ConfDbInfo> class ConfDbInfo : public ke::RefcountedThreadsafe<ConfDbInfo>
@ -56,7 +57,7 @@ public:
DatabaseInfo info; DatabaseInfo info;
}; };
class ConfDbInfoList : public std::vector<ConfDbInfo *> class ConfDbInfoList : public std::vector<ke::RefPtr<ConfDbInfo>>
{ {
/* Allow internal usage of ConfDbInfoList */ /* Allow internal usage of ConfDbInfoList */
friend class DBManager; friend class DBManager;
@ -66,10 +67,10 @@ private:
return m_DefDriver; return m_DefDriver;
} }
ConfDbInfo *GetDatabaseConf(const char *name) { ke::RefPtr<ConfDbInfo> GetDatabaseConf(const char *name) {
for (size_t i = 0; i < this->size(); i++) for (size_t i = 0; i < this->size(); i++)
{ {
ConfDbInfo *current = this->at(i); ke::RefPtr<ConfDbInfo> current = this->at(i);
/* If we run into the default configuration, then we'll save it /* If we run into the default configuration, then we'll save it
* for the next call to GetDefaultConfiguration */ * for the next call to GetDefaultConfiguration */
if (strcmp(current->name.c_str(), "default") == 0) if (strcmp(current->name.c_str(), "default") == 0)
@ -83,20 +84,15 @@ private:
} }
return nullptr; return nullptr;
} }
ConfDbInfo *GetDefaultConfiguration() {
ke::RefPtr<ConfDbInfo> GetDefaultConfiguration() {
return m_DefaultConfig; return m_DefaultConfig;
} }
void SetDefaultDriver(const char *input) { void SetDefaultDriver(const char *input) {
m_DefDriver = std::string(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: private:
ConfDbInfo *m_DefaultConfig; ke::RefPtr<ConfDbInfo> m_DefaultConfig;
std::string m_DefDriver; std::string m_DefDriver;
}; };
@ -108,7 +104,7 @@ public:
~DatabaseConfBuilder(); ~DatabaseConfBuilder();
void StartParse(); void StartParse();
void SetPath(char* path); void SetPath(char* path);
ConfDbInfoList *GetConfigList(); ConfDbInfoList &GetConfigList();
public: //ITextListener_SMC public: //ITextListener_SMC
void ReadSMC_ParseStart(); void ReadSMC_ParseStart();
SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name); SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
@ -120,10 +116,10 @@ private:
unsigned int m_ParseLevel; unsigned int m_ParseLevel;
unsigned int m_ParseState; unsigned int m_ParseState;
ConfDbInfo *m_ParseCurrent; ConfDbInfo *m_ParseCurrent;
ConfDbInfoList *m_ParseList; ConfDbInfoList m_ParseList;
private: private:
std::string m_Filename; std::string m_Filename;
ConfDbInfoList *m_InfoList; ConfDbInfoList m_InfoList;
}; };
#endif //_INCLUDE_DATABASE_CONF_BUILDER_H_ #endif //_INCLUDE_DATABASE_CONF_BUILDER_H_