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)
{
ConfDbInfoList *list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> pInfo = list->GetDatabaseConf(name);
ConfDbInfoList &list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> 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<ConfDbInfo> 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<ConfDbInfo> info = list->GetDatabaseConf(name);
ConfDbInfoList &list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> 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<ConfDbInfo> DBManager::GetDatabaseConf(const char *name)
{
ConfDbInfoList *list = m_Builder.GetConfigList();
ke::RefPtr<ConfDbInfo> 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)

View File

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

View File

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

View File

@ -38,6 +38,7 @@
#include <am-vector.h>
#include <am-string.h>
#include <am-refcounting.h>
#include <am-refcounting-threadsafe.h>
class ConfDbInfo : public ke::RefcountedThreadsafe<ConfDbInfo>
@ -56,7 +57,7 @@ public:
DatabaseInfo info;
};
class ConfDbInfoList : public std::vector<ConfDbInfo *>
class ConfDbInfoList : public std::vector<ke::RefPtr<ConfDbInfo>>
{
/* Allow internal usage of ConfDbInfoList */
friend class DBManager;
@ -66,10 +67,10 @@ private:
return m_DefDriver;
}
ConfDbInfo *GetDatabaseConf(const char *name) {
ke::RefPtr<ConfDbInfo> GetDatabaseConf(const char *name) {
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
* 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<ConfDbInfo> 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<ConfDbInfo> 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_