• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2004
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         object_cache.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Implements a generic object cache.
17   */
18 
19 #ifndef BOOST_REGEX_OBJECT_CACHE_HPP
20 #define BOOST_REGEX_OBJECT_CACHE_HPP
21 
22 #include <boost/regex/config.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <map>
25 #include <list>
26 #include <stdexcept>
27 #include <string>
28 #ifdef BOOST_HAS_THREADS
29 #include <boost/regex/pending/static_mutex.hpp>
30 #endif
31 
32 namespace boost{
33 
34 template <class Key, class Object>
35 class object_cache
36 {
37 public:
38    typedef std::pair< ::boost::shared_ptr<Object const>, Key const*> value_type;
39    typedef std::list<value_type> list_type;
40    typedef typename list_type::iterator list_iterator;
41    typedef std::map<Key, list_iterator> map_type;
42    typedef typename map_type::iterator map_iterator;
43    typedef typename list_type::size_type size_type;
44    static boost::shared_ptr<Object const> get(const Key& k, size_type l_max_cache_size);
45 
46 private:
47    static boost::shared_ptr<Object const> do_get(const Key& k, size_type l_max_cache_size);
48 
49    struct data
50    {
51       list_type   cont;
52       map_type    index;
53    };
54 
55    // Needed by compilers not implementing the resolution to DR45. For reference,
56    // see http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#45.
57    friend struct data;
58 };
59 
60 #ifdef BOOST_MSVC
61 #pragma warning(push)
62 #pragma warning(disable: 4702)
63 #endif
64 template <class Key, class Object>
get(const Key & k,size_type l_max_cache_size)65 boost::shared_ptr<Object const> object_cache<Key, Object>::get(const Key& k, size_type l_max_cache_size)
66 {
67 #ifdef BOOST_HAS_THREADS
68    static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT;
69    boost::static_mutex::scoped_lock l(mut);
70    if (l)
71    {
72       return do_get(k, l_max_cache_size);
73    }
74    //
75    // what do we do if the lock fails?
76    // for now just throw, but we should never really get here...
77    //
78    ::boost::throw_exception(std::runtime_error("Error in thread safety code: could not acquire a lock"));
79 #if defined(BOOST_NO_UNREACHABLE_RETURN_DETECTION) || defined(BOOST_NO_EXCEPTIONS)
80    return boost::shared_ptr<Object>();
81 #endif
82 #else
83    return do_get(k, l_max_cache_size);
84 #endif
85 }
86 #ifdef BOOST_MSVC
87 #pragma warning(pop)
88 #endif
89 
90 template <class Key, class Object>
do_get(const Key & k,size_type l_max_cache_size)91 boost::shared_ptr<Object const> object_cache<Key, Object>::do_get(const Key& k, size_type l_max_cache_size)
92 {
93    typedef typename object_cache<Key, Object>::data object_data;
94    typedef typename map_type::size_type map_size_type;
95    static object_data s_data;
96 
97    //
98    // see if the object is already in the cache:
99    //
100    map_iterator mpos = s_data.index.find(k);
101    if(mpos != s_data.index.end())
102    {
103       //
104       // Eureka!
105       // We have a cached item, bump it up the list and return it:
106       //
107       if(--(s_data.cont.end()) != mpos->second)
108       {
109          // splice out the item we want to move:
110          list_type temp;
111          temp.splice(temp.end(), s_data.cont, mpos->second);
112          // and now place it at the end of the list:
113          s_data.cont.splice(s_data.cont.end(), temp, temp.begin());
114          BOOST_REGEX_ASSERT(*(s_data.cont.back().second) == k);
115          // update index with new position:
116          mpos->second = --(s_data.cont.end());
117          BOOST_REGEX_ASSERT(&(mpos->first) == mpos->second->second);
118          BOOST_REGEX_ASSERT(&(mpos->first) == s_data.cont.back().second);
119       }
120       return s_data.cont.back().first;
121    }
122    //
123    // if we get here then the item is not in the cache,
124    // so create it:
125    //
126    boost::shared_ptr<Object const> result(new Object(k));
127    //
128    // Add it to the list, and index it:
129    //
130    s_data.cont.push_back(value_type(result, static_cast<Key const*>(0)));
131    s_data.index.insert(std::make_pair(k, --(s_data.cont.end())));
132    s_data.cont.back().second = &(s_data.index.find(k)->first);
133    map_size_type s = s_data.index.size();
134    BOOST_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
135    BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
136    BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
137    if(s > l_max_cache_size)
138    {
139       //
140       // We have too many items in the list, so we need to start
141       // popping them off the back of the list, but only if they're
142       // being held uniquely by us:
143       //
144       list_iterator pos = s_data.cont.begin();
145       list_iterator last = s_data.cont.end();
146       while((pos != last) && (s > l_max_cache_size))
147       {
148          if(pos->first.unique())
149          {
150             list_iterator condemmed(pos);
151             ++pos;
152             // now remove the items from our containers,
153             // then order has to be as follows:
154             BOOST_REGEX_ASSERT(s_data.index.find(*(condemmed->second)) != s_data.index.end());
155             s_data.index.erase(*(condemmed->second));
156             s_data.cont.erase(condemmed);
157             --s;
158          }
159          else
160             ++pos;
161       }
162       BOOST_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
163       BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
164       BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
165    }
166    return result;
167 }
168 
169 }
170 
171 #endif
172