]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
ac89bd251e7bd8d06f1df2924653dd8750394be9
[lyx.git] / src / coordcache.h
1 #ifndef COORDCACHE_H
2 #define COORDCACHE_H
3
4 #include "mathed/math_data.h"
5 #include "insets/insetbase.h"
6 #include "lyxtext.h"
7
8 #include <boost/assert.hpp>
9
10 #include <map>
11
12 void lyxbreaker(void const * data, const char * hint, int size);
13
14 struct Point {
15         Point()
16                 : x_(0), y_(0)
17         {}      
18
19         Point(int x, int y) : x_(x), y_(y)
20         {
21                 BOOST_ASSERT(x > -3000);
22                 BOOST_ASSERT(x <  4000);
23                 BOOST_ASSERT(y > -3000);
24                 BOOST_ASSERT(y <  4000);
25         }       
26
27         int x_, y_;
28 };
29
30
31 template <class T> class CoordCacheBase {
32 public:
33         void clear()
34         {
35                 data_.clear();
36         }
37
38         void add(T const * thing, int x, int y)
39         {
40                 data_[thing] = Point(x, y);
41         }
42
43         int x(T const * thing) const
44         {
45                 check(thing, "x");
46                 return data_.find(thing)->second.x_;
47         }
48
49         int y(T const * thing) const
50         {
51                 check(thing, "y");
52                 return data_.find(thing)->second.y_;
53         }
54
55         Point xy(T const * thing) const
56         {
57                 check(thing, "xy");
58                 return data_.find(thing)->second;
59         }
60
61         bool has(T const * thing) const
62         {
63                 return data_.find(thing) != data_.end();
64         }
65
66 private:
67         friend class CoordCache;
68
69         void check(T const * thing, char const * hint) const
70         {
71                 if (!has(thing)) {
72                         lyxbreaker(thing, hint, data_.size());
73                         BOOST_ASSERT(false);
74                 }
75         }
76
77         std::map<T const *, Point> data_;
78 };
79
80
81 class CoordCache {
82 public:
83         void clear();
84
85         CoordCacheBase<MathArray> arrays_;
86         CoordCacheBase<InsetBase> insets_;
87 };
88
89 extern CoordCache theCoords;
90
91 #endif