Get rid of the TextCache.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7928 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Lars Gullik Bjønnes 2003-10-17 09:32:41 +00:00
parent f88fb47bc7
commit 1684282f8a
7 changed files with 18 additions and 321 deletions

View File

@ -41,7 +41,6 @@
#include "paragraph.h"
#include "paragraph_funcs.h"
#include "ParagraphParameters.h"
#include "TextCache.h"
#include "undo.h"
#include "vspace.h"
@ -297,14 +296,7 @@ void BufferView::Pimpl::buffer(Buffer * b)
<< b << ')' << endl;
if (buffer_) {
disconnectBuffer();
// Put the old text into the TextCache, but
// only if the buffer is still loaded.
// Also set the owner of the test to 0
// bv_->text->owner(0);
textcache.add(buffer_, workarea().workWidth(), bv_->text);
if (lyxerr.debugging())
textcache.show(lyxerr, "BufferView::buffer");
delete bv_->text;
bv_->text = 0;
}
@ -339,13 +331,6 @@ void BufferView::Pimpl::buffer(Buffer * b)
} else {
lyxerr[Debug::INFO] << " No Buffer!" << endl;
owner_->getDialogs().hideBufferDependent();
// Also remove all remaining text's from the testcache.
// (there should not be any!) (if there is any it is a
// bug!)
if (lyxerr.debugging())
textcache.show(lyxerr, "buffer delete all");
textcache.clear();
}
update();
@ -433,30 +418,11 @@ void BufferView::Pimpl::resizeCurrentBuffer()
update();
} else {
lyxerr << "text not available!" << endl;
// See if we have a text in TextCache that fits
// the new buffer_ with the correct width.
bv_->text = textcache.findFit(buffer_, workarea().workWidth());
if (bv_->text) {
lyxerr << "text in cache!" << endl;
if (lyxerr.debugging()) {
lyxerr << "Found a LyXText that fits:" << endl;
textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
}
// Set the owner of the newly found text
// bv_->text->owner(bv_);
if (lyxerr.debugging())
textcache.show(lyxerr, "resizeCurrentBuffer");
} else {
lyxerr << "no text in cache!" << endl;
bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
bv_->text->init(bv_);
}
}
#warning does not help much
//bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
// bv_->text->ownerParagraphs().end());
if (par != -1) {
bv_->text->selection.set(true);
// At this point just to avoid the Delete-Empty-Paragraph-
@ -629,16 +595,6 @@ void BufferView::Pimpl::workAreaResize()
if (widthChange) {
// The visible LyXView need a resize
resizeCurrentBuffer();
// Remove all texts from the textcache
// This is not _really_ what we want to do. What
// we really want to do is to delete in textcache
// that does not have a BufferView with matching
// width, but as long as we have only one BufferView
// deleting all gives the same result.
if (lyxerr.debugging())
textcache.show(lyxerr, "Expose delete all");
textcache.clear();
}
}

View File

@ -1,3 +1,20 @@
2003-10-17 Lars Gullik Bjønnes <larsbj@gullik.net>
* lyxfunc.C (dispatch): remove textcache stuff
* bufferlist.C (release): remove textcache stuff
(closeAll): ditto
* TextCache.C: delete file
* TextCache.h: delete file
* Makefile.am (lyx_SOURCES): delete TextCache.C and TextCache.h
* BufferView_pimpl.C (buffer): remove textcache stuff, add a
delete of the bv_->text.
(resizeCurrentBuffer): remove texcache stuff
(workAreaResize): ditto
2003-10-16 Lars Gullik Bjønnes <larsbj@gullik.net>
* lyxfunc.C (getStatus): also set flag disabled if it is a unknown

View File

@ -102,8 +102,6 @@ lyx_SOURCES = \
ShareContainer.h \
Spacing.C \
Spacing.h \
TextCache.C \
TextCache.h \
Thesaurus.C \
Thesaurus.h \
ToolbarBackend.C \

View File

@ -1,121 +0,0 @@
/**
* \file TextCache.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "TextCache.h"
#include "lyxtext.h"
#include "bufferlist.h"
#include "debug.h"
#include <algorithm>
using std::endl;
using std::find_if;
using std::for_each;
using std::make_pair;
using std::string;
using std::ostream;
extern BufferList bufferlist;
namespace {
class text_fits {
public:
text_fits(Buffer * b, int p)
: buf(b), pw(p) {}
bool operator()(TextCache::value_type const & vt) const {
if (vt.first == buf && vt.second.first == pw)
return true;
return false;
}
private:
Buffer * buf;
int pw;
};
class show_text {
public:
show_text(ostream & o) : os(o) {}
void operator()(TextCache::value_type const & vt) {
os << "\tBuffer: " << vt.first
<< "\tWidth: " << vt.second.first << endl;
}
private:
ostream & os;
};
class delete_text {
public:
void operator()(TextCache::value_type & vt) {
delete vt.second.second;
}
};
} // namespace anon
LyXText * TextCache::findFit(Buffer * b, int p)
{
Cache::iterator it = find_if(cache.begin(), cache.end(),
text_fits(b, p));
if (it != cache.end()) {
LyXText * tmp = it->second.second;
cache.erase(it);
return tmp;
}
return 0;
}
void TextCache::show(ostream & os, string const & str)
{
os << "TextCache: " << str << endl;
for_each(cache.begin(), cache.end(), show_text(os));
}
void TextCache::show(ostream & os, TextCache::value_type const & vt)
{
show_text st(os);
st(vt);
}
void TextCache::add(Buffer * buf, int workwidth, LyXText * text)
{
lyxerr[Debug::INFO] << "TextCache::add " << text;
if (bufferlist.isLoaded(buf)) {
cache[buf] = make_pair(workwidth, text);
lyxerr[Debug::INFO] << " added" << endl;
} else {
delete text;
lyxerr[Debug::INFO] << " deleted" << endl;
}
}
void TextCache::clear()
{
for_each(cache.begin(), cache.end(), delete_text());
cache.clear();
}
void TextCache::removeAllWithBuffer(Buffer * buf)
{
cache.erase(buf);
}
// Global instance
TextCache textcache;

View File

@ -1,137 +0,0 @@
// -*- C++ -*-
/**
* \file TextCache.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef TEXT_CACHE_H
#define TEXT_CACHE_H
#include <iosfwd>
#include <map>
#include <string>
class Buffer;
class LyXText;
// This is only the very first implemetation and use of the TextCache,
// operations on it needs to be put into a class or a namespace, that part
// is _NOT_ finished so don't bother to come with too many comments on it
// (unless you have some nice ideas on where/how to do it)
//
// I think we need a global TextCache that is common for all BufferViews,
// also the BufferList needs access to the TextCache. Please tell if you
// don't agree.
//
// Q. What are we caching?
// A. We are caching the screen representations (LyXText) of the
// documents (Buffer,Paragraph) for specific BufferView widths.
// Q. Why the cache?
// A. It is not really needed, but it speeds things up a lot
// when you have more than one document loaded at once since a total
// rebreak (reformatting) need not be done when switching between
// documents. When the cache is in function a document only needs to be
// formatted upon loading and when the with of the BufferView changes.
// Later it will also be unneccessary to reformat when having two
// BufferViews of equal width with the same document, a simple copy
// of the LyXText structure will do.
// Invariant for the TextCache:
// - The buffer of the text in the TextCache _must_ exists
// in the bufferlist.
// - For a text in the TextCache there _must not_ be an equivalent
// text in any BufferView. (same buffer and width).
// Among others this mean:
// - When a document is closed all trace of it must be removed from
// the TextCache.
// Scenarios:
// I believe there are only three possible scenarios where the two first
// are also covered by the third.
// - The simplest scenario is what we have now, a single
// BufferView only.
// o Opening
// Nothing to do with the TextCache is done when opening a file.
// o Switching
// We switch from buffer A to buffer B.
// * A's text is cached in TextCache.
// * We make a search for a text in TextCache that fits B
// (same buffer and same width).
// o Horizontal resize
// If the BufferView's width (LyXView) is horizontally changed all
// the entries in the TextCache are deleted. (This causes
// reformat of all loaded documents when next viewed)
// o Close
// When a buffer is closed we don't have to do anything, because
// to close a single buffer it is required to only exist in the
// BufferView and not in the TextCache. Upon LFUN_QUIT we
// don't really care since everything is deleted anyway.
// - The next scenario is when we have several BufferViews (in one or
// more LyXViews) of equal width.
// o Opening
// Nothing to do with the TextCache is done when opening a file.
// o Switching
// We switch from buffer A to buffer B.
// * If A is in another Bufferview we do not put it into TextCache.
// else we put A into TextCache.
// * If B is viewed in another BufferView we make a copy of its
// text and use that, else we search in TextCache for a match.
// (same buffer same width)
// o Horizontal resize
// If the BufferView's width (LyXView) is horisontaly changed all
// the entries in the TextCache is deleted. (This causes
// reformat of all loaded documents when next viewed)
// o Close
// - The last scenario should cover both the previous ones, this time
// we have several BufferViews (in one or more LyXViews) with no
// limitations on width. (And if you wonder why the two other
// senarios are needed... I used them to get to this one.)
// o Opening
// Nothing to do with the TextCache is done when opening a file.
// o Switching
// We switch from buffer A to buffer B.
// o Horisontal rezize
// o Close
/** This class is used to cache generated LyXText's.
The LyXText's is used by the BufferView to visualize the contents
of a buffer and its paragraphs. Instead of deleting the LyXText when
we switch from one document to another we cache it here so that when
we switch back we do not have to reformat. This makes switching very
fast at the expense of a bit higher memory usage.
*/
class TextCache {
public:
///
typedef std::map<Buffer *, std::pair<int,LyXText *> > Cache;
///
typedef Cache::value_type value_type;
/** Returns a pointer to a LyXText that fits the provided buffer
and width. Of there is no match 0 is returned. */
LyXText * findFit(Buffer * b, int p);
/** Lists all the LyXText's currently in the cache.
Uses msg as header for the list. */
void show(std::ostream & o, std::string const & msg);
/// Gives info on a single LyXText (buffer and width)
static void show(std::ostream & o, value_type const &);
/** Adds a LyXText to the cache iff its buffer is
present in bufferlist. */
void add(Buffer *, int witdth, LyXText *);
/** Clears the cache. Deletes all LyXText's and releases
the allocated memory. */
void clear();
/// Removes all LyXText's that has buffer b from the TextCache
void removeAllWithBuffer(Buffer * b);
private:
/// The cache.
Cache cache;
};
///
extern TextCache textcache;
#endif

View File

@ -21,7 +21,6 @@
#include "lyx_cb.h"
#include "lyx_main.h"
#include "paragraph.h"
#include "TextCache.h"
#include "frontends/Alert.h"
@ -119,12 +118,8 @@ void BufferList::release(Buffer * buf)
BOOST_ASSERT(buf);
BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
if (it != bstore.end()) {
// Make sure that we don't store a LyXText in
// the textcache that points to the buffer
// we just deleted.
Buffer * tmp = (*it);
bstore.erase(it);
textcache.removeAllWithBuffer(tmp);
delete tmp;
}
}
@ -143,10 +138,6 @@ Buffer * BufferList::newBuffer(string const & s, bool ronly)
void BufferList::closeAll()
{
// Since we are closing we can just as well delete all
// in the textcache this will also speed the closing/quiting up a bit.
textcache.clear();
while (!bstore.empty()) {
close(bstore.front(), false);
}

View File

@ -48,7 +48,6 @@
#include "lyxvc.h"
#include "paragraph.h"
#include "ParagraphParameters.h"
#include "TextCache.h"
#include "undo.h"
#include "insets/insetcommand.h"
@ -1583,12 +1582,6 @@ void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
// handle the screen font changes.
lyxrc.set_font_norm_type();
lyx_gui::update_fonts();
// We also need to empty the textcache so that
// the buffer will be formatted correctly after
// a zoom change.
textcache.clear();
// Of course we should only do the resize and the textcache.clear
// if values really changed...but not very important right now. (Lgb)
// All visible buffers will need resize
view()->resize();
view()->update();