object_cache.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <boost/config.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <map>
  22. #include <list>
  23. #include <stdexcept>
  24. #include <string>
  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 l_max_cache_size);
  40. private:
  41. static boost::shared_ptr<Object const> do_get(const Key& k, size_type l_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 l_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, l_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. #if defined(BOOST_NO_UNREACHABLE_RETURN_DETECTION) || defined(BOOST_NO_EXCEPTIONS)
  67. return boost::shared_ptr<Object>();
  68. #endif
  69. #else
  70. return do_get(k, l_max_cache_size);
  71. #endif
  72. }
  73. template <class Key, class Object>
  74. boost::shared_ptr<Object const> object_cache<Key, Object>::do_get(const Key& k, size_type l_max_cache_size)
  75. {
  76. typedef typename object_cache<Key, Object>::data object_data;
  77. typedef typename map_type::size_type map_size_type;
  78. static object_data s_data;
  79. //
  80. // see if the object is already in the cache:
  81. //
  82. map_iterator mpos = s_data.index.find(k);
  83. if(mpos != s_data.index.end())
  84. {
  85. //
  86. // Eureka!
  87. // We have a cached item, bump it up the list and return it:
  88. //
  89. if(--(s_data.cont.end()) != mpos->second)
  90. {
  91. // splice out the item we want to move:
  92. list_type temp;
  93. temp.splice(temp.end(), s_data.cont, mpos->second);
  94. // and now place it at the end of the list:
  95. s_data.cont.splice(s_data.cont.end(), temp, temp.begin());
  96. BOOST_ASSERT(*(s_data.cont.back().second) == k);
  97. // update index with new position:
  98. mpos->second = --(s_data.cont.end());
  99. BOOST_ASSERT(&(mpos->first) == mpos->second->second);
  100. BOOST_ASSERT(&(mpos->first) == s_data.cont.back().second);
  101. }
  102. return s_data.cont.back().first;
  103. }
  104. //
  105. // if we get here then the item is not in the cache,
  106. // so create it:
  107. //
  108. boost::shared_ptr<Object const> result(new Object(k));
  109. //
  110. // Add it to the list, and index it:
  111. //
  112. s_data.cont.push_back(value_type(result, static_cast<Key const*>(0)));
  113. s_data.index.insert(std::make_pair(k, --(s_data.cont.end())));
  114. s_data.cont.back().second = &(s_data.index.find(k)->first);
  115. map_size_type s = s_data.index.size();
  116. BOOST_ASSERT(s_data.index[k]->first.get() == result.get());
  117. BOOST_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  118. BOOST_ASSERT(s_data.index.find(k)->first == k);
  119. if(s > l_max_cache_size)
  120. {
  121. //
  122. // We have too many items in the list, so we need to start
  123. // popping them off the back of the list, but only if they're
  124. // being held uniquely by us:
  125. //
  126. list_iterator pos = s_data.cont.begin();
  127. list_iterator last = s_data.cont.end();
  128. while((pos != last) && (s > l_max_cache_size))
  129. {
  130. if(pos->first.unique())
  131. {
  132. list_iterator condemmed(pos);
  133. ++pos;
  134. // now remove the items from our containers,
  135. // then order has to be as follows:
  136. BOOST_ASSERT(s_data.index.find(*(condemmed->second)) != s_data.index.end());
  137. s_data.index.erase(*(condemmed->second));
  138. s_data.cont.erase(condemmed);
  139. --s;
  140. }
  141. else
  142. ++pos;
  143. }
  144. BOOST_ASSERT(s_data.index[k]->first.get() == result.get());
  145. BOOST_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  146. BOOST_ASSERT(s_data.index.find(k)->first == k);
  147. }
  148. return result;
  149. }
  150. }
  151. #endif