mutable.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. mutable.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_MUTABLE_H
  8. #define BOOST_HOF_GUARD_FUNCTION_MUTABLE_H
  9. /// mutable
  10. /// =======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `mutable` function adaptor allows using a non-const function object
  16. /// inside of a const-function object. In Fit, all the function adaptors use
  17. /// `const` call overloads, so if there is a function that has a non-const
  18. /// call operator, it couldn't be used directly. So, `mutable_` allows the
  19. /// function to be used inside of the call operator.
  20. ///
  21. /// NOTE: This function should be used with caution since many functions are
  22. /// copied, so relying on some internal shared state can be error-prone.
  23. ///
  24. /// Synopsis
  25. /// --------
  26. ///
  27. /// template<class F>
  28. /// mutable_adaptor<F> mutable_(F f)
  29. ///
  30. /// Requirements
  31. /// ------------
  32. ///
  33. /// F must be:
  34. ///
  35. /// * [MutableFunctionObject](MutableFunctionObject)
  36. /// * MoveConstructible
  37. ///
  38. #include <boost/hof/detail/result_of.hpp>
  39. #include <boost/hof/detail/delegate.hpp>
  40. #include <boost/hof/detail/move.hpp>
  41. #include <boost/hof/detail/make.hpp>
  42. #include <boost/hof/detail/static_const_var.hpp>
  43. namespace boost { namespace hof {
  44. template<class F>
  45. struct mutable_adaptor
  46. {
  47. mutable F f;
  48. BOOST_HOF_DELEGATE_CONSTRUCTOR(mutable_adaptor, F, f);
  49. BOOST_HOF_RETURNS_CLASS(mutable_adaptor);
  50. template<class... Ts>
  51. BOOST_HOF_SFINAE_RESULT(F, id_<Ts>...)
  52. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS(BOOST_HOF_CONST_THIS->f(BOOST_HOF_FORWARD(Ts)(xs)...));
  53. };
  54. BOOST_HOF_DECLARE_STATIC_VAR(mutable_, detail::make<mutable_adaptor>);
  55. }} // namespace boost::hof
  56. #endif