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)
10 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), typename ArgT)
12 class light_function< ResultT (BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), ArgT)) >
14 typedef light_function this_type;
15 BOOST_COPYABLE_AND_MOVABLE(this_type)
18 typedef ResultT result_type;
23 typedef result_type (*invoke_type)(void* BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), ArgT));
24 const invoke_type invoke;
26 typedef impl_base* (*clone_type)(const void*);
27 const clone_type clone;
29 typedef void (*destroy_type)(void*);
30 const destroy_type destroy;
32 impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
36 BOOST_DELETED_FUNCTION(impl_base(impl_base const&))
37 BOOST_DELETED_FUNCTION(impl_base& operator= (impl_base const&))
40 #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
41 template< typename FunT >
43 template< typename FunT >
47 template< typename FunT >
51 typedef impl< FunT > this_type;
56 explicit impl(FunT const& fun) :
57 impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
62 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
63 explicit impl(FunT&& fun) :
64 impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
65 m_Function(boost::move(fun))
68 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
70 static void destroy_impl(void* self)
72 delete static_cast< impl* >(static_cast< impl_base* >(self));
74 static impl_base* clone_impl(const void* self)
76 return new impl(static_cast< const impl* >(static_cast< const impl_base* >(self))->m_Function);
78 static result_type invoke_impl(void* self BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg))
80 return static_cast< impl* >(static_cast< impl_base* >(self))->m_Function(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), arg));
83 BOOST_DELETED_FUNCTION(impl(impl const&))
84 BOOST_DELETED_FUNCTION(impl& operator= (impl const&))
91 BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
94 light_function(this_type const& that)
97 m_pImpl = that.m_pImpl->clone(that.m_pImpl);
102 light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
104 m_pImpl = that.m_pImpl;
108 light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
110 m_pImpl = that.m_pImpl;
111 ((this_type&)that).m_pImpl = NULL;
114 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
115 template< typename FunT >
116 light_function(FunT&& fun) :
117 m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
121 template< typename FunT >
122 light_function(FunT const& fun, typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
123 m_pImpl(new impl< FunT >(fun))
126 template< typename FunT >
127 light_function(BOOST_RV_REF(FunT) fun, typename boost::disable_if_c< is_cv_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
128 m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
133 //! Constructor from NULL
134 #if !defined(BOOST_NO_CXX11_NULLPTR)
135 BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
137 BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
141 #if defined(BOOST_NO_CXX11_NULLPTR)
142 BOOST_ASSERT(p == 0);
150 light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
155 light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
157 light_function tmp = static_cast< this_type const& >(that);
161 //! Assignment of NULL
162 #if !defined(BOOST_NO_CXX11_NULLPTR)
163 light_function& operator= (std::nullptr_t)
165 light_function& operator= (int p)
168 #if defined(BOOST_NO_CXX11_NULLPTR)
169 BOOST_ASSERT(p == 0);
174 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
175 template< typename FunT >
176 light_function& operator= (FunT&& fun)
178 light_function tmp(boost::forward< FunT >(fun));
183 template< typename FunT >
184 typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, this_type& >::type
185 operator= (FunT const& fun)
187 light_function tmp(fun);
193 result_type operator() (BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg)) const
195 return m_pImpl->invoke(m_pImpl BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), arg));
198 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
199 bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
200 bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
201 void clear() BOOST_NOEXCEPT
205 m_pImpl->destroy(m_pImpl);
210 void swap(this_type& that) BOOST_NOEXCEPT
212 impl_base* p = m_pImpl;
213 m_pImpl = that.m_pImpl;
219 BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), typename ArgT)
221 class light_function< void (BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), ArgT)) >
223 typedef light_function this_type;
224 BOOST_COPYABLE_AND_MOVABLE(this_type)
227 typedef void result_type;
232 typedef void (*invoke_type)(void* BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), ArgT));
233 const invoke_type invoke;
235 typedef impl_base* (*clone_type)(const void*);
236 const clone_type clone;
238 typedef void (*destroy_type)(void*);
239 const destroy_type destroy;
241 impl_base(invoke_type inv, clone_type cl, destroy_type dstr) : invoke(inv), clone(cl), destroy(dstr)
245 BOOST_DELETED_FUNCTION(impl_base(impl_base const&))
246 BOOST_DELETED_FUNCTION(impl_base& operator= (impl_base const&))
249 #if !defined(BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS)
250 template< typename FunT >
252 template< typename FunT >
256 template< typename FunT >
260 typedef impl< FunT > this_type;
265 explicit impl(FunT const& fun) :
266 impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
271 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
272 explicit impl(FunT&& fun) :
273 impl_base(&this_type::invoke_impl, &this_type::clone_impl, &this_type::destroy_impl),
274 m_Function(boost::move(fun))
277 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
279 static void destroy_impl(void* self)
281 delete static_cast< impl* >(static_cast< impl_base* >(self));
283 static impl_base* clone_impl(const void* self)
285 return new impl(static_cast< const impl* >(static_cast< const impl_base* >(self))->m_Function);
287 static result_type invoke_impl(void* self BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg))
289 static_cast< impl* >(static_cast< impl_base* >(self))->m_Function(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), arg));
292 BOOST_DELETED_FUNCTION(impl(impl const&))
293 BOOST_DELETED_FUNCTION(impl& operator= (impl const&))
300 BOOST_CONSTEXPR light_function() BOOST_NOEXCEPT : m_pImpl(NULL)
303 light_function(this_type const& that)
306 m_pImpl = that.m_pImpl->clone(that.m_pImpl);
310 light_function(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
312 m_pImpl = that.m_pImpl;
316 light_function(BOOST_RV_REF(const this_type) that) BOOST_NOEXCEPT
318 m_pImpl = that.m_pImpl;
319 ((this_type&)that).m_pImpl = NULL;
322 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
323 template< typename FunT >
324 light_function(FunT&& fun) :
325 m_pImpl(new impl< typename remove_cv< typename remove_reference< FunT >::type >::type >(boost::forward< FunT >(fun)))
329 template< typename FunT >
330 light_function(FunT const& fun, typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
331 m_pImpl(new impl< FunT >(fun))
334 template< typename FunT >
335 light_function(BOOST_RV_REF(FunT) fun, typename boost::disable_if_c< is_cv_same< FunT, this_type >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :
336 m_pImpl(new impl< typename remove_cv< FunT >::type >(fun))
341 //! Constructor from NULL
342 #if !defined(BOOST_NO_CXX11_NULLPTR)
343 BOOST_CONSTEXPR light_function(std::nullptr_t) BOOST_NOEXCEPT
345 BOOST_CONSTEXPR light_function(int p) BOOST_NOEXCEPT
349 #if defined(BOOST_NO_CXX11_NULLPTR)
350 BOOST_ASSERT(p == 0);
358 light_function& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
363 light_function& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
365 light_function tmp = static_cast< this_type const& >(that);
369 //! Assignment of NULL
370 #if !defined(BOOST_NO_CXX11_NULLPTR)
371 light_function& operator= (std::nullptr_t)
373 light_function& operator= (int p)
376 #if defined(BOOST_NO_CXX11_NULLPTR)
377 BOOST_ASSERT(p == 0);
382 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
383 template< typename FunT >
384 light_function& operator= (FunT&& fun)
386 light_function tmp(boost::forward< FunT >(fun));
391 template< typename FunT >
392 typename boost::disable_if_c< is_rv_or_same< FunT, this_type >::value, this_type& >::type
393 operator= (FunT const& fun)
395 light_function tmp(fun);
401 result_type operator() (BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_ITERATION(), ArgT, arg)) const
403 m_pImpl->invoke(m_pImpl BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), arg));
406 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
407 bool operator! () const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
408 bool empty() const BOOST_NOEXCEPT { return (m_pImpl == NULL); }
409 void clear() BOOST_NOEXCEPT
413 m_pImpl->destroy(m_pImpl);
418 void swap(this_type& that) BOOST_NOEXCEPT
420 impl_base* p = m_pImpl;
421 m_pImpl = that.m_pImpl;