1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_TOOLS_LINK_HXX
21 #define INCLUDED_TOOLS_LINK_HXX
23 #include <sal/config.h>
25 #include <sal/macros.h>
26 #include <sal/types.h>
28 #define DECL_LINK(Member, ArgType, RetType) \
29 static RetType LinkStub##Member(void *, ArgType); \
30 RetType Member(ArgType)
32 #define DECL_STATIC_LINK(Class, Member, ArgType, RetType) \
33 static RetType LinkStub##Member(void *, ArgType); \
34 static RetType Member(Class *, ArgType)
36 #define DECL_DLLPRIVATE_LINK(Member, ArgType, RetType) \
37 SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \
38 SAL_DLLPRIVATE RetType Member(ArgType)
40 #define DECL_DLLPRIVATE_STATIC_LINK(Class, Member, ArgType, RetType) \
41 SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \
42 SAL_DLLPRIVATE static RetType Member(Class *, ArgType)
44 #define IMPL_LINK(Class, Member, ArgType, ArgName, RetType) \
45 RetType Class::LinkStub##Member(void * instance, ArgType data) { \
46 return static_cast<Class *>(instance)->Member(data); \
48 RetType Class::Member(ArgType ArgName)
50 #define IMPL_LINK_NOARG(Class, Member, ArgType, RetType) \
51 RetType Class::LinkStub##Member(void * instance, ArgType data) { \
52 return static_cast<Class *>(instance)->Member(data); \
54 RetType Class::Member(SAL_UNUSED_PARAMETER ArgType)
56 #define IMPL_STATIC_LINK( \
57 Class, Member, ArgType, ArgName, RetType) \
58 RetType Class::LinkStub##Member(void * instance, ArgType data) { \
59 return Member(static_cast<Class *>(instance), data); \
61 RetType Class::Member(SAL_UNUSED_PARAMETER Class *, ArgType ArgName)
63 #define IMPL_STATIC_LINK_NOARG( \
64 Class, Member, ArgType, RetType) \
65 RetType Class::LinkStub##Member(void * instance, ArgType data) { \
66 return Member(static_cast<Class *>(instance), data); \
68 RetType Class::Member( \
69 SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER ArgType)
72 #define LINK(Instance, Class, Member) ::tools::detail::makeLink( \
73 ::tools::detail::castTo<Class *>(Instance), &Class::LinkStub##Member, __FILE__, __LINE__, SAL_STRINGIFY(Class::LinkStub##Member))
75 #define LINK(Instance, Class, Member) ::tools::detail::makeLink( \
76 ::tools::detail::castTo<Class *>(Instance), &Class::LinkStub##Member)
79 template<typename Arg
, typename Ret
>
80 class SAL_WARN_UNUSED Link
{
82 typedef Ret
Stub(void *, Arg
);
94 Link(void* instance
, Stub
* function
, const char* const file
= "unknown", const int line
= 0,
95 const char* const target
= "unknown")
104 Link(): function_(nullptr), instance_(nullptr) {}
106 Link(void * instance
, Stub
* function
):
107 function_(function
), instance_(instance
) {}
110 Ret
Call(Arg data
) const
111 { return function_
== nullptr ? Ret() : (*function_
)(instance_
, data
); }
113 bool IsSet() const { return function_
!= nullptr; }
115 bool operator !() const { return !IsSet(); }
117 bool operator <(Link
const & other
) const {
118 char* ptr1
= reinterpret_cast<char*>(function_
);
119 char* ptr2
= reinterpret_cast<char*>(other
.function_
);
122 else if (ptr1
> ptr2
)
125 return instance_
< other
.instance_
;
128 bool operator ==(Link
const & other
) const
129 { return function_
== other
.function_
&& instance_
== other
.instance_
; };
131 void *GetInstance() const { return instance_
; }
134 const char* getSourceFilename() const { return file_
; }
135 int getSourceLineNumber() const { return line_
; }
136 const char* getTargetName() const { return target_
; }
144 /// Support tracing link source and target.
145 /// When debugging async events, it's often critical
146 /// to find out not only where a link leads (i.e. the target
147 /// function), but also where it was created (file:line).
154 // Class used to indicate that the Call() parameter is not in use:
155 class LinkParamNone
{ LinkParamNone() = delete; };
157 namespace tools::detail
{
159 // Avoids loplugin:redundantcast in LINK macro, in the common case that Instance
160 // is already of type Class * (instead of a derived type):
161 template<typename To
, typename From
> To
castTo(From from
)
162 { return static_cast<To
>(from
); }
165 template<typename Arg
, typename Ret
>
166 Link
<Arg
, Ret
> makeLink(void * instance
, Ret (* function
)(void *, Arg
), const char* file
, int line
, const char* target
) {
167 return Link
<Arg
, Ret
>(instance
, function
, file
, line
, target
);
170 template<typename Arg
, typename Ret
>
171 Link
<Arg
, Ret
> makeLink(void * instance
, Ret (* function
)(void *, Arg
)) {
172 return Link
<Arg
, Ret
>(instance
, function
);
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */