]> git.lyx.org Git - lyx.git/blob - src/ShareContainer.h
ShareContainer compile fixes
[lyx.git] / src / ShareContainer.h
1 // -*- C++ -*-
2
3 #ifndef SHARECONTAINER_H
4 #define SHARECONTAINER_H
5
6 #include <vector>
7 #include <algorithm>
8 #include <boost/utility.hpp>
9 #include <boost/smart_ptr.hpp>
10
11 ///
12 template<class Share>
13 class ShareContainer : public noncopyable {
14 public:
15         ///
16         typedef std::vector<boost::shared_ptr<Share> > Params;
17         ///
18         typedef typename Params::value_type value_type;
19         ///
20         value_type
21         get(Share const & ps) const {
22                 // First see if we already have this ps in the container
23                 Params::iterator it = params.begin();
24                 Params::iterator end = params.end();
25                 for (; it != end; ++it) {
26                         if (ps == *(*it).get())
27                                 break;
28                 }
29                 value_type tmp;
30                 if (it == end) {
31                         // ok we don't have it so we should
32                         // insert it.
33                         tmp.reset(new Share(ps));
34                         params.push_back(tmp);
35                         // We clean here. This can cause us to have
36                         // some (one) unique elemements some times
37                         // but we should gain a lot in speed.
38                         clean();
39                         std::sort(params.rbegin(), params.rend(), comp());
40                 } else {
41                         // yes we have it already
42                         tmp = *it;
43                 }
44                 return tmp;
45         }
46 private:
47         ///
48         struct comp {
49                 int operator()(value_type const & p1,
50                                value_type const & p2) {
51                         return p1.use_count() < p2.use_count();
52                 }
53         };
54         ///
55         struct isUnique {
56                 bool operator()(value_type const & p) const {
57                         return p.unique();
58                 }
59         };
60
61         ///
62         void clean() const {
63                 // Remove all unique items. (i.e. entries that only
64                 // exists in the conatainer and does not have a
65                 // corresponding paragrah.
66                 Params::iterator it = std::remove_if(params.begin(),
67                                                      params.end(),
68                                                      isUnique());
69                 params.erase(it, params.end());
70         }
71         
72         ///
73         mutable Params params;
74 };
75 #endif