]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / coordcache.h
1 #ifndef COORDCACHE_H
2 #define COORDCACHE_H
3
4 class InsetBase;
5 class LyXText;
6 class MathArray;
7 class Paragraph;
8
9 #include "support/types.h"
10
11 #include <boost/assert.hpp>
12
13 #include <map>
14
15
16 // All positions cached in this cache are only valid between subsequent
17 // updated. (x,y) == (0,0) is the upper left screen corner, x increases
18 // to the right, y increases downwords.
19
20 void lyxbreaker(void const * data, const char * hint, int size);
21 void lyxaborter(int x, int y);
22
23 struct Point {
24         Point()
25                 : x_(0), y_(0)
26         {}
27
28         Point(int x, int y) : x_(x), y_(y)
29         {
30                 BOOST_ASSERT(x > -3000);
31                 BOOST_ASSERT(x <  4000);
32                 BOOST_ASSERT(y > -3000);
33                 BOOST_ASSERT(y <  4000);
34         }
35
36         int x_, y_;
37 };
38
39
40 template <class T> class CoordCacheBase {
41 public:
42         void clear()
43         {
44                 data_.clear();
45         }
46
47         void add(T const * thing, int x, int y)
48         {
49                 data_[thing] = Point(x, y);
50         }
51
52         int x(T const * thing) const
53         {
54                 check(thing, "x");
55                 return data_.find(thing)->second.x_;
56         }
57
58         int y(T const * thing) const
59         {
60                 check(thing, "y");
61                 return data_.find(thing)->second.y_;
62         }
63
64         Point xy(T const * thing) const
65         {
66                 check(thing, "xy");
67                 return data_.find(thing)->second;
68         }
69
70         bool has(T const * thing) const
71         {
72                 return data_.find(thing) != data_.end();
73         }
74
75 //      T * find(int x, int y) const
76 //      {
77 //              T *
78 //              cache_type iter
79 //      }
80
81 private:
82         friend class CoordCache;
83
84         void check(T const * thing, char const * hint) const
85         {
86                 if (!has(thing))
87                         lyxbreaker(thing, hint, data_.size());
88         }
89
90         typedef std::map<T const *, Point> cache_type;
91         cache_type data_;
92 };
93
94
95 class CoordCache {
96 public:
97         void clear();
98         Point get(LyXText const *, lyx::pit_type);
99
100         CoordCacheBase<MathArray> arrays_;
101         
102         // all insets
103         CoordCacheBase<InsetBase> insets_;
104
105         // paragraph grouped by owning text
106         typedef std::map<lyx::pit_type, Point> InnerParPosCache;
107         typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
108         ParPosCache pars_;
109 };
110
111 extern CoordCache theCoords;
112
113 #endif