light_function.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /*!
  8. * \file light_function.hpp
  9. * \author Andrey Semashev
  10. * \date 20.06.2010
  11. *
  12. * \brief This header is the Boost.Log library impl, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. *
  15. * The file contains a lightweight alternative of Boost.Function. It does not provide all
  16. * features of Boost.Function but doesn't introduce dependency on Boost.Bind.
  17. */
  18. #ifndef BOOST_LOG_DETAIL_LIGHT_FUNCTION_HPP_INCLUDED_
  19. #define BOOST_LOG_DETAIL_LIGHT_FUNCTION_HPP_INCLUDED_
  20. #include <cstddef>
  21. #include <boost/move/core.hpp>
  22. #include <boost/move/utility.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/utility/explicit_operator_bool.hpp>
  25. #include <boost/type_traits/remove_cv.hpp>
  26. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  27. #include <boost/preprocessor/iteration/iterate.hpp>
  28. #include <boost/preprocessor/repetition/enum_params.hpp>
  29. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  30. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  31. #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
  32. #endif
  33. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  34. #include <boost/utility/enable_if.hpp>
  35. #include <boost/type_traits/is_same.hpp>
  36. #include <boost/mpl/or.hpp>
  37. #else
  38. #include <boost/type_traits/remove_reference.hpp>
  39. #endif
  40. #if defined(BOOST_NO_CXX11_NULLPTR)
  41. #include <boost/assert.hpp>
  42. #endif
  43. #include <boost/log/detail/header.hpp>
  44. #ifdef BOOST_HAS_PRAGMA_ONCE
  45. #pragma once
  46. #endif
  47. #ifndef BOOST_LOG_LIGHT_FUNCTION_LIMIT
  48. #define BOOST_LOG_LIGHT_FUNCTION_LIMIT 2
  49. #endif
  50. namespace boost {
  51. BOOST_LOG_OPEN_NAMESPACE
  52. namespace aux {
  53. template< typename SignatureT >
  54. class light_function;
  55. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  56. template< typename ResultT, typename... ArgsT >
  57. class light_function< ResultT (ArgsT...) >
  58. {
  59. typedef light_function this_type;
  60. BOOST_COPYABLE_AND_MOVABLE(this_type)
  61. public:
  62. typedef ResultT result_type;
  63. private:
  64. struct impl_base
  65. {
  66. typedef result_type (*invoke_type)(impl_base*, ArgsT...);
  67. const invoke_type invoke;
  68. typedef impl_base* (*clone_type)(const impl_base*);
  69. const clone_type clone;
  70. typedef void (*destroy_type)(impl_base*);
  71. const destroy_type destroy;
  72. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  73. {
  74. }
  75. };
  76. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  77. template< typename FunT >
  78. class impl;
  79. template< typename FunT >
  80. friend class impl;
  81. #endif
  82. template< typename FunT >
  83. class impl :
  84. public impl_base
  85. {
  86. typedef impl< FunT > this_type;
  87. FunT m_Function;
  88. public:
  89. explicit impl(FunT const& fun) :
  90. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  91. m_Function(fun)
  92. {
  93. }
  94. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  95. explicit impl(FunT&& fun) :
  96. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  97. m_Function(fun)
  98. {
  99. }
  100. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  101. static void destroy_impl(impl_base* self)
  102. {
  103. delete static_cast< impl* >(self);
  104. }
  105. static impl_base* clone_impl(const impl_base* self)
  106. {
  107. return new impl(static_cast< const impl* >(self)->m_Function);
  108. }
  109. static result_type invoke_impl(impl_base* self, ArgsT... args)
  110. {
  111. return static_cast< impl* >(self)->m_Function(args...);
  112. }
  113. };
  114. private:
  115. impl_base* m_pImpl;
  116. public:
  117. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  118. {
  119. }
  120. light_function(this_type const& that)
  121. {
  122. if (that.m_pImpl)
  123. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  124. else
  125. m_pImpl = NULL;
  126. }
  127. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  128. {
  129. m_pImpl = that.m_pImpl;
  130. that.m_pImpl = NULL;
  131. }
  132. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  133. {
  134. m_pImpl = that.m_pImpl;
  135. ((this_type&)that).m_pImpl = NULL;
  136. }
  137. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  138. template< typename FunT >
  139. light_function(FunT&& fun) :
  140. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  141. {
  142. }
  143. #else
  144. template< typename FunT >
  145. light_function(FunT const& fun, typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, int >::type = 0) :
  146. m_pImpl(new impl< FunT >(fun))
  147. {
  148. }
  149. template< typename FunT >
  150. light_function(rv< FunT > const& fun, typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, int >::type = 0) :
  151. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  152. {
  153. }
  154. #endif
  155. //! Constructor from NULL
  156. #if !defined(BOOST_NO_CXX11_NULLPTR)
  157. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  158. #else
  159. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  160. #endif
  161. : m_pImpl(NULL)
  162. {
  163. #if defined(BOOST_NO_CXX11_NULLPTR)
  164. BOOST_ASSERT(p == 0);
  165. #endif
  166. }
  167. ~light_function()
  168. {
  169. clear();
  170. }
  171. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  172. {
  173. this->swap(that);
  174. return *this;
  175. }
  176. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  177. {
  178. light_function tmp(that);
  179. this->swap(tmp);
  180. return *this;
  181. }
  182. //! Assignment of NULL
  183. #if !defined(BOOST_NO_CXX11_NULLPTR)
  184. light_function& operator= (std::nullptr_t)
  185. #else
  186. light_function& operator= (int p)
  187. #endif
  188. {
  189. #if defined(BOOST_NO_CXX11_NULLPTR)
  190. BOOST_ASSERT(p == 0);
  191. #endif
  192. clear();
  193. return *this;
  194. }
  195. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  196. template< typename FunT >
  197. light_function& operator= (FunT&& fun)
  198. {
  199. light_function tmp(boost::forward< FunT >(fun));
  200. this->swap(tmp);
  201. return *this;
  202. }
  203. #else
  204. template< typename FunT >
  205. typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, this_type& >::type
  206. operator= (FunT const& fun)
  207. {
  208. light_function tmp(fun);
  209. this->swap(tmp);
  210. return *this;
  211. }
  212. #endif
  213. result_type operator() (ArgsT... args) const
  214. {
  215. return m_pImpl->invoke(m_pImpl, args...);
  216. }
  217. BOOST_EXPLICIT_OPERATOR_BOOL()
  218. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  219. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  220. void clear() BOOST_NOEXCEPT
  221. {
  222. if (m_pImpl)
  223. {
  224. m_pImpl->destroy(m_pImpl);
  225. m_pImpl = NULL;
  226. }
  227. }
  228. void swap(this_type& that) BOOST_NOEXCEPT
  229. {
  230. register impl_base* p = m_pImpl;
  231. m_pImpl = that.m_pImpl;
  232. that.m_pImpl = p;
  233. }
  234. };
  235. template< typename... ArgsT >
  236. class light_function< void (ArgsT...) >
  237. {
  238. typedef light_function this_type;
  239. BOOST_COPYABLE_AND_MOVABLE(this_type)
  240. public:
  241. typedef void result_type;
  242. private:
  243. struct impl_base
  244. {
  245. typedef void (*invoke_type)(impl_base*, ArgsT...);
  246. const invoke_type invoke;
  247. typedef impl_base* (*clone_type)(const impl_base*);
  248. const clone_type clone;
  249. typedef void (*destroy_type)(impl_base*);
  250. const destroy_type destroy;
  251. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  252. {
  253. }
  254. };
  255. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  256. template< typename FunT >
  257. class impl;
  258. template< typename FunT >
  259. friend class impl;
  260. #endif
  261. template< typename FunT >
  262. class impl :
  263. public impl_base
  264. {
  265. typedef impl< FunT > this_type;
  266. FunT m_Function;
  267. public:
  268. explicit impl(FunT const& fun) :
  269. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  270. m_Function(fun)
  271. {
  272. }
  273. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  274. explicit impl(FunT&& fun) :
  275. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  276. m_Function(fun)
  277. {
  278. }
  279. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  280. static void destroy_impl(impl_base* self)
  281. {
  282. delete static_cast< impl* >(self);
  283. }
  284. static impl_base* clone_impl(const impl_base* self)
  285. {
  286. return new impl(static_cast< const impl* >(self)->m_Function);
  287. }
  288. static result_type invoke_impl(impl_base* self, ArgsT... args)
  289. {
  290. static_cast< impl* >(self)->m_Function(args...);
  291. }
  292. };
  293. private:
  294. impl_base* m_pImpl;
  295. public:
  296. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  297. {
  298. }
  299. light_function(this_type const& that)
  300. {
  301. if (that.m_pImpl)
  302. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  303. else
  304. m_pImpl = NULL;
  305. }
  306. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  307. {
  308. m_pImpl = that.m_pImpl;
  309. that.m_pImpl = NULL;
  310. }
  311. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  312. {
  313. m_pImpl = that.m_pImpl;
  314. ((this_type&)that).m_pImpl = NULL;
  315. }
  316. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  317. template< typename FunT >
  318. light_function(FunT&& fun) :
  319. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  320. {
  321. }
  322. #else
  323. template< typename FunT >
  324. light_function(FunT const& fun, typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, int >::type = 0) :
  325. m_pImpl(new impl< FunT >(fun))
  326. {
  327. }
  328. template< typename FunT >
  329. light_function(rv< FunT > const& fun, typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, int >::type = 0) :
  330. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  331. {
  332. }
  333. #endif
  334. //! Constructor from NULL
  335. #if !defined(BOOST_NO_CXX11_NULLPTR)
  336. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  337. #else
  338. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  339. #endif
  340. : m_pImpl(NULL)
  341. {
  342. #if defined(BOOST_NO_CXX11_NULLPTR)
  343. BOOST_ASSERT(p == 0);
  344. #endif
  345. }
  346. ~light_function()
  347. {
  348. clear();
  349. }
  350. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  351. {
  352. this->swap(that);
  353. return *this;
  354. }
  355. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  356. {
  357. light_function tmp = that;
  358. this->swap(tmp);
  359. return *this;
  360. }
  361. //! Assignment of NULL
  362. #if !defined(BOOST_NO_CXX11_NULLPTR)
  363. light_function& operator= (std::nullptr_t)
  364. #else
  365. light_function& operator= (int p)
  366. #endif
  367. {
  368. #if defined(BOOST_NO_CXX11_NULLPTR)
  369. BOOST_ASSERT(p == 0);
  370. #endif
  371. clear();
  372. return *this;
  373. }
  374. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  375. template< typename FunT >
  376. light_function& operator= (FunT&& fun)
  377. {
  378. light_function tmp(boost::forward< FunT >(fun));
  379. this->swap(tmp);
  380. return *this;
  381. }
  382. #else
  383. template< typename FunT >
  384. typename disable_if< mpl::or_< move_detail::is_rv< FunT >, is_same< FunT, this_type > >, this_type& >::type
  385. operator= (FunT const& fun)
  386. {
  387. light_function tmp(fun);
  388. this->swap(tmp);
  389. return *this;
  390. }
  391. #endif
  392. result_type operator() (ArgsT... args) const
  393. {
  394. m_pImpl->invoke(m_pImpl, args...);
  395. }
  396. BOOST_EXPLICIT_OPERATOR_BOOL()
  397. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  398. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  399. void clear() BOOST_NOEXCEPT
  400. {
  401. if (m_pImpl)
  402. {
  403. m_pImpl->destroy(m_pImpl);
  404. m_pImpl = NULL;
  405. }
  406. }
  407. void swap(this_type& that) BOOST_NOEXCEPT
  408. {
  409. register impl_base* p = m_pImpl;
  410. m_pImpl = that.m_pImpl;
  411. that.m_pImpl = p;
  412. }
  413. };
  414. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  415. #define BOOST_PP_FILENAME_1 <boost/log/detail/light_function_pp.hpp>
  416. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_LOG_LIGHT_FUNCTION_LIMIT)
  417. #include BOOST_PP_ITERATE()
  418. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  419. template< typename SignatureT >
  420. inline void swap(light_function< SignatureT >& left, light_function< SignatureT >& right)
  421. {
  422. left.swap(right);
  423. }
  424. } // namespace aux
  425. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  426. } // namespace boost
  427. #include <boost/log/detail/footer.hpp>
  428. #endif // BOOST_LOG_DETAIL_LIGHT_FUNCTION_HPP_INCLUDED_