diff options
-rw-r--r-- | src/ExternalLexer.cxx | 91 | ||||
-rw-r--r-- | src/ExternalLexer.h | 28 |
2 files changed, 25 insertions, 94 deletions
diff --git a/src/ExternalLexer.cxx b/src/ExternalLexer.cxx index 8710e0cdd..4a2811c18 100644 --- a/src/ExternalLexer.cxx +++ b/src/ExternalLexer.cxx @@ -11,6 +11,8 @@ #include <stdexcept> #include <string> +#include <vector> +#include <memory> #include "Platform.h" @@ -26,7 +28,7 @@ using namespace Scintilla; #endif -LexerManager *LexerManager::theInstance = NULL; +std::unique_ptr<LexerManager> LexerManager::theInstance; //------------------------------------------ // @@ -45,22 +47,15 @@ void ExternalLexerModule::SetExternal(GetLexerFactoryFunction fFactory, int inde // //------------------------------------------ -LexerLibrary::LexerLibrary(const char *ModuleName) { - // Initialise some members... - first = NULL; - last = NULL; - +LexerLibrary::LexerLibrary(const char *moduleName_) { // Load the DLL - lib = DynamicLibrary::Load(ModuleName); + lib.reset(DynamicLibrary::Load(moduleName_)); if (lib->IsValid()) { - m_sModuleName = ModuleName; + moduleName = moduleName_; //Cannot use reinterpret_cast because: ANSI C++ forbids casting between pointers to functions and objects GetLexerCountFn GetLexerCount = (GetLexerCountFn)(sptr_t)lib->FindFunction("GetLexerCount"); if (GetLexerCount) { - ExternalLexerModule *lex; - LexerMinder *lm; - // Find functions in the DLL GetLexerNameFn GetLexerName = (GetLexerNameFn)(sptr_t)lib->FindFunction("GetLexerName"); GetLexerFactoryFunction fnFactory = (GetLexerFactoryFunction)(sptr_t)lib->FindFunction("GetLexerFactory"); @@ -71,20 +66,11 @@ LexerLibrary::LexerLibrary(const char *ModuleName) { // Assign a buffer for the lexer name. char lexname[100] = ""; GetLexerName(i, lexname, sizeof(lexname)); - lex = new ExternalLexerModule(SCLEX_AUTOMATIC, NULL, lexname, NULL); + ExternalLexerModule *lex = new ExternalLexerModule(SCLEX_AUTOMATIC, NULL, lexname, NULL); Catalogue::AddLexerModule(lex); - // Create a LexerMinder so we don't leak the ExternalLexerModule... - lm = new LexerMinder; - lm->self = lex; - lm->next = NULL; - if (first != NULL) { - last->next = lm; - last = lm; - } else { - first = lm; - last = lm; - } + // Remember ExternalLexerModule so we don't leak it + modules.push_back(std::unique_ptr<ExternalLexerModule>(lex)); // The external lexer needs to know how to call into its DLL to // do its lexing and folding, we tell it here. @@ -92,27 +78,9 @@ LexerLibrary::LexerLibrary(const char *ModuleName) { } } } - next = NULL; } LexerLibrary::~LexerLibrary() { - Release(); - delete lib; -} - -void LexerLibrary::Release() { - LexerMinder *lm; - LexerMinder *lmNext; - lm = first; - while (NULL != lm) { - lmNext = lm->next; - delete lm->self; - delete lm; - lm = lmNext; - } - - first = NULL; - last = NULL; } //------------------------------------------ @@ -124,20 +92,17 @@ void LexerLibrary::Release() { /// Return the single LexerManager instance... LexerManager *LexerManager::GetInstance() { if (!theInstance) - theInstance = new LexerManager; - return theInstance; + theInstance.reset(new LexerManager); + return theInstance.get(); } /// Delete any LexerManager instance... void LexerManager::DeleteInstance() { - delete theInstance; - theInstance = NULL; + theInstance.reset(); } /// protected constructor - this is a singleton... LexerManager::LexerManager() { - first = NULL; - last = NULL; } LexerManager::~LexerManager() { @@ -145,41 +110,21 @@ LexerManager::~LexerManager() { } void LexerManager::Load(const char *path) { - LoadLexerLibrary(path); -} - -void LexerManager::LoadLexerLibrary(const char *module) { - for (LexerLibrary *ll = first; ll; ll= ll->next) { - if (strcmp(ll->m_sModuleName.c_str(), module) == 0) + for (const std::unique_ptr<LexerLibrary> &ll : libraries) { + if (ll->moduleName == path) return; } - LexerLibrary *lib = new LexerLibrary(module); - if (NULL != first) { - last->next = lib; - last = lib; - } else { - first = lib; - last = lib; - } + LexerLibrary *lib = new LexerLibrary(path); + libraries.push_back(std::unique_ptr<LexerLibrary>(lib)); } void LexerManager::Clear() { - if (NULL != first) { - LexerLibrary *cur = first; - LexerLibrary *next; - while (cur) { - next = cur->next; - delete cur; - cur = next; - } - first = NULL; - last = NULL; - } + libraries.clear(); } //------------------------------------------ // -// LexerManager +// LMMinder -- trigger to clean up at exit. // //------------------------------------------ diff --git a/src/ExternalLexer.h b/src/ExternalLexer.h index a85213e31..7804c19f0 100644 --- a/src/ExternalLexer.h +++ b/src/ExternalLexer.h @@ -38,26 +38,15 @@ public: virtual void SetExternal(GetLexerFactoryFunction fFactory, int index); }; -/// LexerMinder points to an ExternalLexerModule - so we don't leak them. -class LexerMinder { -public: - ExternalLexerModule *self; - LexerMinder *next; -}; - -/// LexerLibrary exists for every External Lexer DLL, contains LexerMinders. +/// LexerLibrary exists for every External Lexer DLL, contains ExternalLexerModules. class LexerLibrary { - DynamicLibrary *lib; - LexerMinder *first; - LexerMinder *last; - + std::unique_ptr<DynamicLibrary> lib; + std::vector<std::unique_ptr<ExternalLexerModule>> modules; public: - explicit LexerLibrary(const char *ModuleName); + explicit LexerLibrary(const char *moduleName_); ~LexerLibrary(); - void Release(); - LexerLibrary *next; - std::string m_sModuleName; + std::string moduleName; }; /// LexerManager manages external lexers, contains LexerLibrarys. @@ -73,11 +62,8 @@ public: private: LexerManager(); - static LexerManager *theInstance; - - void LoadLexerLibrary(const char *module); - LexerLibrary *first; - LexerLibrary *last; + static std::unique_ptr<LexerManager> theInstance; + std::vector<std::unique_ptr<LexerLibrary>> libraries; }; class LMMinder { |