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