Simplify class structure in TocBackend

Deriving from std::vector to provide helper functions appears a touch
excessive. Use typedef instead and move helper functions to the base class. New
header Toc.h provided to replace forward-declarations.

Remove TocIterator which is useless.
This commit is contained in:
Guillaume Munch 2016-01-08 19:06:50 +00:00 committed by Richard Heck
parent d48d426c72
commit e3540f33e1
12 changed files with 83 additions and 71 deletions

View File

@ -2229,8 +2229,8 @@ void Buffer::getLabelList(vector<docstring> & list) const
list.clear();
shared_ptr<Toc> toc = d->toc_backend.toc("label");
TocIterator toc_it = toc->begin();
TocIterator end = toc->end();
Toc::const_iterator toc_it = toc->begin();
Toc::const_iterator end = toc->end();
for (; toc_it != end; ++toc_it) {
if (toc_it->depth() == 0)
list.push_back(toc_it->str());

View File

@ -2407,8 +2407,8 @@ void BufferView::gotoLabel(docstring const & label)
// find label
shared_ptr<Toc> toc = buf->tocBackend().toc("label");
TocIterator toc_it = toc->begin();
TocIterator end = toc->end();
Toc::const_iterator toc_it = toc->begin();
Toc::const_iterator end = toc->end();
for (; toc_it != end; ++toc_it) {
if (label == toc_it->str()) {
lyx::dispatch(toc_it->action());

View File

@ -501,7 +501,7 @@ void Changes::addToToc(DocIterator const & cdit, Buffer const & buffer,
// ¶ U+00B6 PILCROW SIGN
str.push_back(0xb6);
docstring const & author = author_list.get(it->change.author).name();
Toc::iterator it = change_list->item(0, author);
Toc::iterator it = TocBackend::findItem(*change_list, 0, author);
if (it == change_list->end()) {
change_list->push_back(TocItem(dit, 0, author, true));
change_list->push_back(TocItem(dit, 1, str, output_active,

View File

@ -229,7 +229,7 @@ CursorSlice const & DocIterator::innerTextSlice() const
DocIterator DocIterator::getInnerText() const
{
DocIterator texted = *this;
while (!texted.inTexted())
while (!texted.inTexted())
texted.pop_back();
return texted;
}

View File

@ -289,6 +289,7 @@ HEADERFILESCORE = \
Text.h \
TextClass.h \
TextMetrics.h \
Toc.h \
TocBackend.h \
Trans.h \
Undo.h \

View File

@ -49,7 +49,6 @@ class MetricsInfo;
class OutputParams;
class PainterInfo;
class ParagraphParameters;
class Toc;
class WordLangTuple;
class XHTMLStream;
class otexstream;

43
src/Toc.h Normal file
View File

@ -0,0 +1,43 @@
// -*- C++ -*-
/**
* \file TocBackend.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Jean-Marc Lasgouttes
* \author Angus Leeming
* \author Abdelrazak Younes
* \author Guillaume Munch
*
* Full author contact details are available in file CREDITS.
*/
#ifndef TOC_H
#define TOC_H
#include "support/shared_ptr.h"
#include <map>
#include <vector>
#include <string>
namespace lyx {
// TocItem is defined in TocBackend.h
class TocItem;
typedef std::vector<TocItem> Toc;
class TocList : public std::map<std::string, shared_ptr<Toc> >
{
private:
// TocList should never map to null pointers.
// We forbid the following method which creates null pointers.
using std::map<std::string, shared_ptr<Toc> >::operator[];
};
} // namespace lyx
#endif // TOC_H

View File

@ -107,21 +107,15 @@ FuncRequest TocItem::action() const
//
///////////////////////////////////////////////////////////////////////////
TocIterator Toc::item(DocIterator const & dit) const
Toc::const_iterator TocBackend::findItem(Toc const & toc,
DocIterator const & dit)
{
TocIterator last = begin();
TocIterator it = end();
Toc::const_iterator last = toc.begin();
Toc::const_iterator it = toc.end();
if (it == last)
return it;
--it;
DocIterator dit_text = dit;
if (dit_text.inMathed()) {
// We are only interested in text so remove the math CursorSlice.
while (dit_text.inMathed())
dit_text.pop_back();
}
DocIterator dit_text = dit.getInnerText();
for (; it != last; --it) {
// We verify that we don't compare contents of two
@ -138,12 +132,12 @@ TocIterator Toc::item(DocIterator const & dit) const
}
Toc::iterator Toc::item(int depth, docstring const & str)
Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
{
if (empty())
return end();
iterator it = begin();
iterator itend = end();
if (toc.empty())
return toc.end();
Toc::iterator it = toc.begin();
Toc::iterator itend = toc.end();
for (; it != itend; ++it) {
if (it->depth() == depth && it->str() == str)
break;
@ -185,7 +179,7 @@ void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
arg = "paragraph-goto " +
paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
if (!stack_.empty() && !stack_.top().is_captioned) {
// The float we entered has not yet been assigned a caption.
// Assign the caption string to it.
@ -286,7 +280,7 @@ bool TocBackend::updateItem(DocIterator const & dit_in)
BufferParams const & bufparams = buffer_->params();
const int min_toclevel = bufparams.documentClass().min_toclevel();
TocIterator toc_item = item("tableofcontents", dit);
Toc::const_iterator toc_item = item("tableofcontents", dit);
docstring tocstring;
@ -336,14 +330,14 @@ void TocBackend::update(bool output_active, UpdateType utype)
}
TocIterator TocBackend::item(string const & type,
DocIterator const & dit) const
Toc::const_iterator TocBackend::item(string const & type,
DocIterator const & dit) const
{
TocList::const_iterator toclist_it = tocs_.find(type);
// Is the type supported?
// We will try to make the best of it in release mode
LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
return toclist_it->second->item(dit);
return findItem(*toclist_it->second, dit);
}
@ -352,8 +346,8 @@ void TocBackend::writePlaintextTocList(string const & type,
{
TocList::const_iterator cit = tocs_.find(type);
if (cit != tocs_.end()) {
TocIterator ccit = cit->second->begin();
TocIterator end = cit->second->end();
Toc::const_iterator ccit = cit->second->begin();
Toc::const_iterator end = cit->second->end();
for (; ccit != end; ++ccit) {
os << ccit->asString() << from_utf8("\n");
if (os.str().size() > max_length)

View File

@ -18,14 +18,11 @@
#include "DocIterator.h"
#include "FuncRequest.h"
#include "OutputEnums.h"
#include "Toc.h"
#include "support/shared_ptr.h"
#include "support/strfwd.h"
#include <map>
#include <vector>
#include <stack>
#include <string>
namespace lyx {
@ -67,7 +64,6 @@ enum TocType {
*/
class TocItem
{
friend class Toc;
friend class TocBackend;
friend class TocBuilder;
@ -123,25 +119,6 @@ private:
};
///
class Toc : public std::vector<TocItem>
{
public:
// This is needed to work around a libc++ bug
// https://llvm.org/bugs/show_bug.cgi?id=24137
Toc() {}
typedef std::vector<TocItem>::const_iterator const_iterator;
typedef std::vector<TocItem>::iterator iterator;
const_iterator item(DocIterator const & dit) const;
/// Look for a TocItem given its depth and string.
/// \return The first matching item.
/// \retval end() if no item was found.
iterator item(int depth, docstring const & str);
};
typedef Toc::const_iterator TocIterator;
/// Caption-enabled TOC builders
class TocBuilder
{
@ -169,16 +146,6 @@ private:
};
/// The ToC list.
/// A class and no typedef because we want to forward declare it.
class TocList : public std::map<std::string, shared_ptr<Toc> >
{
private:
// this can create null pointers
using std::map<std::string, shared_ptr<Toc> >::operator[];
};
///
class TocBuilderStore
{
@ -200,6 +167,12 @@ private:
class TocBackend
{
public:
static Toc::const_iterator findItem(Toc const & toc,
DocIterator const & dit);
/// Look for a TocItem given its depth and string.
/// \return The first matching item.
/// \retval end() if no item was found.
static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
///
TocBackend(Buffer const * buffer) : buffer_(buffer) {}
///
@ -216,7 +189,7 @@ public:
/// nevel null
shared_ptr<TocBuilder> builder(std::string const & type);
/// Return the first Toc Item before the cursor
TocIterator item(
Toc::const_iterator item(
std::string const & type, ///< Type of Toc.
DocIterator const & dit ///< The cursor location in the document.
) const;

View File

@ -133,7 +133,8 @@ QModelIndex TocModel::modelIndex(DocIterator const & dit) const
if (toc_->empty())
return QModelIndex();
unsigned int const toc_index = toc_->item(dit) - toc_->begin();
unsigned int const toc_index = TocBackend::findItem(*toc_, dit) -
toc_->begin();
QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
QVariant(toc_index), 1,
@ -329,10 +330,10 @@ TocItem const TocModels::currentItem(QString const & type,
return TocItem();
}
LASSERT(index.model() == it.value()->model(), return TocItem());
return it.value()->tocItem(index);
}
void TocModels::updateItem(QString const & type, DocIterator const & dit)
{

View File

@ -12,6 +12,8 @@
#ifndef TOCMODEL_H
#define TOCMODEL_H
#include "Toc.h"
#include "support/shared_ptr.h"
#include <QHash>
@ -22,8 +24,6 @@ namespace lyx {
class Buffer;
class BufferView;
class DocIterator;
class Toc;
class TocItem;
namespace frontend {

View File

@ -14,11 +14,12 @@
#include "InsetCommand.h"
#include "Toc.h"
namespace lyx {
class Paragraph;
class Toc;
/// Used to insert table of contents and similar lists
/// at present, supports only \tableofcontents and \listoflistings.