light_function.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  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_core.hpp>
  23. #include <boost/core/explicit_operator_bool.hpp>
  24. #include <boost/log/detail/config.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/log/detail/sfinae_tools.hpp>
  35. #else
  36. #include <boost/type_traits/remove_reference.hpp>
  37. #endif
  38. #if defined(BOOST_NO_CXX11_NULLPTR)
  39. #include <boost/assert.hpp>
  40. #endif
  41. #include <boost/log/detail/header.hpp>
  42. #ifdef BOOST_HAS_PRAGMA_ONCE
  43. #pragma once
  44. #endif
  45. #ifndef BOOST_LOG_LIGHT_FUNCTION_LIMIT
  46. #define BOOST_LOG_LIGHT_FUNCTION_LIMIT 2
  47. #endif
  48. namespace boost {
  49. BOOST_LOG_OPEN_NAMESPACE
  50. namespace aux {
  51. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  52. template< typename T, typename ThisT >
  53. struct is_cv_same { enum _ { value = false }; };
  54. template< typename T >
  55. struct is_cv_same< T, T > { enum _ { value = true }; };
  56. template< typename T >
  57. struct is_cv_same< T, const T > { enum _ { value = true }; };
  58. template< typename T >
  59. struct is_cv_same< T, volatile T > { enum _ { value = true }; };
  60. template< typename T >
  61. struct is_cv_same< T, const volatile T > { enum _ { value = true }; };
  62. template< typename T, typename ThisT >
  63. struct is_rv_or_same { enum _ { value = false }; };
  64. template< typename T >
  65. struct is_rv_or_same< T, T > { enum _ { value = true }; };
  66. template< typename T, typename ThisT >
  67. struct is_rv_or_same< boost::rv< T >, ThisT > { enum _ { value = true }; };
  68. #endif
  69. template< typename SignatureT >
  70. class light_function;
  71. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  72. template< typename ResultT, typename... ArgsT >
  73. class light_function< ResultT (ArgsT...) >
  74. {
  75. typedef light_function this_type;
  76. BOOST_COPYABLE_AND_MOVABLE(this_type)
  77. public:
  78. typedef ResultT result_type;
  79. private:
  80. struct impl_base
  81. {
  82. typedef result_type (*invoke_type)(void*, ArgsT...);
  83. const invoke_type invoke;
  84. typedef impl_base* (*clone_type)(const void*);
  85. const clone_type clone;
  86. typedef void (*destroy_type)(void*);
  87. const destroy_type destroy;
  88. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  89. {
  90. }
  91. BOOST_DELETED_FUNCTION(impl_base(impl_base const&))
  92. BOOST_DELETED_FUNCTION(impl_base& operator= (impl_base const&))
  93. };
  94. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  95. template< typename FunT >
  96. class impl;
  97. template< typename FunT >
  98. friend class impl;
  99. #endif
  100. template< typename FunT >
  101. class impl :
  102. public impl_base
  103. {
  104. typedef impl< FunT > this_type;
  105. FunT m_Function;
  106. public:
  107. explicit impl(FunT const& fun) :
  108. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  109. m_Function(fun)
  110. {
  111. }
  112. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  113. explicit impl(FunT&& fun) :
  114. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  115. m_Function(boost::move(fun))
  116. {
  117. }
  118. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  119. static void destroy_impl(void* self)
  120. {
  121. delete static_cast< impl* >(static_cast< impl_base* >(self));
  122. }
  123. static impl_base* clone_impl(const void* self)
  124. {
  125. return new impl(static_cast< const impl* >(static_cast< const impl_base* >(self))->m_Function);
  126. }
  127. static result_type invoke_impl(void* self, ArgsT... args)
  128. {
  129. return static_cast< impl* >(static_cast< impl_base* >(self))->m_Function(args...);
  130. }
  131. BOOST_DELETED_FUNCTION(impl(impl const&))
  132. BOOST_DELETED_FUNCTION(impl& operator= (impl const&))
  133. };
  134. private:
  135. impl_base* m_pImpl;
  136. public:
  137. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  138. {
  139. }
  140. light_function(this_type const& that)
  141. {
  142. if (that.m_pImpl)
  143. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  144. else
  145. m_pImpl = NULL;
  146. }
  147. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  148. {
  149. m_pImpl = that.m_pImpl;
  150. that.m_pImpl = NULL;
  151. }
  152. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  153. {
  154. m_pImpl = that.m_pImpl;
  155. ((this_type&)that).m_pImpl = NULL;
  156. }
  157. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  158. template< typename FunT >
  159. light_function(FunT&& fun) :
  160. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  161. {
  162. }
  163. #else
  164. template< typename FunT >
  165. light_function(FunT const& fun, typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
  166. m_pImpl(new impl< FunT >(fun))
  167. {
  168. }
  169. template< typename FunT >
  170. light_function(BOOST_RV_REF(FunT) fun, typename boost::disable_if_c< is_cv_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
  171. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  172. {
  173. }
  174. #endif
  175. //! Constructor from NULL
  176. #if !defined(BOOST_NO_CXX11_NULLPTR)
  177. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  178. #else
  179. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  180. #endif
  181. : m_pImpl(NULL)
  182. {
  183. #if defined(BOOST_NO_CXX11_NULLPTR)
  184. BOOST_ASSERT(p == 0);
  185. #endif
  186. }
  187. ~light_function()
  188. {
  189. clear();
  190. }
  191. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  192. {
  193. this->swap(that);
  194. return *this;
  195. }
  196. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  197. {
  198. light_function tmp = static_cast< this_type const& >(that);
  199. this->swap(tmp);
  200. return *this;
  201. }
  202. //! Assignment of NULL
  203. #if !defined(BOOST_NO_CXX11_NULLPTR)
  204. light_function& operator= (std::nullptr_t)
  205. #else
  206. light_function& operator= (int p)
  207. #endif
  208. {
  209. #if defined(BOOST_NO_CXX11_NULLPTR)
  210. BOOST_ASSERT(p == 0);
  211. #endif
  212. clear();
  213. return *this;
  214. }
  215. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  216. template< typename FunT >
  217. light_function& operator= (FunT&& fun)
  218. {
  219. light_function tmp(boost::forward< FunT >(fun));
  220. this->swap(tmp);
  221. return *this;
  222. }
  223. #else
  224. template< typename FunT >
  225. typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, this_type& >::type
  226. operator= (FunT const& fun)
  227. {
  228. light_function tmp(fun);
  229. this->swap(tmp);
  230. return *this;
  231. }
  232. #endif
  233. result_type operator() (ArgsT... args) const
  234. {
  235. return m_pImpl->invoke(m_pImpl, args...);
  236. }
  237. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  238. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  239. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  240. void clear() BOOST_NOEXCEPT
  241. {
  242. if (m_pImpl)
  243. {
  244. m_pImpl->destroy(m_pImpl);
  245. m_pImpl = NULL;
  246. }
  247. }
  248. void swap(this_type& that) BOOST_NOEXCEPT
  249. {
  250. impl_base* p = m_pImpl;
  251. m_pImpl = that.m_pImpl;
  252. that.m_pImpl = p;
  253. }
  254. };
  255. template< typename... ArgsT >
  256. class light_function< void (ArgsT...) >
  257. {
  258. typedef light_function this_type;
  259. BOOST_COPYABLE_AND_MOVABLE(this_type)
  260. public:
  261. typedef void result_type;
  262. private:
  263. struct impl_base
  264. {
  265. typedef void (*invoke_type)(void*, ArgsT...);
  266. const invoke_type invoke;
  267. typedef impl_base* (*clone_type)(const void*);
  268. const clone_type clone;
  269. typedef void (*destroy_type)(void*);
  270. const destroy_type destroy;
  271. impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
  272. {
  273. }
  274. BOOST_DELETED_FUNCTION(impl_base(impl_base const&))
  275. BOOST_DELETED_FUNCTION(impl_base& operator= (impl_base const&))
  276. };
  277. #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
  278. template< typename FunT >
  279. class impl;
  280. template< typename FunT >
  281. friend class impl;
  282. #endif
  283. template< typename FunT >
  284. class impl :
  285. public impl_base
  286. {
  287. typedef impl< FunT > this_type;
  288. FunT m_Function;
  289. public:
  290. explicit impl(FunT const& fun) :
  291. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  292. m_Function(fun)
  293. {
  294. }
  295. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  296. explicit impl(FunT&& fun) :
  297. impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
  298. m_Function(boost::move(fun))
  299. {
  300. }
  301. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  302. static void destroy_impl(void* self)
  303. {
  304. delete static_cast< impl* >(static_cast< impl_base* >(self));
  305. }
  306. static impl_base* clone_impl(const void* self)
  307. {
  308. return new impl(static_cast< const impl* >(static_cast< const impl_base* >(self))->m_Function);
  309. }
  310. static result_type invoke_impl(void* self, ArgsT... args)
  311. {
  312. static_cast< impl* >(static_cast< impl_base* >(self))->m_Function(args...);
  313. }
  314. BOOST_DELETED_FUNCTION(impl(impl const&))
  315. BOOST_DELETED_FUNCTION(impl& operator= (impl const&))
  316. };
  317. private:
  318. impl_base* m_pImpl;
  319. public:
  320. BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
  321. {
  322. }
  323. light_function(this_type const& that)
  324. {
  325. if (that.m_pImpl)
  326. m_pImpl = that.m_pImpl->clone(that.m_pImpl);
  327. else
  328. m_pImpl = NULL;
  329. }
  330. light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  331. {
  332. m_pImpl = that.m_pImpl;
  333. that.m_pImpl = NULL;
  334. }
  335. light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
  336. {
  337. m_pImpl = that.m_pImpl;
  338. ((this_type&)that).m_pImpl = NULL;
  339. }
  340. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  341. template< typename FunT >
  342. light_function(FunT&& fun) :
  343. m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
  344. {
  345. }
  346. #else
  347. template< typename FunT >
  348. light_function(FunT const& fun, typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
  349. m_pImpl(new impl< FunT >(fun))
  350. {
  351. }
  352. template< typename FunT >
  353. light_function(BOOST_RV_REF(FunT) fun, typename boost::disable_if_c< is_cv_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
  354. m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
  355. {
  356. }
  357. #endif
  358. //! Constructor from NULL
  359. #if !defined(BOOST_NO_CXX11_NULLPTR)
  360. BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
  361. #else
  362. BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
  363. #endif
  364. : m_pImpl(NULL)
  365. {
  366. #if defined(BOOST_NO_CXX11_NULLPTR)
  367. BOOST_ASSERT(p == 0);
  368. #endif
  369. }
  370. ~light_function()
  371. {
  372. clear();
  373. }
  374. light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  375. {
  376. this->swap(that);
  377. return *this;
  378. }
  379. light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  380. {
  381. light_function tmp = static_cast< this_type const& >(that);
  382. this->swap(tmp);
  383. return *this;
  384. }
  385. //! Assignment of NULL
  386. #if !defined(BOOST_NO_CXX11_NULLPTR)
  387. light_function& operator= (std::nullptr_t)
  388. #else
  389. light_function& operator= (int p)
  390. #endif
  391. {
  392. #if defined(BOOST_NO_CXX11_NULLPTR)
  393. BOOST_ASSERT(p == 0);
  394. #endif
  395. clear();
  396. return *this;
  397. }
  398. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  399. template< typename FunT >
  400. light_function& operator= (FunT&& fun)
  401. {
  402. light_function tmp(boost::forward< FunT >(fun));
  403. this->swap(tmp);
  404. return *this;
  405. }
  406. #else
  407. template< typename FunT >
  408. typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, this_type& >::type
  409. operator= (FunT const& fun)
  410. {
  411. light_function tmp(fun);
  412. this->swap(tmp);
  413. return *this;
  414. }
  415. #endif
  416. result_type operator() (ArgsT... args) const
  417. {
  418. m_pImpl->invoke(m_pImpl, args...);
  419. }
  420. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  421. bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  422. bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
  423. void clear() BOOST_NOEXCEPT
  424. {
  425. if (m_pImpl)
  426. {
  427. m_pImpl->destroy(m_pImpl);
  428. m_pImpl = NULL;
  429. }
  430. }
  431. void swap(this_type& that) BOOST_NOEXCEPT
  432. {
  433. impl_base* p = m_pImpl;
  434. m_pImpl = that.m_pImpl;
  435. that.m_pImpl = p;
  436. }
  437. };
  438. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  439. #define BOOST_PP_FILENAME_1 <boost/log/detail/light_function_pp.hpp>
  440. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_LOG_LIGHT_FUNCTION_LIMIT)
  441. #include BOOST_PP_ITERATE()
  442. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  443. template< typename SignatureT >
  444. inline void swap(light_function< SignatureT >& left, light_function< SignatureT >& right)
  445. {
  446. left.swap(right);
  447. }
  448. } // namespace aux
  449. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  450. } // namespace boost
  451. #include <boost/log/detail/footer.hpp>
  452. #endif // BOOST_LOG_DETAIL_LIGHT_FUNCTION_HPP_INCLUDED_