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