Toc: clean-up

This commit is contained in:
Guillaume Munch 2016-06-02 21:40:11 +01:00
parent 6144bbfbf8
commit aedc6720c7
4 changed files with 35 additions and 66 deletions

View File

@ -49,9 +49,9 @@ namespace lyx {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
TocItem::TocItem(DocIterator const & dit, int d, docstring const & s, TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
bool output_active, docstring const & t, FuncRequest action) : bool output_active, docstring const & t, FuncRequest action)
dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active), : dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active),
action_(action) action_(action)
{ {
} }
@ -160,7 +160,7 @@ TocBuilder::TocBuilder(shared_ptr<Toc> toc)
} }
void TocBuilder::pushItem(DocIterator const & dit, docstring const & s, 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)); toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
frame f = { frame f = {
@ -171,7 +171,7 @@ void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
} }
void TocBuilder::captionItem(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 // first show the float before moving to the caption
docstring arg = "paragraph-goto " + paragraph_goto_arg(dit); docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
@ -207,23 +207,6 @@ void TocBuilder::pop()
///////////////////////////////////////////////////////////////////////////
//
// TocBuilderStore implementation
//
///////////////////////////////////////////////////////////////////////////
shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
shared_ptr<Toc> toc)
{
map_t::const_iterator it = map_.find(type);
if (it == map_.end())
it = map_.insert(make_pair(type, make_shared<TocBuilder>(toc))).first;
return it->second;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// //
// TocBackend implementation // TocBackend implementation
@ -241,16 +224,17 @@ shared_ptr<Toc const> TocBackend::toc(string const & type) const
shared_ptr<Toc> TocBackend::toc(string const & type) shared_ptr<Toc> TocBackend::toc(string const & type)
{ {
TocList::const_iterator it = tocs_.find(type); // std::map::insert only really performs the insertion if the key is not
if (it == tocs_.end()) // already bound, and otherwise returns an iterator to the element already
it = tocs_.insert(make_pair(type, make_shared<Toc>())).first; // there, see manual.
return it->second; return tocs_.insert({type, make_shared<Toc>()}).first->second;
} }
shared_ptr<TocBuilder> TocBackend::builder(string const & type) TocBuilder & TocBackend::builder(string const & type)
{ {
return builders_.get(type, toc(type)); auto p = make_unique<TocBuilder>(toc(type));
return * builders_.insert(make_pair(type, move(p))).first->second;
} }

View File

@ -21,6 +21,7 @@
#include "Toc.h" #include "Toc.h"
#include "support/strfwd.h" #include "support/strfwd.h"
#include "support/unique_ptr.h"
#include <stack> #include <stack>
@ -74,12 +75,11 @@ public:
TocItem() : dit_(0), depth_(0), output_(false) {} TocItem() : dit_(0), depth_(0), output_(false) {}
/// ///
TocItem(DocIterator const & dit, TocItem(DocIterator const & dit,
int depth, int depth,
docstring const & s, docstring const & s,
bool output_active, bool output_active,
docstring const & t = docstring(), docstring const & t = docstring(),
FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION) FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION));
);
/// ///
~TocItem() {} ~TocItem() {}
/// ///
@ -128,10 +128,10 @@ public:
TocBuilder(shared_ptr<Toc> const toc); TocBuilder(shared_ptr<Toc> const toc);
/// When entering a float /// When entering a float
void pushItem(DocIterator const & dit, docstring const & s, 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 /// When encountering a caption
void captionItem(DocIterator const & dit, docstring const & s, void captionItem(DocIterator const & dit, docstring const & s,
bool output_active); bool output_active);
/// When exiting a float /// When exiting a float
void pop(); void pop();
private: private:
@ -148,24 +148,7 @@ private:
}; };
/// /// Class to build and access the Tocs of a particular buffer.
class TocBuilderStore
{
public:
TocBuilderStore() {};
///
shared_ptr<TocBuilder> get(std::string const & type, shared_ptr<Toc> toc);
///
void clear() { map_.clear(); };
private:
typedef std::map<std::string, shared_ptr<TocBuilder>> map_t;
map_t map_;
};
///
/**
*/
class TocBackend class TocBackend
{ {
public: public:
@ -187,14 +170,16 @@ public:
TocList const & tocs() const { return tocs_; } TocList const & tocs() const { return tocs_; }
/// never null /// never null
shared_ptr<Toc const> toc(std::string const & type) const; shared_ptr<Toc const> toc(std::string const & type) const;
/// never null
shared_ptr<Toc> toc(std::string const & type); shared_ptr<Toc> toc(std::string const & type);
/// nevel null /// \return the current TocBuilder for the Toc of type \param type, or
shared_ptr<TocBuilder> builder(std::string const & type); /// creates one if it does not already exist.
/// Return the first Toc Item before the cursor TocBuilder & builder(std::string const & type);
Toc::const_iterator item( /// \return the first Toc Item before the cursor.
std::string const & type, ///< Type of Toc. /// \param type: Type of Toc.
DocIterator const & dit ///< The cursor location in the document. /// \param dit: The cursor location in the document.
) const; Toc::const_iterator
item(std::string const & type, DocIterator const & dit) const;
/// ///
void writePlaintextTocList(std::string const & type, void writePlaintextTocList(std::string const & type,
@ -206,7 +191,7 @@ private:
/// ///
TocList tocs_; TocList tocs_;
/// ///
TocBuilderStore builders_; std::map<std::string, unique_ptr<TocBuilder>> builders_;
/// ///
Buffer const * buffer_; Buffer const * buffer_;
}; // TocBackend }; // TocBackend

View File

@ -107,7 +107,7 @@ void InsetCaption::addToToc(DocIterator const & cpit, bool output_active,
str = full_label_; str = full_label_;
text().forOutliner(str, length); 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. // Proceed with the rest of the inset.
InsetText::addToToc(cpit, output_active, utype); InsetText::addToToc(cpit, output_active, utype);
} }

View File

@ -55,11 +55,11 @@ void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
// non-empty. // non-empty.
if (utype != OutputUpdate) if (utype != OutputUpdate)
text().forOutliner(str, TOC_ENTRY_LENGTH); text().forOutliner(str, TOC_ENTRY_LENGTH);
shared_ptr<TocBuilder> b = buffer().tocBackend().builder(caption_type_); TocBuilder & b = buffer().tocBackend().builder(caption_type_);
b->pushItem(pit, str, output_active); b.pushItem(pit, str, output_active);
// Proceed with the rest of the inset. // Proceed with the rest of the inset.
InsetCollapsable::addToToc(cpit, output_active, utype); InsetCollapsable::addToToc(cpit, output_active, utype);
b->pop(); b.pop();
} }
void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype) void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype)