]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
52645966f41ca9882b8bbee400de8e0f8bd8e5de
[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 & vt) {
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 void TextCache::show(ostream & os, string const & str)
74 {
75         os << "TextCache: " << str << endl;
76         for_each(cache.begin(), cache.end(), show_text(os));
77 }
78
79
80 void TextCache::show(ostream & os, TextCache::value_type const & vt)
81 {
82         show_text st(os);
83         st(vt);
84 }
85
86
87 void TextCache::add(Buffer *buf, int workwidth, LyXText * text)
88 {
89         lyxerr.debug() << "TextCache::add " << text;
90         if (bufferlist.isLoaded(buf)) {
91                 cache[buf] = make_pair(workwidth, text);
92                 lyxerr.debug() << " added" << endl;
93         } else {
94                 delete text;
95                 lyxerr.debug() << " deleted" << endl;
96         }
97 }
98
99
100 class delete_text {
101 public:
102         void operator()(TextCache::value_type & vt) {
103                 delete vt.second.second;
104         }
105 };
106
107
108 void TextCache::clear()
109 {
110         for_each(cache.begin(), cache.end(), delete_text());
111         cache.clear();
112 }
113
114
115 class has_buffer {
116 public:
117         has_buffer(Buffer * b)
118                 : buf(b) {}
119         bool operator()(TextCache::value_type & vt) {
120                 if (vt.first == buf) return true;
121                 return false;
122         }
123 private:
124         Buffer const * buf;
125 };
126
127
128 void TextCache::removeAllWithBuffer(Buffer * buf)
129 {
130         cache.erase(buf);
131 }
132
133 // Global instance
134 TextCache textcache;