flyweight.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Flyweight class.
  2. *
  3. * Copyright 2006-2023 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/flyweight for library home page.
  9. */
  10. #ifndef BOOST_FLYWEIGHT_FLYWEIGHT_HPP
  11. #define BOOST_FLYWEIGHT_FLYWEIGHT_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  16. #include <algorithm>
  17. #include <boost/core/addressof.hpp>
  18. #include <boost/core/invoke_swap.hpp>
  19. #include <boost/detail/workaround.hpp>
  20. #include <boost/flyweight/detail/default_value_policy.hpp>
  21. #include <boost/flyweight/detail/flyweight_core.hpp>
  22. #include <boost/flyweight/detail/perfect_fwd.hpp>
  23. #include <boost/flyweight/factory_tag.hpp>
  24. #include <boost/flyweight/flyweight_fwd.hpp>
  25. #include <boost/flyweight/locking_tag.hpp>
  26. #include <boost/flyweight/simple_locking_fwd.hpp>
  27. #include <boost/flyweight/static_holder_fwd.hpp>
  28. #include <boost/flyweight/hashed_factory_fwd.hpp>
  29. #include <boost/flyweight/holder_tag.hpp>
  30. #include <boost/flyweight/refcounted_fwd.hpp>
  31. #include <boost/flyweight/tag.hpp>
  32. #include <boost/flyweight/tracking_tag.hpp>
  33. #include <boost/mpl/assert.hpp>
  34. #include <boost/mpl/if.hpp>
  35. #include <boost/mpl/not.hpp>
  36. #include <boost/mpl/or.hpp>
  37. #include <boost/parameter/binding.hpp>
  38. #include <boost/type_traits/is_same.hpp>
  39. #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  40. #include <boost/core/enable_if.hpp>
  41. #include <boost/type_traits/is_convertible.hpp>
  42. #include <initializer_list>
  43. #endif
  44. #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400))
  45. #pragma warning(push)
  46. #pragma warning(disable:4520) /* multiple default ctors */
  47. #pragma warning(disable:4521) /* multiple copy ctors */
  48. #endif
  49. namespace boost{
  50. namespace flyweights{
  51. namespace detail{
  52. /* Used for the detection of unmatched template args in a
  53. * flyweight instantiation.
  54. */
  55. struct unmatched_arg;
  56. /* Boost.Parameter structures for use in flyweight.
  57. * NB: these types are derived from instead of typedef'd to force their
  58. * instantiation, which solves http://bugs.sun.com/view_bug.do?bug_id=6782987
  59. * as found out by Simon Atanasyan.
  60. */
  61. struct flyweight_signature:
  62. parameter::parameters<
  63. parameter::optional<
  64. parameter::deduced<tag<> >,
  65. detail::is_tag<boost::mpl::_>
  66. >,
  67. parameter::optional<
  68. parameter::deduced<tracking<> >,
  69. is_tracking<boost::mpl::_>
  70. >,
  71. parameter::optional<
  72. parameter::deduced<factory<> >,
  73. is_factory<boost::mpl::_>
  74. >,
  75. parameter::optional<
  76. parameter::deduced<locking<> >,
  77. is_locking<boost::mpl::_>
  78. >,
  79. parameter::optional<
  80. parameter::deduced<holder<> >,
  81. is_holder<boost::mpl::_>
  82. >
  83. >
  84. {};
  85. struct flyweight_unmatched_signature:
  86. parameter::parameters<
  87. parameter::optional<
  88. parameter::deduced<
  89. detail::unmatched_arg
  90. >,
  91. mpl::not_<
  92. mpl::or_<
  93. detail::is_tag<boost::mpl::_>,
  94. is_tracking<boost::mpl::_>,
  95. is_factory<boost::mpl::_>,
  96. is_locking<boost::mpl::_>,
  97. is_holder<boost::mpl::_>
  98. >
  99. >
  100. >
  101. >
  102. {};
  103. } /* namespace flyweights::detail */
  104. template<
  105. typename T,
  106. typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5
  107. >
  108. class flyweight
  109. {
  110. private:
  111. typedef typename mpl::if_<
  112. detail::is_value<T>,
  113. T,
  114. detail::default_value_policy<T>
  115. >::type value_policy;
  116. typedef typename detail::
  117. flyweight_signature::bind<
  118. Arg1,Arg2,Arg3,Arg4,Arg5
  119. >::type args;
  120. typedef typename parameter::binding<
  121. args,tag<>,mpl::na
  122. >::type tag_type;
  123. typedef typename parameter::binding<
  124. args,tracking<>,refcounted
  125. >::type tracking_policy;
  126. typedef typename parameter::binding<
  127. args,factory<>,hashed_factory<>
  128. >::type factory_specifier;
  129. typedef typename parameter::binding<
  130. args,locking<>,simple_locking
  131. >::type locking_policy;
  132. typedef typename parameter::binding<
  133. args,holder<>,static_holder
  134. >::type holder_specifier;
  135. typedef typename detail::
  136. flyweight_unmatched_signature::bind<
  137. Arg1,Arg2,Arg3,Arg4,Arg5
  138. >::type unmatched_args;
  139. typedef typename parameter::binding<
  140. unmatched_args,detail::unmatched_arg,
  141. detail::unmatched_arg
  142. >::type unmatched_arg_detected;
  143. /* You have passed a type in the specification of a flyweight type that
  144. * could not be interpreted as a valid argument.
  145. */
  146. BOOST_MPL_ASSERT_MSG(
  147. (is_same<unmatched_arg_detected,detail::unmatched_arg>::value),
  148. INVALID_ARGUMENT_TO_FLYWEIGHT,
  149. (flyweight));
  150. typedef detail::flyweight_core<
  151. value_policy,tag_type,tracking_policy,
  152. factory_specifier,locking_policy,
  153. holder_specifier
  154. > core;
  155. typedef typename core::handle_type handle_type;
  156. public:
  157. typedef typename value_policy::key_type key_type;
  158. typedef typename value_policy::value_type value_type;
  159. /* static data initialization */
  160. static bool init(){return core::init();}
  161. class initializer
  162. {
  163. public:
  164. initializer():b(init()){}
  165. private:
  166. bool b;
  167. };
  168. /* construct/copy/destroy */
  169. flyweight():h(core::insert()){}
  170. #define BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY(args) \
  171. :h(core::insert(BOOST_FLYWEIGHT_FORWARD(args))){}
  172. BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(
  173. explicit flyweight,
  174. BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY)
  175. #undef BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY
  176. #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  177. template<typename V>
  178. flyweight(
  179. std::initializer_list<V> list,
  180. typename boost::enable_if<
  181. boost::is_convertible<std::initializer_list<V>,key_type> >::type* =0):
  182. h(core::insert(list)){}
  183. #endif
  184. flyweight(const flyweight& x):h(x.h){}
  185. flyweight(flyweight& x):h(x.h){}
  186. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  187. flyweight(const flyweight&& x):h(x.h){}
  188. flyweight(flyweight&& x):h(x.h){}
  189. #endif
  190. #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  191. template<typename V>
  192. typename boost::enable_if<
  193. boost::is_convertible<std::initializer_list<V>,key_type>,flyweight&>::type
  194. operator=(std::initializer_list<V> list)
  195. {
  196. return operator=(flyweight(list));
  197. }
  198. #endif
  199. flyweight& operator=(const flyweight& x){h=x.h;return *this;}
  200. flyweight& operator=(const value_type& x){return operator=(flyweight(x));}
  201. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  202. flyweight& operator=(value_type&& x)
  203. {
  204. return operator=(flyweight(std::move(x)));
  205. }
  206. #endif
  207. /* convertibility to underlying type */
  208. const key_type& get_key()const{return core::key(h);}
  209. const value_type& get()const{return core::value(h);}
  210. const value_type& operator*()const{return get();}
  211. operator const value_type&()const{return get();}
  212. const value_type* operator->()const{return boost::addressof(get());}
  213. /* exact type equality */
  214. friend bool operator==(const flyweight& x,const flyweight& y)
  215. {
  216. return &x.get()==&y.get();
  217. }
  218. /* modifiers */
  219. void swap(flyweight& x){boost::core::invoke_swap(h,x.h);}
  220. private:
  221. handle_type h;
  222. };
  223. #define BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(n) \
  224. typename Arg##n##1,typename Arg##n##2,typename Arg##n##3, \
  225. typename Arg##n##4,typename Arg##n##5
  226. #define BOOST_FLYWEIGHT_TEMPL_ARGS(n) \
  227. Arg##n##1,Arg##n##2,Arg##n##3,Arg##n##4,Arg##n##5
  228. /* Comparison. Unlike exact type comparison defined above, intertype
  229. * comparison just forwards to the underlying objects.
  230. */
  231. template<
  232. typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1),
  233. typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2)
  234. >
  235. bool operator==(
  236. const flyweight<T1,BOOST_FLYWEIGHT_TEMPL_ARGS(1)>& x,
  237. const flyweight<T2,BOOST_FLYWEIGHT_TEMPL_ARGS(2)>& y)
  238. {
  239. return x.get()==y.get();
  240. }
  241. template<
  242. typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1),
  243. typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2)
  244. >
  245. bool operator<(
  246. const flyweight<T1,BOOST_FLYWEIGHT_TEMPL_ARGS(1)>& x,
  247. const flyweight<T2,BOOST_FLYWEIGHT_TEMPL_ARGS(2)>& y)
  248. {
  249. return x.get()<y.get();
  250. }
  251. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  252. template<
  253. typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1),
  254. typename T2
  255. >
  256. bool operator==(
  257. const flyweight<T1,BOOST_FLYWEIGHT_TEMPL_ARGS(1)>& x,const T2& y)
  258. {
  259. return x.get()==y;
  260. }
  261. template<
  262. typename T1,
  263. typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2)
  264. >
  265. bool operator==(
  266. const T1& x,const flyweight<T2,BOOST_FLYWEIGHT_TEMPL_ARGS(2)>& y)
  267. {
  268. return x==y.get();
  269. }
  270. template<
  271. typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1),
  272. typename T2
  273. >
  274. bool operator<(
  275. const flyweight<T1,BOOST_FLYWEIGHT_TEMPL_ARGS(1)>& x,const T2& y)
  276. {
  277. return x.get()<y;
  278. }
  279. template<
  280. typename T1,
  281. typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2)
  282. >
  283. bool operator<(
  284. const T1& x,const flyweight<T2,BOOST_FLYWEIGHT_TEMPL_ARGS(2)>& y)
  285. {
  286. return x<y.get();
  287. }
  288. #endif /* !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) */
  289. /* rest of comparison operators */
  290. #define BOOST_FLYWEIGHT_COMPLETE_COMP_OPS(t,a1,a2) \
  291. template<t> \
  292. inline bool operator!=(const a1& x,const a2& y) \
  293. { \
  294. return !(x==y); \
  295. } \
  296. \
  297. template<t> \
  298. inline bool operator>(const a1& x,const a2& y) \
  299. { \
  300. return y<x; \
  301. } \
  302. \
  303. template<t> \
  304. inline bool operator>=(const a1& x,const a2& y) \
  305. { \
  306. return !(x<y); \
  307. } \
  308. \
  309. template<t> \
  310. inline bool operator<=(const a1& x,const a2& y) \
  311. { \
  312. return !(y<x); \
  313. }
  314. BOOST_FLYWEIGHT_COMPLETE_COMP_OPS(
  315. typename T1 BOOST_PP_COMMA()
  316. BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1) BOOST_PP_COMMA()
  317. typename T2 BOOST_PP_COMMA()
  318. BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2),
  319. flyweight<
  320. T1 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(1)
  321. >,
  322. flyweight<
  323. T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2)
  324. >)
  325. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  326. BOOST_FLYWEIGHT_COMPLETE_COMP_OPS(
  327. typename T1 BOOST_PP_COMMA()
  328. BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1) BOOST_PP_COMMA()
  329. typename T2,
  330. flyweight<
  331. T1 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(1)
  332. >,
  333. T2)
  334. BOOST_FLYWEIGHT_COMPLETE_COMP_OPS(
  335. typename T1 BOOST_PP_COMMA()
  336. typename T2 BOOST_PP_COMMA()
  337. BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2),
  338. T1,
  339. flyweight<
  340. T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2)
  341. >)
  342. #endif /* !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) */
  343. /* specialized algorithms */
  344. template<typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_)>
  345. void swap(
  346. flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)>& x,
  347. flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)>& y)
  348. {
  349. x.swap(y);
  350. }
  351. template<
  352. BOOST_TEMPLATED_STREAM_ARGS(ElemType,Traits)
  353. BOOST_TEMPLATED_STREAM_COMMA
  354. typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_)
  355. >
  356. BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& operator<<(
  357. BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& out,
  358. const flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)>& x)
  359. {
  360. return out<<x.get();
  361. }
  362. template<
  363. BOOST_TEMPLATED_STREAM_ARGS(ElemType,Traits)
  364. BOOST_TEMPLATED_STREAM_COMMA
  365. typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_)
  366. >
  367. BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& operator>>(
  368. BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& in,
  369. flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)>& x)
  370. {
  371. typedef typename flyweight<
  372. T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)
  373. >::value_type value_type;
  374. /* value_type need not be default ctble but must be copy ctble */
  375. value_type t(x.get());
  376. in>>t;
  377. x=t;
  378. return in;
  379. }
  380. } /* namespace flyweights */
  381. } /* namespace boost */
  382. #if !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT)
  383. /* hash support */
  384. #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  385. namespace std{
  386. template<typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_)>
  387. BOOST_FLYWEIGHT_STD_HASH_STRUCT_KEYWORD
  388. hash<boost::flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)> >
  389. {
  390. public:
  391. typedef std::size_t result_type;
  392. typedef boost::flyweight<
  393. T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)> argument_type;
  394. result_type operator()(const argument_type& x)const
  395. {
  396. typedef typename argument_type::value_type value_type;
  397. std::hash<const value_type*> h;
  398. return h(&x.get());
  399. }
  400. };
  401. } /* namespace std */
  402. #endif /* !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) */
  403. namespace boost{
  404. #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  405. namespace flyweights{
  406. #endif
  407. template<typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_)>
  408. std::size_t hash_value(const flyweight<T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)>& x)
  409. {
  410. typedef typename flyweight<
  411. T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)
  412. >::value_type value_type;
  413. boost::hash<const value_type*> h;
  414. return h(&x.get());
  415. }
  416. #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  417. } /* namespace flyweights */
  418. #endif
  419. } /* namespace boost */
  420. #endif /* !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT) */
  421. #undef BOOST_FLYWEIGHT_COMPLETE_COMP_OPS
  422. #undef BOOST_FLYWEIGHT_TEMPL_ARGS
  423. #undef BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS
  424. #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400))
  425. #pragma warning(pop)
  426. #endif
  427. #endif