protect.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef BOOST_BIND_PROTECT_HPP_INCLUDED
  2. #define BOOST_BIND_PROTECT_HPP_INCLUDED
  3. //
  4. // protect.hpp
  5. //
  6. // Copyright 2002, 2020 Peter Dimov
  7. // Copyright 2009 Steven Watanabe
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. #include <utility>
  14. namespace boost
  15. {
  16. namespace _bi
  17. {
  18. template<class T> struct protect_make_void
  19. {
  20. typedef void type;
  21. };
  22. template<class F, class E = void> struct protect_result_type
  23. {
  24. };
  25. template<class F> struct protect_result_type< F, typename protect_make_void<typename F::result_type>::type >
  26. {
  27. typedef typename F::result_type result_type;
  28. };
  29. template<class F> class protected_bind_t: public protect_result_type<F>
  30. {
  31. private:
  32. F f_;
  33. public:
  34. explicit protected_bind_t( F f ): f_( f )
  35. {
  36. }
  37. template<class... A> auto operator()( A&&... a ) -> decltype( f_( std::forward<A>(a)... ) )
  38. {
  39. return f_( std::forward<A>(a)... );
  40. }
  41. template<class... A> auto operator()( A&&... a ) const -> decltype( f_( std::forward<A>(a)... ) )
  42. {
  43. return f_( std::forward<A>(a)... );
  44. }
  45. };
  46. } // namespace _bi
  47. template<class F> _bi::protected_bind_t<F> protect(F f)
  48. {
  49. return _bi::protected_bind_t<F>(f);
  50. }
  51. } // namespace boost
  52. #endif // #ifndef BOOST_BIND_PROTECT_HPP_INCLUDED