flyweight.hpp 12 KB

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