]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
Asger's exchanging of the class and struct keywords.
[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 class Point {
24 public:
25         Point()
26                 : x_(0), y_(0)
27         {}
28
29         Point(int x, int y) : x_(x), y_(y)
30         {
31                 BOOST_ASSERT(x > -3000);
32                 BOOST_ASSERT(x <  4000);
33                 BOOST_ASSERT(y > -3000);
34                 BOOST_ASSERT(y <  4000);
35         }
36
37         int x_, y_;
38 };
39
40
41 template <class T> class CoordCacheBase {
42 public:
43         void clear()
44         {
45                 data_.clear();
46         }
47
48         void add(T const * thing, int x, int y)
49         {
50                 data_[thing] = Point(x, y);
51         }
52
53         int x(T const * thing) const
54         {
55                 check(thing, "x");
56                 return data_.find(thing)->second.x_;
57         }
58
59         int y(T const * thing) const
60         {
61                 check(thing, "y");
62                 return data_.find(thing)->second.y_;
63         }
64
65         Point xy(T const * thing) const
66         {
67                 check(thing, "xy");
68                 return data_.find(thing)->second;
69         }
70
71         bool has(T const * thing) const
72         {
73                 return data_.find(thing) != data_.end();
74         }
75
76 //      T * find(int x, int y) const
77 //      {
78 //              T *
79 //              cache_type iter
80 //      }
81
82 private:
83         friend class CoordCache;
84
85         void check(T const * thing, char const * hint) const
86         {
87                 if (!has(thing))
88                         lyxbreaker(thing, hint, data_.size());
89         }
90
91         typedef std::map<T const *, Point> cache_type;
92         cache_type data_;
93 };
94
95
96 class CoordCache {
97 public:
98         void clear();
99         Point get(LyXText const *, lyx::pit_type);
100
101         CoordCacheBase<MathArray> arrays_;
102         
103         // all insets
104         CoordCacheBase<InsetBase> insets_;
105
106         // paragraph grouped by owning text
107         typedef std::map<lyx::pit_type, Point> InnerParPosCache;
108         typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
109         ParPosCache pars_;
110 };
111
112 extern CoordCache theCoords;
113
114 #endif