From: TimePath Date: Sun, 31 Dec 2017 15:16:35 +0000 (+1100) Subject: Callback: work at any arity X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fnetradiant.git;a=commitdiff_plain;h=f6b4543bd54926411cb71a776bc5b37eea846c4f Callback: work at any arity --- diff --git a/include/icamera.h b/include/icamera.h index e9becbae..36cc24f1 100644 --- a/include/icamera.h +++ b/include/icamera.h @@ -29,7 +29,7 @@ #define INCLUDED_ICAMERA_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" class Matrix4; diff --git a/include/ifilesystem.h b/include/ifilesystem.h index f497528f..605aba29 100644 --- a/include/ifilesystem.h +++ b/include/ifilesystem.h @@ -24,7 +24,7 @@ #include #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef Callback1 ArchiveNameCallback; typedef Callback1 FileNameCallback; diff --git a/include/irender.h b/include/irender.h index 8b0c3ec6..fbba51db 100644 --- a/include/irender.h +++ b/include/irender.h @@ -23,7 +23,7 @@ #define INCLUDED_IRENDER_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" // Rendering states to sort by. diff --git a/include/iselection.h b/include/iselection.h index a5951f9a..898a9aa1 100644 --- a/include/iselection.h +++ b/include/iselection.h @@ -24,7 +24,7 @@ #include #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" #include "signal/signalfwd.h" class Renderer; diff --git a/include/ishaders.h b/include/ishaders.h index ac447090..e66c9c7b 100644 --- a/include/ishaders.h +++ b/include/ishaders.h @@ -23,7 +23,7 @@ #define INCLUDED_ISHADERS_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" enum { diff --git a/include/iundo.h b/include/iundo.h index eb1e8681..25fa1489 100644 --- a/include/iundo.h +++ b/include/iundo.h @@ -27,7 +27,7 @@ #include #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" class UndoMemento { diff --git a/include/mapfile.h b/include/mapfile.h index df1ab36f..7d61f6ed 100644 --- a/include/mapfile.h +++ b/include/mapfile.h @@ -25,7 +25,7 @@ #include #include "iscenegraph.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" const std::size_t MAPFILE_MAX_CHANGES = std::numeric_limits::max(); diff --git a/include/modelskin.h b/include/modelskin.h index a2c219e6..90fef945 100644 --- a/include/modelskin.h +++ b/include/modelskin.h @@ -23,7 +23,7 @@ #define INCLUDED_MODELSKIN_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" class SkinRemap { diff --git a/include/nameable.h b/include/nameable.h index bbc6f899..e295e5c6 100644 --- a/include/nameable.h +++ b/include/nameable.h @@ -23,7 +23,7 @@ #define INCLUDED_NAMEABLE_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef Callback1 NameCallback; diff --git a/include/namespace.h b/include/namespace.h index 9a00793c..f87a2e06 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -23,7 +23,7 @@ #define INCLUDED_NAMESPACE_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef Callback1 NameCallback; typedef Callback1 NameCallbackCallback; diff --git a/include/preferencesystem.h b/include/preferencesystem.h index 45678024..bbc838ad 100644 --- a/include/preferencesystem.h +++ b/include/preferencesystem.h @@ -23,7 +23,7 @@ #define INCLUDED_PREFERENCESYSTEM_H #include "generic/constant.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef Callback1 StringImportCallback; typedef Callback1 StringExportCallback; diff --git a/include/selectable.h b/include/selectable.h index 2b8eab07..1cea909f 100644 --- a/include/selectable.h +++ b/include/selectable.h @@ -26,7 +26,7 @@ #include "generic/vector.h" #include "scenelib.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" class SelectionIntersection { diff --git a/libs/generic/CMakeLists.txt b/libs/generic/CMakeLists.txt index b872d874..d89f5743 100644 --- a/libs/generic/CMakeLists.txt +++ b/libs/generic/CMakeLists.txt @@ -2,7 +2,6 @@ add_library(generic arrayrange.h bitfield.h callback.cpp callback.h - callbackfwd.h constant.cpp constant.h enumeration.h functional.h diff --git a/libs/generic/callback.h b/libs/generic/callback.h index 4110691e..1e8cd46d 100644 --- a/libs/generic/callback.h +++ b/libs/generic/callback.h @@ -27,7 +27,6 @@ #include #include "functional.h" -#include "callbackfwd.h" template inline void* convertToOpaque( Type* t ){ @@ -150,6 +149,9 @@ public: template using BindFirstOpaque = BindFirstOpaqueN>; +/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer. +/// +/// Use with the callback constructors MemberCaller, ConstMemberCaller, ReferenceCaller, ConstReferenceCaller, PointerCaller, ConstPointerCaller and FreeCaller. template class CallbackN; @@ -178,226 +180,205 @@ public: } }; -/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer. -/// -/// Use with the callback constructors MemberCaller, ConstMemberCaller, ReferenceCaller, ConstReferenceCaller, PointerCaller, ConstPointerCaller and FreeCaller. -template -class Callback0 : public CallbackN { -public: - using CallbackN::CallbackN; -}; +namespace detail { + template + struct Arglist; -template -inline Callback0> makeCallback0(const Caller &caller, get_argument callee) { - return Callback0>(BindFirstOpaque(callee)); -} -template -inline Callback0> makeStatelessCallback0(const Caller &caller) { - return makeCallback0( Caller0To1(), 0 ); -} + template + struct Arglist { + using type = R(Head, Ts...); -typedef Callback0 Callback; + template + using unshift = Arglist; + using shift = Arglist; + }; + template + struct Arglist { + using type = R(Ts...); -/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and one other argument. -/// -/// Use with the callback constructors MemberCaller1, ConstMemberCaller1, ReferenceCaller1, ConstReferenceCaller1, PointerCaller1, ConstPointerCaller1 and FreeCaller1. -template -class Callback1 : public CallbackN { -public: - using CallbackN::CallbackN; -}; - -template -inline Callback1, get_result_type> -makeCallback1(const Caller &caller, get_argument callee) { - return Callback1, get_result_type>(BindFirstOpaque(callee)); + template + using unshift = Arglist; + }; } -template -inline Callback1, get_result_type> makeStatelessCallback1(const Caller &caller) { - return makeCallback1( Caller1To2(), 0 ); -} - - -/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and two other arguments. -/// -template -class Callback2 : public CallbackN { -public: - using CallbackN::CallbackN; -}; template -inline Callback2< - get_argument, - get_argument, - get_result_type -> makeCallback2(const Caller &caller, get_argument callee) { - return Callback2< - get_argument, - get_argument, - get_result_type - >(BindFirstOpaque(callee)); +inline CallbackN>::shift::type> makeCallbackN(const Caller &caller, get_argument callee) { + return CallbackN>::shift::type>(BindFirstOpaque(callee)); } + template -inline Callback2< - get_argument, - get_argument, - get_result_type -> makeStatelessCallback2(const Caller &caller) { - return makeCallback2( Caller2To3(), 0 ); +inline CallbackN> makeStatelessCallbackN(const Caller &caller) { + return makeCallbackN(CallerShiftFirst>::template unshift::type>(), nullptr); } +namespace detail { + template + struct MemberFunction; -/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and three other arguments. -/// -template -class Callback3 : public CallbackN { -public: - using CallbackN::CallbackN; -}; - -template -inline Callback3< - get_argument, - get_argument, - get_argument, - get_result_type -> makeCallback3(const Caller &caller, get_argument callee) { - return Callback3< - get_argument, - get_argument, - get_argument, - get_result_type - >(BindFirstOpaque(callee)); -} -template -inline Callback3< - get_argument, - get_argument, - get_argument, - get_result_type -> makeStatelessCallback3(const Caller &caller) { - return makeCallback3( Caller3To4(), 0 ); + template + struct MemberFunction { + using type = R(Object::*)(Ts...); + using type_const = R(Object::*)(Ts...) const; + }; } +template +using MemberFunction = typename detail::MemberFunction::type; + +template +using ConstMemberFunction = typename detail::MemberFunction::type_const; /// \brief Forms a Callback from a non-const Environment reference and a non-const Environment member-function. /// /// \dontinclude generic/callback.cpp /// \skipline MemberCaller example /// \until end example -template -using MemberCaller = BindFirstOpaque>; + +template member> +using MemberCallerN = BindFirstOpaque::template instance>; /// \brief Forms a Callback from a const Environment reference and a const Environment member-function. /// /// \dontinclude generic/callback.cpp /// \skipline MemberCaller example /// \until end example -template -using ConstMemberCaller = BindFirstOpaque>; - -/// \brief Forms a Callback from a non-const Environment reference and a const Environment member-function which takes one argument. -template -using MemberCaller1 = BindFirstOpaque>; - -/// \brief Forms a Callback from a const Environment reference and a const Environment member-function which takes one argument. -template -using ConstMemberCaller1 = BindFirstOpaque>; +template member> +using ConstMemberCallerN = BindFirstOpaque::template instance>; /// \brief Forms a Callback from a non-const Environment reference and a free function which operates on a non-const Environment reference. /// /// \dontinclude generic/callback.cpp /// \skipline ReferenceCaller example /// \until end example -template -using ReferenceCaller = BindFirstOpaque>; +template::template unshift::type *func> +using ReferenceCallerN = BindFirstOpaque::template unshift::type>::template instance>; /// \brief Forms a Callback from a const Environment reference and a free function which operates on a const Environment reference. /// /// \dontinclude generic/callback.cpp /// \skipline ReferenceCaller example /// \until end example +template::template unshift::type *func> +using ConstReferenceCallerN = BindFirstOpaque::template unshift::type>::template instance>; + +/// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer. +template::template unshift::type *func> +using PointerCallerN = BindFirstOpaque::template unshift::type>::template instance>; + +/// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer. +template::template unshift::type *func> +using ConstPointerCallerN = BindFirstOpaque::template unshift::type>::template instance>; + +/// \brief Forms a Callback from a free function +template +class FreeCallerN : public BindFirstOpaque::template instance, + typename detail::Arglist::template unshift::type +>> { +public: + FreeCallerN() + : BindFirstOpaque::template instance, + typename detail::Arglist::template unshift::type + >>(nullptr) { + } +}; + +/// \brief Constructs a Callback1 from a non-const \p functor +/// +/// \param Functor Must define \c first_argument_type and \c operator()(first_argument_type). +template +inline CallbackN> makeCallbackN(Functor &functor) { + return CallbackN>(MemberCallerN, &Functor::operator()>(functor)); +} + +/// \brief Constructs a Callback1 from a const \p functor +/// +/// \param Functor Must define \c first_argument_type and const \c operator()(first_argument_type). +template +inline CallbackN> makeCallbackN(const Functor &functor) { + return CallbackN>(ConstMemberCallerN, &Functor::operator()>(functor)); +} + +// todo: inline + +#define makeCallback makeCallbackN + +using Callback = CallbackN; + +template +using Callback0 = CallbackN; + +template +using Callback1 = CallbackN; + +template +using Callback2 = CallbackN; + +template +using Callback3 = CallbackN; + +#define makeCallback0 makeCallbackN +#define makeStatelessCallback0 makeStatelessCallbackN + +#define makeCallback1 makeCallbackN +#define makeStatelessCallback1 makeStatelessCallbackN + +#define makeCallback2 makeCallbackN +#define makeStatelessCallback2 makeStatelessCallbackN + +#define makeCallback3 makeCallbackN +#define makeStatelessCallback3 makeStatelessCallbackN + +template +using MemberCaller = MemberCallerN; + +template +using ConstMemberCaller = ConstMemberCallerN; + +template +using MemberCaller1 = MemberCallerN; + +template +using ConstMemberCaller1 = ConstMemberCallerN; + +template +using ReferenceCaller = ReferenceCallerN; + template -using ConstReferenceCaller = BindFirstOpaque>; +using ConstReferenceCaller = ConstReferenceCallerN; /// \brief Forms a Callback from a non-const Environment reference and a free function which operates on a non-const Environment reference and one other argument. template -using ReferenceCaller1 = BindFirstOpaque>; +using ReferenceCaller1 = ReferenceCallerN; /// \brief Forms a Callback from a const Environment reference and a free function which operates on a const Environment reference and one other argument. template -using ConstReferenceCaller1 = BindFirstOpaque>; +using ConstReferenceCaller1 = ConstReferenceCallerN; /// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer. template -using PointerCaller = BindFirstOpaque>; +using PointerCaller = PointerCallerN; /// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer. template -using ConstPointerCaller = BindFirstOpaque>; +using ConstPointerCaller = ConstPointerCallerN; /// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer and one other argument. template -using PointerCaller1 = BindFirstOpaque>; +using PointerCaller1 = PointerCallerN; /// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer and one other argument. template -using ConstPointerCaller1 = BindFirstOpaque>; +using ConstPointerCaller1 = ConstPointerCallerN; -/// \brief Forms a Callback from a free function which takes no arguments. template -class FreeCaller : public BindFirstOpaque>> { -public: - FreeCaller() : BindFirstOpaque>>(0) { - } -}; +using FreeCaller = FreeCallerN; -/// \brief Forms a Callback from a free function which takes a single argument. template -class FreeCaller1 : public BindFirstOpaque>> { -public: - FreeCaller1() : BindFirstOpaque>>(0) { - } -}; - - -/// \brief Constructs a Callback from a non-const \p functor with zero arguments. -/// -/// \param Functor Must define \c operator()(). -template -inline Callback makeCallback( Functor& functor ){ - return Callback( MemberCaller( functor ) ); -} - -/// \brief Constructs a Callback from a const \p functor with zero arguments. -/// -/// \param Functor Must define const \c operator()(). -template -inline Callback makeCallback( const Functor& functor ){ - return Callback( ConstMemberCaller( functor ) ); -} - -/// \brief Constructs a Callback1 from a non-const \p functor with one argument. -/// -/// \param Functor Must define \c first_argument_type and \c operator()(first_argument_type). -template -inline Callback1> makeCallback1(Functor &functor) { - typedef get_argument FirstArgument; - return Callback1( MemberCaller1( functor ) ); -} - -/// \brief Constructs a Callback1 from a const \p functor with one argument. -/// -/// \param Functor Must define \c first_argument_type and const \c operator()(first_argument_type). -template -inline Callback1> makeCallback1(const Functor &functor) { - typedef get_argument FirstArgument; - return Callback1( ConstMemberCaller1( functor ) ); -} - +using FreeCaller1 = FreeCallerN; typedef Callback1 BoolImportCallback; typedef Callback1 BoolExportCallback; @@ -414,5 +395,4 @@ typedef Callback1 StringExportCallback; typedef Callback1 SizeImportCallback; typedef Callback1 SizeExportCallback; - #endif diff --git a/libs/generic/callbackfwd.h b/libs/generic/callbackfwd.h deleted file mode 100644 index 48c01d39..00000000 --- a/libs/generic/callbackfwd.h +++ /dev/null @@ -1,18 +0,0 @@ - -#if !defined( INCLUDED_CALLBACKFWD_H ) -#define INCLUDED_CALLBACKFWD_H - -template -class Callback0; -typedef Callback0 Callback; - -template -class Callback1; - -template -class Callback2; - -template -class Callback3; - -#endif diff --git a/libs/generic/functional.h b/libs/generic/functional.h index 8843e363..ecbf6c7d 100644 --- a/libs/generic/functional.h +++ b/libs/generic/functional.h @@ -184,6 +184,8 @@ public: template using FunctorInvoke = FunctorNInvoke>; +// todo: inline + template using Member = typename MemberN::template instance; @@ -223,30 +225,4 @@ using Function3 = typename FunctionN::template instance; template using Function4 = typename FunctionN::template instance; -template -using Caller0To1 = CallerShiftFirst( - FirstArgument -)>; - -template -using Caller1To2 = CallerShiftFirst( - FirstArgument, - get_argument -)>; - -template -using Caller2To3 = CallerShiftFirst( - FirstArgument, - get_argument, - get_argument -)>; - -template -using Caller3To4 = CallerShiftFirst( - FirstArgument, - get_argument, - get_argument, - get_argument -)>; - #endif diff --git a/libs/gtkutil/button.h b/libs/gtkutil/button.h index b7aa4127..cec03b2f 100644 --- a/libs/gtkutil/button.h +++ b/libs/gtkutil/button.h @@ -23,7 +23,7 @@ #define INCLUDED_GTKUTIL_BUTTON_H #include -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef int gint; typedef gint gboolean; diff --git a/libs/gtkutil/menu.h b/libs/gtkutil/menu.h index 9b040450..5d361f29 100644 --- a/libs/gtkutil/menu.h +++ b/libs/gtkutil/menu.h @@ -23,7 +23,7 @@ #define INCLUDED_GTKUTIL_MENU_H #include -#include "generic/callbackfwd.h" +#include "generic/callback.h" typedef int gint; typedef gint gboolean; diff --git a/libs/gtkutil/toolbar.h b/libs/gtkutil/toolbar.h index 66f38805..60515019 100644 --- a/libs/gtkutil/toolbar.h +++ b/libs/gtkutil/toolbar.h @@ -23,7 +23,7 @@ #define INCLUDED_GTKUTIL_TOOLBAR_H #include -#include "generic/callbackfwd.h" +#include "generic/callback.h" class Command; class Toggle; diff --git a/radiant/brushmanip.h b/radiant/brushmanip.h index 8795bf4e..0654fd89 100644 --- a/radiant/brushmanip.h +++ b/radiant/brushmanip.h @@ -25,7 +25,7 @@ #include #include #include "string/stringfwd.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" enum EBrushPrefab { diff --git a/radiant/selection.h b/radiant/selection.h index 1082c002..b8f4fcfb 100644 --- a/radiant/selection.h +++ b/radiant/selection.h @@ -23,7 +23,7 @@ #define INCLUDED_SELECTION_H #include "windowobserver.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" struct rect_t { diff --git a/radiant/textures.h b/radiant/textures.h index 297ebc99..cdd1a9b1 100644 --- a/radiant/textures.h +++ b/radiant/textures.h @@ -22,7 +22,7 @@ #if !defined ( INCLUDED_TEXTURES_H ) #define INCLUDED_TEXTURES_H -#include "generic/callbackfwd.h" +#include "generic/callback.h" void Textures_Realise(); void Textures_Unrealise(); diff --git a/radiant/texwindow.h b/radiant/texwindow.h index 15ec907c..9a153c44 100644 --- a/radiant/texwindow.h +++ b/radiant/texwindow.h @@ -24,7 +24,7 @@ #include #include "math/vector.h" -#include "generic/callbackfwd.h" +#include "generic/callback.h" #include "signal/signalfwd.h" #include "xml/xmltextags.h" @@ -44,8 +44,6 @@ void TextureBrowser_Construct(); void TextureBrowser_Destroy(); typedef Callback1 StringImportCallback; -template -class FreeCaller1; extern ui::Widget g_page_textures; void TextureBrowser_exportTitle( const StringImportCallback& importer );