From 5f0bb4db4354d775a9fa1df5b9042fa5719782d7 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Wed, 24 Oct 2012 22:24:56 -0400 Subject: [PATCH] Adding fast compiling boost::function replacement --- include/fc/function.hpp | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/include/fc/function.hpp b/include/fc/function.hpp index 0773a6e..153b64f 100644 --- a/include/fc/function.hpp +++ b/include/fc/function.hpp @@ -1,6 +1,8 @@ #ifndef _FC_FUNCTION_HPP_ #define _FC_FUNCTION_HPP_ #include +#include +/* #include #include @@ -11,6 +13,102 @@ namespace fc { using function = std::function; #else #endif +*/ + + +//template +//class function { }; + +namespace fc { + +template +class function_impl { + public: + template + function_impl( Functor&& f ) + :func( new impl( fc::forward(f) ) ){}; + + function_impl( const function_impl& c ):func(c.func){} + function_impl( function_impl&& c ) { fc::swap( func, c.func); } + ~function_impl(){} + + template + function_impl& operator=( Functor&& f ) { + func.reset( new impl( fc::forward(f) ) ); + return *this; + } + + function_impl& operator=( const function_impl& c ) { func = c.func; return *this; } + function_impl& operator=( function_impl&& c ) { fc::swap(func,c.func); return *this; } + + template + R operator()( Args2... args2) { return func->call(fc::forward(args2)...); } + + private: + struct impl_base : public fc::retainable { + virtual ~impl_base(){} + virtual R call(Args...) = 0; + }; + + template + struct impl : impl_base { + template + impl( U&& u ):func( fc::forward(u) ){} + + virtual R call(Args... args) { return func(args...); } + + Functor func; + }; + + fc::shared_ptr func; +}; + +/** + * Provides functionality similar to boost::function. + * + * Functions have 'reference semantics', meaning that copies will all + * refer to the same underlying function. + * + * TODO: Small functions are allocated on the stack, large functors are + * allocated on the heap. + */ +template +class function : public function_impl { + public: + template + function( U&& u ):function_impl( fc::forward(u) ){} + using function_impl::operator=; +}; + +template +class function : public function_impl { + public: + template + function( U&& u ):function_impl( fc::forward(u) ){} + using function_impl::operator=; +}; + +template +class function : public function_impl { + public: + template + function( U&& u ):function_impl( fc::forward(u) ){} + using function_impl::operator=; +}; + +template +class function : public function_impl { + public: + template + function( U&& u ):function_impl( fc::forward(u) ){} + using function_impl::operator=; +}; + + + + + + } #endif // _FC_FUNCTION_HPP_