From 3dddcefc3c8ee388003d3af67dc825163c6ed456 Mon Sep 17 00:00:00 2001 From: Richard Kimberly Heck Date: Mon, 9 Nov 2020 15:39:20 -0500 Subject: [PATCH] Properly implement the singleton pattern --- src/Buffer.cpp | 2 +- src/mathed/InsetMathMacro.cpp | 4 ++-- src/mathed/InsetMathNest.cpp | 2 +- src/mathed/MacroTable.cpp | 4 ++-- src/mathed/MacroTable.h | 11 +++++++---- src/mathed/MathFactory.cpp | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 44b3178bf4..7ed1685327 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -3686,7 +3686,7 @@ MacroData const * Buffer::getMacro(docstring const & name, } if (global) { - data = MacroTable::globalMacros().get(name); + data = MacroTable::get().getMacro(name); if (data != nullptr) return data; } diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp index 57294c0753..600562f2fc 100644 --- a/src/mathed/InsetMathMacro.cpp +++ b/src/mathed/InsetMathMacro.cpp @@ -938,7 +938,7 @@ MacroData const * InsetMathMacro::macroBackup() const { if (macro()) return &d->macroBackup_; - if (MacroData const * data = MacroTable::globalMacros().get(name())) + if (MacroData const * data = MacroTable::get().getMacro(name())) return data; return nullptr; } @@ -957,7 +957,7 @@ void InsetMathMacro::validate(LaTeXFeatures & features) const features.require(d->required_); else if (!d->macro_) { // Update requires for known global macros. - MacroData const * data = MacroTable::globalMacros().get(name()); + MacroData const * data = MacroTable::get().getMacro(name()); if (data && !data->required().empty()) features.require(data->required()); } diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index ada0400f18..19aa745d50 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -2106,7 +2106,7 @@ MathCompletionList::MathCompletionList(Cursor const & cur) // fill in global macros macros.clear(); - MacroTable::globalMacros().getMacroNames(macros, false); + MacroTable::get().getMacroNames(macros, false); //lyxerr << "Globals completion macros: "; for (it = macros.begin(); it != macros.end(); ++it) { //lyxerr << "\\" + *it << " "; diff --git a/src/mathed/MacroTable.cpp b/src/mathed/MacroTable.cpp index 218c8492e2..2bc427b221 100644 --- a/src/mathed/MacroTable.cpp +++ b/src/mathed/MacroTable.cpp @@ -215,14 +215,14 @@ int MacroData::write(odocstream & os, bool overwriteRedefinition) const // ///////////////////////////////////////////////////////////////////// -MacroTable & MacroTable::globalMacros() +MacroTable & MacroTable::get() { static MacroTable theGlobalMacros; return theGlobalMacros; } -MacroData const * MacroTable::get(docstring const & name) const +MacroData const * MacroTable::getMacro(docstring const & name) const { const_iterator it = find(name); return it == end() ? 0 : &it->second; diff --git a/src/mathed/MacroTable.h b/src/mathed/MacroTable.h index 77b306fccd..ab3c6b486f 100644 --- a/src/mathed/MacroTable.h +++ b/src/mathed/MacroTable.h @@ -160,19 +160,22 @@ class MacroSet : public std::set {}; class MacroTable : public std::map { public: + /// We are a singleton + MacroTable() = default; + MacroTable(MacroTable const &) = delete; + void operator=(MacroTable const &) = delete; + /// the global list: our unique instance + static MacroTable & get(); /// Parse full "\\def..." or "\\newcommand..." or ... iterator insert(Buffer * buf, docstring const & definition); /// Insert pre-digested macro definition iterator insert(docstring const & name, MacroData const & data); /// - MacroData const * get(docstring const & name) const; + MacroData const * getMacro(docstring const & name) const; /// void dump(); /// void getMacroNames(std::set & names, bool gethidden) const; - - /// the global list - static MacroTable & globalMacros(); }; diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 54fe012857..f9569737f6 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -202,7 +202,7 @@ void initSymbols() required = ""; } else htmlname = xmlname = ""; - MacroTable::iterator it = MacroTable::globalMacros().insert( + MacroTable::iterator it = MacroTable::get().insert( 0, from_utf8(macro)); if (!extra.empty() || !htmlname.empty() || !xmlname.empty() || !required.empty()) { MathWordList::iterator wit = theMathWordList.find(it->first); -- 2.39.5