light_function_pp.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. template<
  8. typename ResultT
  9. BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), typename ArgT)
  10. >
  11. class light_function< ResultT (BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), ArgT)) >
  12. {
  13. typedef light_function this_type;
  14. BOOST_COPYABLE_AND_MOVABLE(this_type)
  15. public:
  16. typedef ResultT result_type;
  17. private:
  18. struct impl_base
  19. {
  20. typedef result_type (*invoke_type)(impl_base* BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), ArgT));
  21. const invoke_type invoke;
  22. typedef impl_base* (*clone_type)(const impl_base*);
  23. const clone_type clone;
  24. typedef void (*destroy_type)(impl_base*);
  25. const destroy_type destroy;
  26. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  27. {
  28. }
  29. };
  30. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  31. template< typename FunT >
  32. class impl;
  33. template< typename FunT >
  34. friend class impl;
  35. #endif
  36. template< typename FunT >
  37. class impl :
  38. public impl_base
  39. {
  40. typedef impl< FunT > this_type;
  41. FunT m_Function;
  42. public:
  43. explicit impl(FunT const& fun) :
  44. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  45. m_Function(fun)
  46. {
  47. }
  48. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  49. explicit impl(FunT&& fun) :
  50. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  51. m_Function(fun)
  52. {
  53. }
  54. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  55. static void destroy_impl(impl_base* self)
  56. {
  57. delete static_cast< impl* >(self);
  58. }
  59. static impl_base* clone_impl(const impl_base* self)
  60. {
  61. return new impl(static_cast< const impl* >(self)->m_Function);
  62. }
  63. static result_type invoke_impl(impl_base* self BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg))
  64. {
  65. return static_cast< impl* >(self)->m_Function(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), arg));
  66. }
  67. };
  68. private:
  69. impl_base* m_pImpl;
  70. public:
  71. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  72. {
  73. }
  74. light_function(this_type const& that)
  75. {
  76. if (that.m_pImpl)
  77. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  78. else
  79. m_pImpl = NULL;
  80. }
  81. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  82. {
  83. m_pImpl = that.m_pImpl;
  84. that.m_pImpl = NULL;
  85. }
  86. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  87. {
  88. m_pImpl = that.m_pImpl;
  89. ((this_type&)that).m_pImpl = NULL;
  90. }
  91. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  92. template< typename FunT >
  93. light_function(FunT&& fun) :
  94. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  95. {
  96. }
  97. #else
  98. template< typename FunT >
  99. light_function(FunT const& fun, typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, int >::type = 0) :
  100. m_pImpl(new impl< FunT >(fun))
  101. {
  102. }
  103. template< typename FunT >
  104. light_function(rv< FunT > const& fun, typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, int >::type = 0) :
  105. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  106. {
  107. }
  108. #endif
  109. //! Constructor from NULL
  110. #if !defined(BOOST_NO_CXX11_NULLPTR)
  111. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  112. #else
  113. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  114. #endif
  115. : m_pImpl(NULL)
  116. {
  117. #if defined(BOOST_NO_CXX11_NULLPTR)
  118. BOOST_ASSERT(p == 0);
  119. #endif
  120. }
  121. ~light_function()
  122. {
  123. clear();
  124. }
  125. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  126. {
  127. this->swap(that);
  128. return *this;
  129. }
  130. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  131. {
  132. light_function tmp = that;
  133. this->swap(tmp);
  134. return *this;
  135. }
  136. //! Assignment of NULL
  137. #if !defined(BOOST_NO_CXX11_NULLPTR)
  138. light_function& operator= (std::nullptr_t)
  139. #else
  140. light_function& operator= (int p)
  141. #endif
  142. {
  143. #if defined(BOOST_NO_CXX11_NULLPTR)
  144. BOOST_ASSERT(p == 0);
  145. #endif
  146. clear();
  147. return *this;
  148. }
  149. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  150. template< typename FunT >
  151. light_function& operator= (FunT&& fun)
  152. {
  153. light_function tmp(boost::forward< FunT >(fun));
  154. this->swap(tmp);
  155. return *this;
  156. }
  157. #else
  158. template< typename FunT >
  159. typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, this_type& >::type
  160. operator= (FunT const& fun)
  161. {
  162. light_function tmp(fun);
  163. this->swap(tmp);
  164. return *this;
  165. }
  166. #endif
  167. result_type operator() (BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg)) const
  168. {
  169. return m_pImpl->invoke(m_pImpl BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), arg));
  170. }
  171. BOOST_EXPLICIT_OPERATOR_BOOL()
  172. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  173. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  174. void clear() BOOST_NOEXCEPT
  175. {
  176. if (m_pImpl)
  177. {
  178. m_pImpl->destroy(m_pImpl);
  179. m_pImpl = NULL;
  180. }
  181. }
  182. void swap(this_type& that) BOOST_NOEXCEPT
  183. {
  184. register impl_base* p = m_pImpl;
  185. m_pImpl = that.m_pImpl;
  186. that.m_pImpl = p;
  187. }
  188. };
  189. template<
  190. BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), typename ArgT)
  191. >
  192. class light_function< void (BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), ArgT)) >
  193. {
  194. typedef light_function this_type;
  195. BOOST_COPYABLE_AND_MOVABLE(this_type)
  196. public:
  197. typedef void result_type;
  198. private:
  199. struct impl_base
  200. {
  201. typedef void (*invoke_type)(impl_base* BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), ArgT));
  202. const invoke_type invoke;
  203. typedef impl_base* (*clone_type)(const impl_base*);
  204. const clone_type clone;
  205. typedef void (*destroy_type)(impl_base*);
  206. const destroy_type destroy;
  207. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  208. {
  209. }
  210. };
  211. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  212. template< typename FunT >
  213. class impl;
  214. template< typename FunT >
  215. friend class impl;
  216. #endif
  217. template< typename FunT >
  218. class impl :
  219. public impl_base
  220. {
  221. typedef impl< FunT > this_type;
  222. FunT m_Function;
  223. public:
  224. explicit impl(FunT const& fun) :
  225. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  226. m_Function(fun)
  227. {
  228. }
  229. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  230. explicit impl(FunT&& fun) :
  231. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  232. m_Function(fun)
  233. {
  234. }
  235. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  236. static void destroy_impl(impl_base* self)
  237. {
  238. delete static_cast< impl* >(self);
  239. }
  240. static impl_base* clone_impl(const impl_base* self)
  241. {
  242. return new impl(static_cast< const impl* >(self)->m_Function);
  243. }
  244. static result_type invoke_impl(impl_base* self BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg))
  245. {
  246. static_cast< impl* >(self)->m_Function(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), arg));
  247. }
  248. };
  249. private:
  250. impl_base* m_pImpl;
  251. public:
  252. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  253. {
  254. }
  255. light_function(this_type const& that)
  256. {
  257. if (that.m_pImpl)
  258. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  259. else
  260. m_pImpl = NULL;
  261. }
  262. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  263. {
  264. m_pImpl = that.m_pImpl;
  265. that.m_pImpl = NULL;
  266. }
  267. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  268. {
  269. m_pImpl = that.m_pImpl;
  270. ((this_type&)that).m_pImpl = NULL;
  271. }
  272. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  273. template< typename FunT >
  274. light_function(FunT&& fun) :
  275. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  276. {
  277. }
  278. #else
  279. template< typename FunT >
  280. light_function(FunT const& fun, typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, int >::type = 0) :
  281. m_pImpl(new impl< FunT >(fun))
  282. {
  283. }
  284. template< typename FunT >
  285. light_function(rv< FunT > const& fun, typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, int >::type = 0) :
  286. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  287. {
  288. }
  289. #endif
  290. //! Constructor from NULL
  291. #if !defined(BOOST_NO_CXX11_NULLPTR)
  292. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  293. #else
  294. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  295. #endif
  296. : m_pImpl(NULL)
  297. {
  298. #if defined(BOOST_NO_CXX11_NULLPTR)
  299. BOOST_ASSERT(p == 0);
  300. #endif
  301. }
  302. ~light_function()
  303. {
  304. clear();
  305. }
  306. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  307. {
  308. this->swap(that);
  309. return *this;
  310. }
  311. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  312. {
  313. light_function tmp = that;
  314. this->swap(tmp);
  315. return *this;
  316. }
  317. //! Assignment of NULL
  318. #if !defined(BOOST_NO_CXX11_NULLPTR)
  319. light_function& operator= (std::nullptr_t)
  320. #else
  321. light_function& operator= (int p)
  322. #endif
  323. {
  324. #if defined(BOOST_NO_CXX11_NULLPTR)
  325. BOOST_ASSERT(p == 0);
  326. #endif
  327. clear();
  328. return *this;
  329. }
  330. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  331. template< typename FunT >
  332. light_function& operator= (FunT&& fun)
  333. {
  334. light_function tmp(boost::forward< FunT >(fun));
  335. this->swap(tmp);
  336. return *this;
  337. }
  338. #else
  339. template< typename FunT >
  340. typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, this_type& >::type
  341. operator= (FunT const& fun)
  342. {
  343. light_function tmp(fun);
  344. this->swap(tmp);
  345. return *this;
  346. }
  347. #endif
  348. result_type operator() (BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg)) const
  349. {
  350. m_pImpl->invoke(m_pImpl BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), arg));
  351. }
  352. BOOST_EXPLICIT_OPERATOR_BOOL()
  353. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  354. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  355. void clear() BOOST_NOEXCEPT
  356. {
  357. if (m_pImpl)
  358. {
  359. m_pImpl->destroy(m_pImpl);
  360. m_pImpl = NULL;
  361. }
  362. }
  363. void swap(this_type& that) BOOST_NOEXCEPT
  364. {
  365. register impl_base* p = m_pImpl;
  366. m_pImpl = that.m_pImpl;
  367. that.m_pImpl = p;
  368. }
  369. };