]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
b2f6c1b75c3d563725395aaa3900158da02c080c
[lyx.git] / src / TextCache.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor         
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2000 The LyX Team
7  *
8  *           This file is Copyright 2000
9  *           Lars Gullik Bjønnes
10  *
11  * ====================================================== */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20
21 #include "TextCache.h"
22 #include "buffer.h"
23 #include "bufferlist.h"
24
25 using std::for_each;
26 using std::remove_if;
27 using std::find_if;
28 using std::endl;
29
30 extern BufferList bufferlist;
31
32 class text_fits {
33 public:
34         text_fits(Buffer * b, unsigned short p)
35                 : buf(b), pw(p) {}
36         bool operator()(TextCache::value_type & vt) {
37                 if (vt->params == buf && vt->paperWidth() == pw) return true;
38                 return false;
39         }
40 private:
41         Buffer * buf;
42         unsigned short pw;
43 };
44
45
46 LyXText * TextCache::findFit(Buffer * b, unsigned short p)
47 {
48         Cache::iterator it = find_if(cache.begin(), cache.end(),
49                                      text_fits(b, p));
50         if (it != cache.end()) {
51                 LyXText * tmp = (*it);
52                 cache.erase(it);
53                 return tmp;
54         }
55         return 0;
56 }
57
58
59 class show_text {
60 public:
61         show_text(ostream & o) : os(o) {}
62         void operator()(TextCache::value_type & vt) {
63                 os << "\tBuffer: " << vt->params
64                    << "\tWidth: " << vt->paperWidth() << endl;
65         }
66 private:
67         ostream & os;
68 };
69
70 void TextCache::show(ostream & os, string const & str)
71 {
72         os << "TextCache: " << str << endl;
73         for_each(cache.begin(), cache.end(), show_text(os));
74 }
75
76
77 void TextCache::show(ostream & os, LyXText * lt)
78 {
79         show_text st(os);
80         st(lt);
81 }
82
83
84 void TextCache::add(LyXText * text)
85 {
86         lyxerr.debug() << "TextCache::add " << text;
87         if (bufferlist.isLoaded(text->params)) {
88                 cache.push_back(text);
89                 lyxerr.debug() << " added" << endl;
90         } else {
91                 delete text;
92                 lyxerr.debug() << " deleted" << endl;
93         }
94 }
95
96
97 class delete_text {
98 public:
99         void operator()(TextCache::value_type & vt) {
100                 delete vt;
101         }
102 };
103
104
105 void TextCache::clear()
106 {
107         for_each(cache.begin(), cache.end(), delete_text());
108         cache.clear();
109 }
110
111
112 class has_buffer {
113 public:
114         has_buffer(Buffer * b)
115                 : buf(b) {}
116         bool operator()(TextCache::value_type & vt) {
117                 if (vt->params == buf) return true;
118                 return false;
119         }
120 private:
121         Buffer * buf;
122 };
123
124
125 void TextCache::removeAllWithBuffer(Buffer * buf)
126 {
127         Cache::iterator it = remove_if(cache.begin(), cache.end(),
128                                        has_buffer(buf));
129         if (it != cache.end()) {
130                 if (lyxerr.debugging()) {
131                         lyxerr.debug() << "TextCache::removeAllWithbuffer "
132                                 "Removing:\n";
133                         for_each(it, cache.end(), show_text(lyxerr));
134                         lyxerr << endl;
135                 }
136                 for_each(it, cache.end(), delete_text());
137                 cache.erase(it, cache.end());
138         }
139 }
140
141 // Global instance
142 TextCache textcache;