object_cache.hpp 4.9 KB

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