]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
ws changes only
[lyx.git] / src / TextCache.C
1 /**
2  * \file TextCache.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "TextCache.h"
14 #include "lyxtext.h"
15 #include "bufferlist.h"
16 #include "debug.h"
17
18 #include <algorithm>
19
20 using std::endl;
21 using std::find_if;
22 using std::for_each;
23 using std::make_pair;
24 using std::string;
25 using std::ostream;
26
27 extern BufferList bufferlist;
28
29 namespace {
30
31 class text_fits {
32 public:
33         text_fits(Buffer * b, int p)
34                 : buf(b), pw(p) {}
35         bool operator()(TextCache::value_type const & vt) const {
36                 if (vt.first == buf && vt.second.first == pw)
37                         return true;
38                 return false;
39         }
40 private:
41         Buffer * buf;
42         int pw;
43 };
44
45
46 class show_text {
47 public:
48         show_text(ostream & o) : os(o) {}
49         void operator()(TextCache::value_type const & vt) {
50                 os << "\tBuffer: " << vt.first
51                    << "\tWidth: " << vt.second.first << endl;
52         }
53 private:
54         ostream & os;
55 };
56
57
58 class delete_text {
59 public:
60         void operator()(TextCache::value_type & vt) {
61                 delete vt.second.second;
62         }
63 };
64
65 } // namespace anon
66
67
68 LyXText * TextCache::findFit(Buffer * b, int p)
69 {
70         Cache::iterator it = find_if(cache.begin(), cache.end(),
71                                      text_fits(b, p));
72         if (it != cache.end()) {
73                 LyXText * tmp = it->second.second;
74                 cache.erase(it);
75                 return tmp;
76         }
77         return 0;
78 }
79
80
81 void TextCache::show(ostream & os, string const & str)
82 {
83         os << "TextCache: " << str << endl;
84         for_each(cache.begin(), cache.end(), show_text(os));
85 }
86
87
88 void TextCache::show(ostream & os, TextCache::value_type const & vt)
89 {
90         show_text st(os);
91         st(vt);
92 }
93
94
95 void TextCache::add(Buffer * buf, int workwidth, LyXText * text)
96 {
97         lyxerr[Debug::INFO] << "TextCache::add " << text;
98         if (bufferlist.isLoaded(buf)) {
99                 cache[buf] = make_pair(workwidth, text);
100                 lyxerr[Debug::INFO] << " added" << endl;
101         } else {
102                 delete text;
103                 lyxerr[Debug::INFO] << " deleted" << endl;
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 void TextCache::removeAllWithBuffer(Buffer * buf)
116 {
117         cache.erase(buf);
118 }
119
120 // Global instance
121 TextCache textcache;