function_wrapper.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2013 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // 2013/09 Vicente J. Botet Escriba
  7. // Adapt to boost from CCIA C++11 implementation
  8. // Make use of Boost.Move
  9. #ifndef BOOST_THREAD_DETAIL_FUNCTION_WRAPPER_HPP
  10. #define BOOST_THREAD_DETAIL_FUNCTION_WRAPPER_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/thread/detail/memory.hpp>
  13. #include <boost/thread/detail/move.hpp>
  14. #include <boost/thread/csbl/memory/unique_ptr.hpp>
  15. #include <memory>
  16. #include <functional>
  17. namespace boost
  18. {
  19. namespace detail
  20. {
  21. class function_wrapper
  22. {
  23. struct impl_base
  24. {
  25. virtual void call()=0;
  26. virtual ~impl_base()
  27. {
  28. }
  29. };
  30. typedef boost::csbl::unique_ptr<impl_base> impl_base_type;
  31. impl_base_type impl;
  32. template <typename F>
  33. struct impl_type: impl_base
  34. {
  35. F f;
  36. impl_type(F const &f_)
  37. : f(f_)
  38. {}
  39. impl_type(BOOST_THREAD_RV_REF(F) f_)
  40. : f(boost::move(f_))
  41. {}
  42. void call()
  43. {
  44. if (impl) f();
  45. }
  46. };
  47. public:
  48. BOOST_THREAD_MOVABLE_ONLY(function_wrapper)
  49. //#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  50. template<typename F>
  51. function_wrapper(F const& f):
  52. impl(new impl_type<F>(f))
  53. {}
  54. //#endif
  55. template<typename F>
  56. function_wrapper(BOOST_THREAD_RV_REF(F) f):
  57. impl(new impl_type<F>(boost::forward<F>(f)))
  58. {}
  59. function_wrapper(BOOST_THREAD_RV_REF(function_wrapper) other) BOOST_NOEXCEPT :
  60. impl(other.impl)
  61. {
  62. other.impl = 0;
  63. }
  64. function_wrapper()
  65. : impl(0)
  66. {
  67. }
  68. ~function_wrapper()
  69. {
  70. }
  71. function_wrapper& operator=(BOOST_THREAD_RV_REF(function_wrapper) other) BOOST_NOEXCEPT
  72. {
  73. impl=other.impl;
  74. other.impl=0;
  75. return *this;
  76. }
  77. void operator()()
  78. { impl->call();}
  79. };
  80. }
  81. }
  82. #endif // header