diff options
author | Neil <nyamatongwe@gmail.com> | 2021-08-22 22:14:19 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2021-08-22 22:14:19 +1000 |
commit | 6f709917a1134aaf55fc83d8a9f18a30b50efd89 (patch) | |
tree | 303457f17677c8c92d484b4485e1ec4cfc2499c8 | |
parent | 557bea24106e3868d2b5c5c4b066575c5236538a (diff) | |
download | scintilla-mirror-6f709917a1134aaf55fc83d8a9f18a30b50efd89.tar.gz |
Encapsulate an ILexer5* in LexerInstance class to simplify client code.
-rw-r--r-- | src/Document.cxx | 14 | ||||
-rw-r--r-- | src/Document.h | 36 | ||||
-rw-r--r-- | src/ScintillaBase.cxx | 33 |
3 files changed, 44 insertions, 39 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index b5ec9a275..24ddd04ce 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -51,6 +51,16 @@ using namespace Scintilla; using namespace Scintilla::Internal; +LexInterface::LexInterface(Document *pdoc_) noexcept : pdoc(pdoc_), performingStyle(false) { +} + +LexInterface::~LexInterface() noexcept = default; + +void LexInterface::SetInstance(ILexer5 *instance_) { + instance.reset(instance_); + pdoc->LexerChanged(); +} + void LexInterface::Colourise(Sci::Position start, Sci::Position end) { if (pdoc && instance && !performingStyle) { // Protect against reentrance, which may occur, for example, when @@ -86,6 +96,10 @@ LineEndType LexInterface::LineEndTypesSupported() { return LineEndType::Default; } +bool LexInterface::UseContainerLexing() const noexcept { + return !instance; +} + ActionDuration::ActionDuration(double duration_, double minDuration_, double maxDuration_) noexcept : duration(duration_), minDuration(minDuration_), maxDuration(maxDuration_) { } diff --git a/src/Document.h b/src/Document.h index 7984ad018..e2962b4a3 100644 --- a/src/Document.h +++ b/src/Document.h @@ -164,21 +164,41 @@ public: bool isEnabled; }; +struct LexerReleaser { + // Called by unique_ptr to destroy/free the Resource + void operator()(Scintilla::ILexer5 *pLexer) noexcept { + if (pLexer) { + try { + pLexer->Release(); + } catch (...) { + // ILexer5::Release must not throw, ignore if it does. + } + } + } +}; + +using LexerInstance = std::unique_ptr<Scintilla::ILexer5, LexerReleaser>; + +// LexInterface defines the interface to ILexer used in Document. +// The LexState subclass is actually created and that is used within ScintillaBase +// to provide more methods that are exposed through Scintilla's external API. class LexInterface { protected: Document *pdoc; - Scintilla::ILexer5 *instance; + LexerInstance instance; bool performingStyle; ///< Prevent reentrance public: - explicit LexInterface(Document *pdoc_) noexcept : pdoc(pdoc_), instance(nullptr), performingStyle(false) { - } - virtual ~LexInterface() { - } + explicit LexInterface(Document *pdoc_) noexcept; + // Deleted so LexInterface objects can not be copied. + LexInterface(const LexInterface &) = delete; + LexInterface(LexInterface &&) = delete; + LexInterface &operator=(const LexInterface &) = delete; + LexInterface &operator=(LexInterface &&) = delete; + virtual ~LexInterface() noexcept; + void SetInstance(ILexer5 *instance_); void Colourise(Sci::Position start, Sci::Position end); virtual Scintilla::LineEndType LineEndTypesSupported(); - bool UseContainerLexing() const noexcept { - return instance == nullptr; - } + bool UseContainerLexing() const noexcept; }; struct RegexError : public std::runtime_error { diff --git a/src/ScintillaBase.cxx b/src/ScintillaBase.cxx index 238299734..b2a15a286 100644 --- a/src/ScintillaBase.cxx +++ b/src/ScintillaBase.cxx @@ -550,13 +550,8 @@ namespace Scintilla::Internal { class LexState : public LexInterface { public: explicit LexState(Document *pdoc_) noexcept; - void SetInstance(ILexer5 *instance_); - // Deleted so LexState objects can not be copied. - LexState(const LexState &) = delete; - LexState(LexState &&) = delete; - LexState &operator=(const LexState &) = delete; - LexState &operator=(LexState &&) = delete; - ~LexState() override; + + // LexInterface deleted the standard operators and defined the virtual destructor so don't need to here. const char *DescribeWordListSets(); void SetWordList(int n, const char *wl); @@ -592,30 +587,6 @@ public: LexState::LexState(Document *pdoc_) noexcept : LexInterface(pdoc_) { } -LexState::~LexState() { - if (instance) { - try { - instance->Release(); - } catch (...) { - // ILexer5::Release must not throw, ignore if it does. - } - instance = nullptr; - } -} - -void LexState::SetInstance(ILexer5 *instance_) { - if (instance) { - try { - instance->Release(); - } catch (...) { - // ILexer5::Release must not throw, ignore if it does. - } - instance = nullptr; - } - instance = instance_; - pdoc->LexerChanged(); -} - LexState *ScintillaBase::DocumentLexState() { if (!pdoc->GetLexInterface()) { pdoc->SetLexInterface(std::make_unique<LexState>(pdoc)); |