object_cache.hpp 5.0 KB

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