3 * \file ShareContainer.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
12 #ifndef SHARECONTAINER_H
13 #define SHARECONTAINER_H
15 #include <boost/utility.hpp>
16 #include <boost/shared_ptr.hpp>
22 /// Share objects between several users.
24 This class can be used to reduce memory consuption when you have a lot
25 of equal objects used all over your code.
27 \author Lars Gullik Bjønnes
30 class ShareContainer : boost::noncopyable {
33 typedef std::vector<boost::shared_ptr<Share> > Params;
35 typedef typename Params::value_type value_type;
36 /// Return a shared_ptr that points to a element equal to ps.
38 get(Share const & ps) const {
39 // First see if we already have this ps in the container
40 typename Params::iterator it = std::find_if(params.begin(),
44 if (it == params.end()) {
45 // ok we don't have it so we should
47 tmp.reset(new Share(ps));
48 params.push_back(tmp);
49 // We clean here. This can cause us to have
50 // some (one) unique elemements some times
51 // but we should gain a lot in speed.
54 // yes we have it already
56 // move it forward - optimization
57 // makes the next find faster.
58 if (it != params.begin())
59 std::swap(*it, *(it - 1));
64 /// A functor returning true if the elements are equal.
66 isEqual(Share const & s) : p_(s) {}
67 bool operator()(value_type const & p1) const {
68 return *p1.get() == p_;
73 /// A functor returning true if the element is unique.
75 bool operator()(value_type const & p) const {
80 /** Remove all unique items.
81 This removes all elements from params that is only referenced
82 from the private container. This can be considered a memory
86 typename Params::iterator it = std::remove_if(params.begin(),
89 params.erase(it, params.end());
92 /// The actual container.
93 mutable Params params;