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