diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2015-04-30 10:20:00 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2015-04-30 10:20:00 +0200 |
commit | 3ead3ad52f9bb2f9d1d6cf8dfc73a0a25e6778ed (patch) | |
tree | bdfd28afe5a452060e3d985c5f01b45f4b7bc2cd /include/tools/link.hxx | |
parent | 57d254d42b6e1d836bd21e6fb2e968af2b511c7d (diff) |
Gradually typed Link
Turn the Link class into a template abstracting over the link's argument and
return types, but provide default template arguments that keep the generic,
unsafe "void* in, sal_IntPtr out" behvior. That way, individual uses of the
Link class can be updated over time.
All the related macros are duplicated with ..._TYPED counterparts, that
additionally take the RetType (except for LINK_TYPED, which manages to infer the
relevant types from the supplied Member).
(It would have been attractive to change the "untyped" LinkStubs from taking a
void* to a properly typed ArgType parameter, too, but that would cause
-fsanitize=function to flag uses of "untyped" Link::Call.)
Change-Id: I3b0140378bad99abbf240140ebb4a46a05d2d2f8
Diffstat (limited to 'include/tools/link.hxx')
-rw-r--r-- | include/tools/link.hxx | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/include/tools/link.hxx b/include/tools/link.hxx index 2dd0100096ba..99be32b754f8 100644 --- a/include/tools/link.hxx +++ b/include/tools/link.hxx @@ -76,21 +76,73 @@ SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER void *) #define LINK(Instance, Class, Member) \ - Link(static_cast<Class *>(Instance), &Class::LinkStub##Member) + Link<>(static_cast<Class *>(Instance), &Class::LinkStub##Member) + +#define DECL_LINK_TYPED(Member, ArgType, RetType) \ + static RetType LinkStub##Member(void *, ArgType); \ + RetType Member(ArgType) + +#define DECL_STATIC_LINK_TYPED(Class, Member, ArgType, RetType) \ + static RetType LinkStub##Member(void *, ArgType); \ + static RetType Member(Class *, ArgType) + +#define DECL_DLLPRIVATE_LINK_TYPED(Member, ArgType, RetType) \ + SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \ + SAL_DLLPRIVATE RetType Member(ArgType) + +#define DECL_DLLPRIVATE_STATIC_LINK_TYPED(Class, Member, ArgType, RetType) \ + SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \ + SAL_DLLPRIVATE static RetType Member(Class *, ArgType) + +#define IMPL_LINK_TYPED(Class, Member, ArgType, ArgName, RetType) \ + RetType Class::LinkStub##Member(void * instance, ArgType) { \ + return static_cast<Class *>(instance)->Member(data); \ + } \ + RetType Class::Member(ArgType ArgName) + +#define IMPL_LINK_NOARG_TYPED(Class, Member, RetType) \ + RetType Class::LinkStub##Member(void * instance, void * data) { \ + return static_cast<Class *>(instance)->Member(data); \ + } \ + RetType Class::Member(SAL_UNUSED_PARAMETER void *) + +#define IMPL_STATIC_LINK_TYPED(Class, Member, ArgType, ArgName, RetType) \ + RetType Class::LinkStub##Member(void * instance, ArgType data) { \ + return Member(static_cast<Class *>(instance), data); \ + } \ + RetType Class::Member(Class * pThis, ArgType ArgName) + +#define IMPL_STATIC_LINK_NOINSTANCE_TYPED( \ + Class, Member, ArgType, ArgName, RetType) \ + RetType Class::LinkStub##Member(void * instance, ArgType data) { \ + return Member(static_cast<Class *>(instance), data); \ + } \ + RetType Class::Member(SAL_UNUSED_PARAMETER Class *, ArgType ArgName) + +#define IMPL_STATIC_LINK_NOINSTANCE_NOARG_TYPED(Class, Member, RetType) \ + RetType Class::LinkStub##Member(void * instance, void * data) { \ + return Member(static_cast<Class *>(instance), data); \ + } \ + RetType Class::Member( \ + SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER void *) + +#define LINK_TYPED(Instance, Class, Member) tools::detail::makeLink( \ + static_cast<Class *>(Instance), &Class::LinkStub##Member) #define EMPTYARG +template<typename Arg = void *, typename Ret = sal_IntPtr> class TOOLS_DLLPUBLIC Link { public: - typedef sal_IntPtr Stub(void *, void *); + typedef Ret Stub(void *, Arg); Link(): function_(nullptr), instance_(nullptr) {} Link(void * instance, Stub * function): function_(function), instance_(instance) {} - sal_IntPtr Call(void * data) const - { return function_ == nullptr ? 0 : (*function_)(instance_, data); } + Ret Call(Arg data) const + { return function_ == nullptr ? Ret{} : (*function_)(instance_, data); } bool IsSet() const { return function_ != nullptr; } @@ -111,6 +163,15 @@ private: void * instance_; }; +namespace tools { namespace detail { + +template<typename Arg, typename Ret> +Link<Arg, Ret> makeLink(void * instance, Ret (* function)(void *, Arg)) { + return Link<Arg, Ret>(instance, function); +} + +} } + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |