From aedc6720c7477ffc44d2ebb6c62540c22dd364b1 Mon Sep 17 00:00:00 2001 From: Guillaume Munch Date: Thu, 2 Jun 2016 21:40:11 +0100 Subject: [PATCH] Toc: clean-up --- src/TocBackend.cpp | 40 ++++++++----------------- src/TocBackend.h | 53 ++++++++++++--------------------- src/insets/InsetCaption.cpp | 2 +- src/insets/InsetCaptionable.cpp | 6 ++-- 4 files changed, 35 insertions(+), 66 deletions(-) diff --git a/src/TocBackend.cpp b/src/TocBackend.cpp index e8c430d8eb..2bbdab3c12 100644 --- a/src/TocBackend.cpp +++ b/src/TocBackend.cpp @@ -49,9 +49,9 @@ namespace lyx { /////////////////////////////////////////////////////////////////////////// TocItem::TocItem(DocIterator const & dit, int d, docstring const & s, - bool output_active, docstring const & t, FuncRequest action) : - dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active), - action_(action) + bool output_active, docstring const & t, FuncRequest action) + : dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active), + action_(action) { } @@ -160,7 +160,7 @@ TocBuilder::TocBuilder(shared_ptr toc) } void TocBuilder::pushItem(DocIterator const & dit, docstring const & s, - bool output_active, bool is_captioned) + bool output_active, bool is_captioned) { toc_->push_back(TocItem(dit, stack_.size(), s, output_active)); frame f = { @@ -171,7 +171,7 @@ void TocBuilder::pushItem(DocIterator const & dit, docstring const & s, } void TocBuilder::captionItem(DocIterator const & dit, docstring const & s, - bool output_active) + bool output_active) { // first show the float before moving to the caption docstring arg = "paragraph-goto " + paragraph_goto_arg(dit); @@ -207,23 +207,6 @@ void TocBuilder::pop() -/////////////////////////////////////////////////////////////////////////// -// -// TocBuilderStore implementation -// -/////////////////////////////////////////////////////////////////////////// - -shared_ptr TocBuilderStore::get(string const & type, - shared_ptr toc) -{ - map_t::const_iterator it = map_.find(type); - if (it == map_.end()) - it = map_.insert(make_pair(type, make_shared(toc))).first; - return it->second; -} - - - /////////////////////////////////////////////////////////////////////////// // // TocBackend implementation @@ -241,16 +224,17 @@ shared_ptr TocBackend::toc(string const & type) const shared_ptr TocBackend::toc(string const & type) { - TocList::const_iterator it = tocs_.find(type); - if (it == tocs_.end()) - it = tocs_.insert(make_pair(type, make_shared())).first; - return it->second; + // std::map::insert only really performs the insertion if the key is not + // already bound, and otherwise returns an iterator to the element already + // there, see manual. + return tocs_.insert({type, make_shared()}).first->second; } -shared_ptr TocBackend::builder(string const & type) +TocBuilder & TocBackend::builder(string const & type) { - return builders_.get(type, toc(type)); + auto p = make_unique(toc(type)); + return * builders_.insert(make_pair(type, move(p))).first->second; } diff --git a/src/TocBackend.h b/src/TocBackend.h index 1579cf2fdc..336daf7853 100644 --- a/src/TocBackend.h +++ b/src/TocBackend.h @@ -21,6 +21,7 @@ #include "Toc.h" #include "support/strfwd.h" +#include "support/unique_ptr.h" #include @@ -74,12 +75,11 @@ public: TocItem() : dit_(0), depth_(0), output_(false) {} /// TocItem(DocIterator const & dit, - int depth, - docstring const & s, - bool output_active, - docstring const & t = docstring(), - FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION) - ); + int depth, + docstring const & s, + bool output_active, + docstring const & t = docstring(), + FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)); /// ~TocItem() {} /// @@ -128,10 +128,10 @@ public: TocBuilder(shared_ptr const toc); /// When entering a float void pushItem(DocIterator const & dit, docstring const & s, - bool output_active, bool is_captioned = false); + bool output_active, bool is_captioned = false); /// When encountering a caption void captionItem(DocIterator const & dit, docstring const & s, - bool output_active); + bool output_active); /// When exiting a float void pop(); private: @@ -148,24 +148,7 @@ private: }; -/// -class TocBuilderStore -{ -public: - TocBuilderStore() {}; - /// - shared_ptr get(std::string const & type, shared_ptr toc); - /// - void clear() { map_.clear(); }; -private: - typedef std::map> map_t; - map_t map_; -}; - - -/// -/** -*/ +/// Class to build and access the Tocs of a particular buffer. class TocBackend { public: @@ -187,14 +170,16 @@ public: TocList const & tocs() const { return tocs_; } /// never null shared_ptr toc(std::string const & type) const; + /// never null shared_ptr toc(std::string const & type); - /// nevel null - shared_ptr builder(std::string const & type); - /// Return the first Toc Item before the cursor - Toc::const_iterator item( - std::string const & type, ///< Type of Toc. - DocIterator const & dit ///< The cursor location in the document. - ) const; + /// \return the current TocBuilder for the Toc of type \param type, or + /// creates one if it does not already exist. + TocBuilder & builder(std::string const & type); + /// \return the first Toc Item before the cursor. + /// \param type: Type of Toc. + /// \param dit: The cursor location in the document. + Toc::const_iterator + item(std::string const & type, DocIterator const & dit) const; /// void writePlaintextTocList(std::string const & type, @@ -206,7 +191,7 @@ private: /// TocList tocs_; /// - TocBuilderStore builders_; + std::map> builders_; /// Buffer const * buffer_; }; // TocBackend diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index 208ad1ac38..fbb55fc706 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -107,7 +107,7 @@ void InsetCaption::addToToc(DocIterator const & cpit, bool output_active, str = full_label_; text().forOutliner(str, length); } - buffer().tocBackend().builder(type)->captionItem(pit, str, output_active); + buffer().tocBackend().builder(type).captionItem(pit, str, output_active); // Proceed with the rest of the inset. InsetText::addToToc(cpit, output_active, utype); } diff --git a/src/insets/InsetCaptionable.cpp b/src/insets/InsetCaptionable.cpp index 0434c2eb2f..c105d5ed99 100644 --- a/src/insets/InsetCaptionable.cpp +++ b/src/insets/InsetCaptionable.cpp @@ -55,11 +55,11 @@ void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active, // non-empty. if (utype != OutputUpdate) text().forOutliner(str, TOC_ENTRY_LENGTH); - shared_ptr b = buffer().tocBackend().builder(caption_type_); - b->pushItem(pit, str, output_active); + TocBuilder & b = buffer().tocBackend().builder(caption_type_); + b.pushItem(pit, str, output_active); // Proceed with the rest of the inset. InsetCollapsable::addToToc(cpit, output_active, utype); - b->pop(); + b.pop(); } void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype)