]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
0b5be6340e0e35874ddbbe265039683c9d90230a
[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::ostream;
26 using std::for_each;
27 using std::remove_if;
28 using std::find_if;
29 using std::endl;
30
31 extern BufferList bufferlist;
32
33 class text_fits {
34 public:
35         text_fits(Buffer * b, unsigned short p)
36                 : buf(b), pw(p) {}
37         bool operator()(TextCache::value_type & vt) {
38                 if (vt->params == buf && vt->paperWidth() == pw) return true;
39                 return false;
40         }
41 private:
42         Buffer * buf;
43         unsigned short pw;
44 };
45
46
47 LyXText * TextCache::findFit(Buffer * b, unsigned short p)
48 {
49         Cache::iterator it = find_if(cache.begin(), cache.end(),
50                                      text_fits(b, p));
51         if (it != cache.end()) {
52                 LyXText * tmp = (*it);
53                 cache.erase(it);
54                 return tmp;
55         }
56         return 0;
57 }
58
59
60 class show_text {
61 public:
62         show_text(ostream & o) : os(o) {}
63         void operator()(TextCache::value_type & vt) {
64                 os << "\tBuffer: " << vt->params
65                    << "\tWidth: " << vt->paperWidth() << endl;
66         }
67 private:
68         ostream & os;
69 };
70
71 void TextCache::show(ostream & os, string const & str)
72 {
73         os << "TextCache: " << str << endl;
74         for_each(cache.begin(), cache.end(), show_text(os));
75 }
76
77
78 void TextCache::show(ostream & os, LyXText * lt)
79 {
80         show_text st(os);
81         st(lt);
82 }
83
84
85 void TextCache::add(LyXText * text)
86 {
87         lyxerr.debug() << "TextCache::add " << text;
88         if (bufferlist.isLoaded(text->params)) {
89                 cache.push_back(text);
90                 lyxerr.debug() << " added" << endl;
91         } else {
92                 delete text;
93                 lyxerr.debug() << " deleted" << endl;
94         }
95 }
96
97
98 class delete_text {
99 public:
100         void operator()(TextCache::value_type & vt) {
101                 delete vt;
102         }
103 };
104
105
106 void TextCache::clear()
107 {
108         for_each(cache.begin(), cache.end(), delete_text());
109         cache.clear();
110 }
111
112
113 class has_buffer {
114 public:
115         has_buffer(Buffer * b)
116                 : buf(b) {}
117         bool operator()(TextCache::value_type & vt) {
118                 if (vt->params == buf) return true;
119                 return false;
120         }
121 private:
122         Buffer * buf;
123 };
124
125
126 void TextCache::removeAllWithBuffer(Buffer * buf)
127 {
128         Cache::iterator it = remove_if(cache.begin(), cache.end(),
129                                        has_buffer(buf));
130         if (it != cache.end()) {
131                 if (lyxerr.debugging()) {
132                         lyxerr.debug() << "TextCache::removeAllWithbuffer "
133                                 "Removing:\n";
134                         for_each(it, cache.end(), show_text(lyxerr));
135                         lyxerr << endl;
136                 }
137                 for_each(it, cache.end(), delete_text());
138                 cache.erase(it, cache.end());
139         }
140 }
141
142 // Global instance
143 TextCache textcache;