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