fun_ref.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 fun_ref.hpp
  9. * \author Andrey Semashev
  10. * \date 30.03.2008
  11. *
  12. * This header contains function object reference adapter. The adapter stores a reference to external
  13. * function object and forwards all calls to the referred function.
  14. */
  15. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_FUN_REF_HPP_INCLUDED_
  16. #define BOOST_LOG_UTILITY_FUNCTIONAL_FUN_REF_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/detail/header.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. namespace boost {
  23. BOOST_LOG_OPEN_NAMESPACE
  24. //! Reference wrapper for function objects
  25. template< typename FunT >
  26. struct function_reference_wrapper
  27. {
  28. typedef typename FunT::result_type result_type;
  29. explicit function_reference_wrapper(FunT& fun) : m_Fun(fun) {}
  30. result_type operator() () const
  31. {
  32. return m_Fun();
  33. }
  34. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  35. template< typename... ArgsT >
  36. result_type operator() (ArgsT const&... args) const
  37. {
  38. return m_Fun(args...);
  39. }
  40. #else
  41. template< typename T >
  42. result_type operator() (T const& arg) const
  43. {
  44. return m_Fun(arg);
  45. }
  46. template< typename T1, typename T2 >
  47. result_type operator() (T1 const& arg1, T2 const& arg2) const
  48. {
  49. return m_Fun(arg1, arg2);
  50. }
  51. #endif
  52. private:
  53. FunT& m_Fun;
  54. };
  55. template< typename FunT >
  56. BOOST_FORCEINLINE function_reference_wrapper< FunT > fun_ref(FunT& fun)
  57. {
  58. return function_reference_wrapper< FunT >(fun);
  59. }
  60. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  61. } // namespace boost
  62. #include <boost/log/detail/footer.hpp>
  63. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_FUN_REF_HPP_INCLUDED_