flyweight_core.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Copyright 2006-2018 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/flyweight for library home page.
  7. */
  8. #ifndef BOOST_FLYWEIGHT_DETAIL_FLYWEIGHT_CORE_HPP
  9. #define BOOST_FLYWEIGHT_DETAIL_FLYWEIGHT_CORE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/detail/no_exceptions_support.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/flyweight/detail/perfect_fwd.hpp>
  17. #include <boost/mpl/apply.hpp>
  18. #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400))
  19. #pragma warning(push)
  20. #pragma warning(disable:4101) /* unreferenced local vars */
  21. #endif
  22. /* flyweight_core provides the inner implementation of flyweight<> by
  23. * weaving together a value policy, a flyweight factory, a holder for the
  24. * factory,a tracking policy and a locking policy.
  25. */
  26. namespace boost{
  27. namespace flyweights{
  28. namespace detail{
  29. template<
  30. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  31. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  32. >
  33. class flyweight_core;
  34. template<
  35. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  36. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  37. >
  38. struct flyweight_core_tracking_helper
  39. {
  40. private:
  41. typedef flyweight_core<
  42. ValuePolicy,Tag,TrackingPolicy,
  43. FactorySpecifier,LockingPolicy,
  44. HolderSpecifier
  45. > core;
  46. typedef typename core::handle_type handle_type;
  47. typedef typename core::entry_type entry_type;
  48. public:
  49. static const entry_type& entry(const handle_type& h)
  50. {
  51. return core::entry(h);
  52. }
  53. template<typename Checker>
  54. static void erase(const handle_type& h,Checker chk)
  55. {
  56. typedef typename core::lock_type lock_type;
  57. core::init();
  58. lock_type lock(core::mutex());(void)lock;
  59. if(chk(h))core::factory().erase(h);
  60. }
  61. };
  62. template<
  63. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  64. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  65. >
  66. class flyweight_core
  67. {
  68. public:
  69. typedef typename ValuePolicy::key_type key_type;
  70. typedef typename ValuePolicy::value_type value_type;
  71. typedef typename ValuePolicy::rep_type rep_type;
  72. typedef typename mpl::apply2<
  73. typename TrackingPolicy::entry_type,
  74. rep_type,
  75. key_type
  76. >::type entry_type;
  77. typedef typename mpl::apply2<
  78. FactorySpecifier,
  79. entry_type,
  80. key_type
  81. >::type factory_type;
  82. typedef typename factory_type::handle_type base_handle_type;
  83. typedef typename mpl::apply2<
  84. typename TrackingPolicy::handle_type,
  85. base_handle_type,
  86. flyweight_core_tracking_helper<
  87. ValuePolicy,Tag,TrackingPolicy,
  88. FactorySpecifier,LockingPolicy,
  89. HolderSpecifier
  90. >
  91. >::type handle_type;
  92. typedef typename LockingPolicy::mutex_type mutex_type;
  93. typedef typename LockingPolicy::lock_type lock_type;
  94. static bool init()
  95. {
  96. if(static_initializer)return true;
  97. else{
  98. holder_arg& a=holder_type::get();
  99. static_factory_ptr=&a.factory;
  100. static_mutex_ptr=&a.mutex;
  101. static_initializer=(static_factory_ptr!=0);
  102. return static_initializer;
  103. }
  104. }
  105. /* insert overloads*/
  106. #define BOOST_FLYWEIGHT_PERFECT_FWD_INSERT_BODY(args) \
  107. { \
  108. return insert_rep(rep_type(BOOST_FLYWEIGHT_FORWARD(args))); \
  109. }
  110. BOOST_FLYWEIGHT_PERFECT_FWD(
  111. static handle_type insert,
  112. BOOST_FLYWEIGHT_PERFECT_FWD_INSERT_BODY)
  113. #undef BOOST_FLYWEIGHT_PERFECT_FWD_INSERT_BODY
  114. static handle_type insert(const value_type& x){return insert_value(x);}
  115. static handle_type insert(value_type& x){return insert_value(x);}
  116. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  117. static handle_type insert(const value_type&& x){return insert_value(x);}
  118. static handle_type insert(value_type&& x){return insert_value(std::move(x));}
  119. #endif
  120. static const entry_type& entry(const base_handle_type& h)
  121. {
  122. return factory().entry(h);
  123. }
  124. static const value_type& value(const handle_type& h)
  125. {
  126. return static_cast<const rep_type&>(entry(h));
  127. }
  128. static const key_type& key(const handle_type& h)
  129. {
  130. return static_cast<const rep_type&>(entry(h));
  131. }
  132. static factory_type& factory()
  133. {
  134. return *static_factory_ptr;
  135. }
  136. static mutex_type& mutex()
  137. {
  138. return *static_mutex_ptr;
  139. }
  140. private:
  141. struct holder_arg
  142. {
  143. factory_type factory;
  144. mutex_type mutex;
  145. };
  146. typedef typename mpl::apply1<
  147. HolderSpecifier,
  148. holder_arg
  149. >::type holder_type;
  150. static handle_type insert_rep(const rep_type& x)
  151. {
  152. init();
  153. entry_type e(x);
  154. lock_type lock(mutex());(void)lock;
  155. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  156. base_handle_type h(factory().insert(std::move(e)));
  157. #else
  158. base_handle_type h(factory().insert(e));
  159. #endif
  160. BOOST_TRY{
  161. ValuePolicy::construct_value(
  162. static_cast<const rep_type&>(entry(h)));
  163. }
  164. BOOST_CATCH(...){
  165. factory().erase(h);
  166. BOOST_RETHROW;
  167. }
  168. BOOST_CATCH_END
  169. return static_cast<handle_type>(h);
  170. }
  171. static handle_type insert_value(const value_type& x)
  172. {
  173. init();
  174. entry_type e((rep_type(x)));
  175. lock_type lock(mutex());(void)lock;
  176. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  177. base_handle_type h(factory().insert(std::move(e)));
  178. #else
  179. base_handle_type h(factory().insert(e));
  180. #endif
  181. BOOST_TRY{
  182. ValuePolicy::copy_value(
  183. static_cast<const rep_type&>(entry(h)));
  184. }
  185. BOOST_CATCH(...){
  186. factory().erase(h);
  187. BOOST_RETHROW;
  188. }
  189. BOOST_CATCH_END
  190. return static_cast<handle_type>(h);
  191. }
  192. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  193. static handle_type insert_rep(rep_type&& x)
  194. {
  195. init();
  196. entry_type e(std::move(x));
  197. lock_type lock(mutex());(void)lock;
  198. base_handle_type h(factory().insert(std::move(e)));
  199. BOOST_TRY{
  200. ValuePolicy::construct_value(
  201. static_cast<const rep_type&>(entry(h)));
  202. }
  203. BOOST_CATCH(...){
  204. factory().erase(h);
  205. BOOST_RETHROW;
  206. }
  207. BOOST_CATCH_END
  208. return static_cast<handle_type>(h);
  209. }
  210. static handle_type insert_value(value_type&& x)
  211. {
  212. init();
  213. entry_type e(rep_type(std::move(x)));
  214. lock_type lock(mutex());(void)lock;
  215. base_handle_type h(factory().insert(std::move(e)));
  216. BOOST_TRY{
  217. ValuePolicy::move_value(
  218. static_cast<const rep_type&>(entry(h)));
  219. }
  220. BOOST_CATCH(...){
  221. factory().erase(h);
  222. BOOST_RETHROW;
  223. }
  224. BOOST_CATCH_END
  225. return static_cast<handle_type>(h);
  226. }
  227. #endif
  228. static bool static_initializer;
  229. static factory_type* static_factory_ptr;
  230. static mutex_type* static_mutex_ptr;
  231. };
  232. template<
  233. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  234. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  235. >
  236. bool
  237. flyweight_core<
  238. ValuePolicy,Tag,TrackingPolicy,
  239. FactorySpecifier,LockingPolicy,HolderSpecifier>::static_initializer=
  240. flyweight_core<
  241. ValuePolicy,Tag,TrackingPolicy,
  242. FactorySpecifier,LockingPolicy,HolderSpecifier>::init();
  243. template<
  244. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  245. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  246. >
  247. typename flyweight_core<
  248. ValuePolicy,Tag,TrackingPolicy,
  249. FactorySpecifier,LockingPolicy,HolderSpecifier>::factory_type*
  250. flyweight_core<
  251. ValuePolicy,Tag,TrackingPolicy,
  252. FactorySpecifier,LockingPolicy,HolderSpecifier>::static_factory_ptr=0;
  253. template<
  254. typename ValuePolicy,typename Tag,typename TrackingPolicy,
  255. typename FactorySpecifier,typename LockingPolicy,typename HolderSpecifier
  256. >
  257. typename flyweight_core<
  258. ValuePolicy,Tag,TrackingPolicy,
  259. FactorySpecifier,LockingPolicy,HolderSpecifier>::mutex_type*
  260. flyweight_core<
  261. ValuePolicy,Tag,TrackingPolicy,
  262. FactorySpecifier,LockingPolicy,HolderSpecifier>::static_mutex_ptr=0;
  263. } /* namespace flyweights::detail */
  264. } /* namespace flyweights */
  265. } /* namespace boost */
  266. #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400))
  267. #pragma warning(pop)
  268. #endif
  269. #endif