object_cache.hpp 4.9 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 <boost/regex/config.hpp>
  20. #ifndef BOOST_REGEX_AS_MODULE
  21. #include <memory>
  22. #include <map>
  23. #include <list>
  24. #include <stdexcept>
  25. #include <string>
  26. #ifdef BOOST_HAS_THREADS
  27. #include <mutex>
  28. #endif
  29. #endif
  30. namespace boost{
  31. template <class Key, class Object>
  32. class object_cache
  33. {
  34. public:
  35. typedef std::pair< ::std::shared_ptr<Object const>, Key const*> value_type;
  36. typedef std::list<value_type> list_type;
  37. typedef typename list_type::iterator list_iterator;
  38. typedef std::map<Key, list_iterator> map_type;
  39. typedef typename map_type::iterator map_iterator;
  40. typedef typename list_type::size_type size_type;
  41. static std::shared_ptr<Object const> get(const Key& k, size_type l_max_cache_size);
  42. private:
  43. static std::shared_ptr<Object const> do_get(const Key& k, size_type l_max_cache_size);
  44. struct data
  45. {
  46. list_type cont;
  47. map_type index;
  48. };
  49. // Needed by compilers not implementing the resolution to DR45. For reference,
  50. // see http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#45.
  51. friend struct data;
  52. };
  53. #ifdef BOOST_REGEX_MSVC
  54. #pragma warning(push)
  55. #pragma warning(disable: 4702)
  56. #endif
  57. template <class Key, class Object>
  58. std::shared_ptr<Object const> object_cache<Key, Object>::get(const Key& k, size_type l_max_cache_size)
  59. {
  60. #ifdef BOOST_HAS_THREADS
  61. static std::mutex mut;
  62. std::lock_guard<std::mutex> l(mut);
  63. return do_get(k, l_max_cache_size);
  64. #else
  65. return do_get(k, l_max_cache_size);
  66. #endif
  67. }
  68. #ifdef BOOST_REGEX_MSVC
  69. #pragma warning(pop)
  70. #endif
  71. template <class Key, class Object>
  72. std::shared_ptr<Object const> object_cache<Key, Object>::do_get(const Key& k, size_type l_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_REGEX_ASSERT(*(s_data.cont.back().second) == k);
  95. // update index with new position:
  96. mpos->second = --(s_data.cont.end());
  97. BOOST_REGEX_ASSERT(&(mpos->first) == mpos->second->second);
  98. BOOST_REGEX_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. std::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_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
  115. BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  116. BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
  117. if(s > l_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 > l_max_cache_size))
  127. {
  128. if(pos->first.use_count() == 1)
  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_REGEX_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_REGEX_ASSERT(s_data.index[k]->first.get() == result.get());
  143. BOOST_REGEX_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
  144. BOOST_REGEX_ASSERT(s_data.index.find(k)->first == k);
  145. }
  146. return result;
  147. }
  148. }
  149. #endif