Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=49c507d1ba2a0b186ffbf17462c4d6b13c1bec85
commit 49c507d1ba2a0b186ffbf17462c4d6b13c1bec85 Author: Michel Hermier <herm...@frugalware.org> Date: Mon Jun 2 17:28:40 2014 +0200 libpacman: More FFunction updates. diff --git a/lib/libpacman/kernel/fsignal.h b/lib/libpacman/kernel/fsignal.h index f4ce4b9..a1ac2e9 100644 --- a/lib/libpacman/kernel/fsignal.h +++ b/lib/libpacman/kernel/fsignal.h @@ -71,6 +71,12 @@ public: return true; } + template <typename OtherR, typename... OtherArgs> + bool connect(const flib::FSignal<OtherR(OtherArgs...)> *other) + { + return connect(flib::FFunction<R(Args...)>(other, static_cast<R (flib::FSignal<OtherR(OtherArgs...)>::*)(OtherArgs...) const>(&flib::FSignal<OtherR(OtherArgs...)>::operator()))); + } + template <typename Pointer, typename MethodSignature> bool connect(Pointer pointer, MethodSignature method) { diff --git a/lib/libpacman/util/ffunctional.h b/lib/libpacman/util/ffunctional.h index bbb0f58..5f90112 100644 --- a/lib/libpacman/util/ffunctional.h +++ b/lib/libpacman/util/ffunctional.h @@ -24,7 +24,8 @@ #include <functional> #include <memory> // For std::shared_ptr #include <typeinfo> -#include <type_traits> + +#include "util/ftype_traits.h" #include "util.h" // For ASSERT @@ -60,27 +61,41 @@ namespace detail { template <typename> -struct FAbstractFunctionImpl; +struct FAbstractFunctionImplBase; template <typename R, typename... Args> -struct FAbstractFunctionImpl<R(Args...)> - : flib::FCallable<void(R*, Args...)> +struct FAbstractFunctionImplBase<R(Args...)> + : flib::FCallable<void(R *, Args...)> { virtual const std::type_info &target_type() const = 0; - virtual void *target() const = 0; - virtual const std::type_info &target_method_type() const + virtual const std::type_info &method_type() const { return typeid(void); } }; +template <typename, typename> +struct FAbstractFunctionImpl; + +template <typename R, typename... Args, typename Target> +struct FAbstractFunctionImpl<R(Args...), Target> + : flib::detail::FAbstractFunctionImplBase<R(Args...)> +{ + const std::type_info &target_type() const + { + return typeid(Target); + } + + virtual Target target() const = 0; +}; + template <typename, typename, typename> struct FFunctionImpl; template <typename R, typename... Args, typename Target, typename FunctionR, typename... FunctionArgs> struct FFunctionImpl<R(Args...), Target, FunctionR(FunctionArgs...)> - : flib::detail::FAbstractFunctionImpl<R(Args...)> + : flib::detail::FAbstractFunctionImpl<R(Args...), Target> { Target m_target; @@ -88,31 +103,73 @@ struct FFunctionImpl<R(Args...), Target, FunctionR(FunctionArgs...)> : m_target(target) { } - const std::type_info &target_type() const + virtual void operator()(R *ret, Args... args) const override { - return typeid(Target); + adapted(ret, args...); } - virtual void *target() const + template <typename T = R> + inline void adapted(typename std::enable_if<!std::is_void<T>::value, T *>::type ret, FunctionArgs... args, ...) const { - return (void *)(uintptr_t)m_target; + *ret = m_target(args...); + } + + template <typename T = R> + inline void adapted(typename std::enable_if<std::is_void<T>::value, T *>::type ret, FunctionArgs... args, ...) const + { + m_target(args...); } - virtual void operator()(R *ret, Args... args) const + virtual Target target() const override + { + return m_target; + } +}; + +template <typename, typename, typename> +struct FAbstractCallbackImpl; + +template <typename R, typename... Args, typename Target, typename CallbackR, typename CallbackData, typename... CallbackArgs> +struct FAbstractCallbackImpl<R(Args...), Target, CallbackR(CallbackData, CallbackArgs...)> + : flib::detail::FFunctionImpl<R(Args...), Target, CallbackR(CallbackArgs...)> +{ + virtual CallbackData callback_data() const = 0; +}; + +template <typename, typename, typename, typename> +struct FCallbackImpl; + +template <typename R, typename... Args, typename Target, typename CallbackR, typename CallbackData, typename... CallbackArgs, typename CallbackDataHolder> +struct FCallbackImpl<R(Args...), Target, CallbackR(CallbackData, CallbackArgs...), CallbackDataHolder> + : flib::detail::FFunctionImpl<R(Args...), Target, CallbackR(CallbackArgs...)> +{ + CallbackDataHolder m_data; + + FCallbackImpl(Target target, CallbackDataHolder data) + : flib::detail::FFunctionImpl<R(Args...), Target, CallbackR(CallbackArgs...)>(target) + , m_data(data) + { } + + virtual void operator()(R *ret, Args... args) const override { adapted(ret, args...); } template <typename T = R> - inline void adapted(typename std::enable_if<!std::is_void<T>::value, FunctionR *>::type ret, FunctionArgs... args, ...) const + inline void adapted(typename std::enable_if<!std::is_void<T>::value, T *>::type ret, CallbackArgs... args, ...) const { - *ret = m_target(args...); + *ret = m_target(m_data, args...); } template <typename T = R> - inline void adapted(typename std::enable_if<std::is_void<T>::value, void *>::type ret, FunctionArgs... args, ...) const + inline void adapted(typename std::enable_if<std::is_void<T>::value, T *>::type ret, CallbackArgs... args, ...) const { - m_target(args...); + m_target(m_data, args...); + } + + virtual CallbackData callback_data() const override + { + return m_data; } }; @@ -121,7 +178,7 @@ struct FAbstractMethodImpl; template <typename R, typename... Args, typename MethodSignature, typename Target> struct FAbstractMethodImpl<R(Args...), MethodSignature, Target> - : FAbstractFunctionImpl<R(Args...)> + : FAbstractFunctionImpl<R(Args...), Target> { MethodSignature m_method; @@ -129,7 +186,7 @@ struct FAbstractMethodImpl<R(Args...), MethodSignature, Target> : m_method(method) { } - virtual const std::type_info &target_method_type() const + virtual const std::type_info &method_type() const override { return typeid(MethodSignature); } @@ -151,32 +208,27 @@ struct FMethodImpl<R(Args...), MethodSignature, Target, Pointer, MethodR(MethodA , m_target(target) { } - const std::type_info &target_type() const - { - return typeid(Target); - } - - virtual void *target() const - { - return &(*m_target); - } - - virtual void operator()(R *ret, Args... args) const + virtual void operator()(R *ret, Args... args) const override { adapted(ret, args...); } template <typename T = R> - inline void adapted(typename std::enable_if<!std::is_void<T>::value, MethodR *>::type ret, MethodArgs... args, ...) const + inline void adapted(typename std::enable_if<!std::is_void<T>::value, T *>::type ret, MethodArgs... args, ...) const { *ret = (m_target->*(super_type::m_method))(args...); } template <typename T = R> - inline void adapted(typename std::enable_if<std::is_void<T>::value, void *>::type ret, MethodArgs... args, ...) const + inline void adapted(typename std::enable_if<std::is_void<T>::value, T *>::type ret, MethodArgs... args, ...) const { ((*m_target).*(super_type::m_method))(args...); } + + virtual Target target() const override + { + return &(*m_target); + } }; template <typename> @@ -216,7 +268,7 @@ class FFunction; template <typename R, typename... Args> class FFunction<R(Args...)> { - typedef std::shared_ptr<flib::detail::FAbstractFunctionImpl<R(Args...)>> shared_ptr; + typedef std::shared_ptr<flib::detail::FAbstractFunctionImplBase<R(Args...)>> shared_ptr; shared_ptr d; @@ -288,28 +340,26 @@ public: { return d != nullptr ? d->target_type() : typeid(void); } -#if 0 + template <typename T> - inline T *target() + inline typename std::enable_if<!flib::is_const_pointer<T>::value, T>::type target() const { - T *ret = nullptr; + auto imp = dynamic_cast<flib::detail::FAbstractFunctionImpl<R(Args...), T> *>(d.get()); - if(d != nullptr) { - dynamic_cast<flib::detail::FAbstractFunctionImpl<R (Args...)> &>(*d); - } - return nullptr; + return imp != nullptr ? imp->target() : nullptr; } template <typename T> - inline const T *target() const + inline typename std::enable_if<flib::is_const_pointer<T>::value, T>::type target() const { - return d != nullptr ? d->target() : nullptr; + auto imp = dynamic_cast<flib::detail::FAbstractFunctionImpl<R(Args...), T> *>(d.get()); + + return imp != nullptr ? imp->target() : target<typename flib::is_const_pointer<T>::pointer_type>(); } -#endif - template <typename T> - inline const std::type_info &target_method_type() const + + inline const std::type_info &method_type() const { - return d != nullptr ? d->target_method_type() : typeid(void); + return d != nullptr ? d->method_type() : typeid(void); } }; diff --git a/lib/libpacman/util/ftype_traits.h b/lib/libpacman/util/ftype_traits.h new file mode 100644 index 0000000..dede16a --- /dev/null +++ b/lib/libpacman/util/ftype_traits.h @@ -0,0 +1,52 @@ +/* + * ftype_traits.h + * + * Copyright (c) 2014 by Michel Hermier <herm...@frugalware.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef FTYPE_TRAITS_H +#define FTYPE_TRAITS_H + +#include <type_traits> + +namespace flib +{ + +template <typename> +struct is_const_pointer; + +template <typename _Tp> +struct is_const_pointer<_Tp *> + : public std::false_type +{ + typedef _Tp *pointer_type; + typedef _Tp const *const_pointer_type; +}; + +template<typename _Tp> +struct is_const_pointer<_Tp const *> + : public std::true_type +{ + typedef _Tp *pointer_type; + typedef _Tp const *const_pointer_type; +}; + +} + +#endif /* FTYPE_TRAITS_H */ + +/* vim: set ts=2 sw=2 noet: */ _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git