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 8 #ifndef BOOST_HOF_GUARD_FUNCTION_MUTABLE_H 9 #define BOOST_HOF_GUARD_FUNCTION_MUTABLE_H 10 11 /// mutable 12 /// ======= 13 /// 14 /// Description 15 /// ----------- 16 /// 17 /// The `mutable` function adaptor allows using a non-const function object 18 /// inside of a const-function object. In Fit, all the function adaptors use 19 /// `const` call overloads, so if there is a function that has a non-const 20 /// call operator, it couldn't be used directly. So, `mutable_` allows the 21 /// function to be used inside of the call operator. 22 /// 23 /// NOTE: This function should be used with caution since many functions are 24 /// copied, so relying on some internal shared state can be error-prone. 25 /// 26 /// Synopsis 27 /// -------- 28 /// 29 /// template<class F> 30 /// mutable_adaptor<F> mutable_(F f) 31 /// 32 /// Requirements 33 /// ------------ 34 /// 35 /// F must be: 36 /// 37 /// * [MutableFunctionObject](MutableFunctionObject) 38 /// * MoveConstructible 39 /// 40 41 #include <boost/hof/detail/result_of.hpp> 42 #include <boost/hof/detail/delegate.hpp> 43 #include <boost/hof/detail/move.hpp> 44 #include <boost/hof/detail/make.hpp> 45 #include <boost/hof/detail/static_const_var.hpp> 46 47 namespace boost { namespace hof { 48 49 template<class F> 50 struct mutable_adaptor 51 { 52 mutable F f; 53 54 BOOST_HOF_DELEGATE_CONSTRUCTOR(mutable_adaptor, F, f); 55 56 BOOST_HOF_RETURNS_CLASS(mutable_adaptor); 57 58 template<class... Ts> 59 BOOST_HOF_SFINAE_RESULT(F, id_<Ts>...) 60 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS(BOOST_HOF_CONST_THIS->f(BOOST_HOF_FORWARD(Ts)(xs)...)); 61 }; 62 63 BOOST_HOF_DECLARE_STATIC_VAR(mutable_, detail::make<mutable_adaptor>); 64 65 }} // namespace boost::hof 66 67 68 #endif 69