object_cache.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/regex/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. #ifdef BOOST_MSVC
  52. #pragma warning(push)
  53. #pragma warning(disable: 4702)
  54. #endif
  55. template <class Key, class Object>
  56. boost::shared_ptr<Object const> object_cache<Key, Object>::get(const Key& k, size_type l_max_cache_size)
  57. {
  58. #ifdef BOOST_HAS_THREADS
  59. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT;
  60. boost::static_mutex::scoped_lock l(mut);
  61. if (l)
  62. {
  63. return do_get(k, l_max_cache_size);
  64. }
  65. //
  66. // what do we do if the lock fails?
  67. // for now just throw, but we should never really get here...
  68. //
  69. ::boost::throw_exception(std::runtime_error("Error in thread safety code: could not acquire a lock"));
  70. #if defined(BOOST_NO_UNREACHABLE_RETURN_DETECTION) || defined(BOOST_NO_EXCEPTIONS)
  71. return boost::shared_ptr<Object>();
  72. #endif
  73. #else
  74. return do_get(k, l_max_cache_size);
  75. #endif
  76. }
  77. #ifdef BOOST_MSVC
  78. #pragma warning(pop)
  79. #endif
  80. template <class Key, class Object>
  81. boost::shared_ptr<Object const> object_cache<Key, Object>::do_get(const Key& k, size_type l_max_cache_size)
  82. {
  83. typedef typename object_cache<Key, Object>::data object_data;
  84. typedef typename map_type::size_type map_size_type;
  85. static object_data s_data;
  86. //
  87. // see if the object is already in the cache:
  88. //
  89. map_iterator mpos = s_data.index.find(k);
  90. if(mpos != s_data.index.end())
  91. {
  92. //
  93. // Eureka!
  94. // We have a cached item, bump it up the list and return it:
  95. //
  96. if(--(s_data.cont.end()) != mpos->second)
  97. {
  98. // splice out the item we want to move:
  99. list_type temp;
  100. temp.splice(temp.end(), s_data.cont, mpos->second);
  101. // and now place it at the end of the list:
  102. s_data.cont.splice(s_data.cont.end(), temp, temp.begin());
  103. BOOST_REGEX_ASSERT(*(s_data.cont.back().second) == k);
  104. // update index with new position:
  105. mpos->second = --(s_data.cont.end());
  106. BOOST_REGEX_ASSERT(&(mpos->first) == mpos->second->second);
  107. BOOST_REGEX_ASSERT(&(mpos->first) == s_data.cont.back().second);
  108. }
  109. return s_data.cont.back().first;
  110. }
  111. //
  112. // if we get here then the item is not in the cache,
  113. // so create it:
  114. //
  115. boost::shared_ptr<Object const> result(new Object(k));
  116. //
  117. // Add it to the list, and index it:
  118. //
  119. s_data.cont.push_back(value_type(result, static_cast<Key const*>(0)));
  120. s_data.index.insert(std::make_pair(k, --(s_data.cont.end())));
  121. s_data.cont.back().second = &(s_data.index.find(k)->first);
  122. map_size_type s = s_data.index.size();
  123. BOOST_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
  124. BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  125. BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
  126. if(s > l_max_cache_size)
  127. {
  128. //
  129. // We have too many items in the list, so we need to start
  130. // popping them off the back of the list, but only if they're
  131. // being held uniquely by us:
  132. //
  133. list_iterator pos = s_data.cont.begin();
  134. list_iterator last = s_data.cont.end();
  135. while((pos != last) && (s > l_max_cache_size))
  136. {
  137. if(pos->first.unique())
  138. {
  139. list_iterator condemmed(pos);
  140. ++pos;
  141. // now remove the items from our containers,
  142. // then order has to be as follows:
  143. BOOST_REGEX_ASSERT(s_data.index.find(*(condemmed->second)) != s_data.index.end());
  144. s_data.index.erase(*(condemmed->second));
  145. s_data.cont.erase(condemmed);
  146. --s;
  147. }
  148. else
  149. ++pos;
  150. }
  151. BOOST_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
  152. BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  153. BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
  154. }
  155. return result;
  156. }
  157. }
  158. #endif