flyweight.hpp 14 KB

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