Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_TOOLS_LINK_HXX
21 : #define INCLUDED_TOOLS_LINK_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : #include <sal/types.h>
26 :
27 : #define DECL_LINK(Member, ArgType) \
28 : static sal_IntPtr LinkStub##Member(void *, void *); \
29 : sal_IntPtr Member(ArgType)
30 :
31 : #define DECL_STATIC_LINK(Class, Member, ArgType) \
32 : static sal_IntPtr LinkStub##Member(void *, void *); \
33 : static sal_IntPtr Member(Class *, ArgType)
34 :
35 : #define DECL_DLLPRIVATE_LINK(Member, ArgType) \
36 : SAL_DLLPRIVATE static sal_IntPtr LinkStub##Member(void *, void *); \
37 : SAL_DLLPRIVATE sal_IntPtr Member(ArgType)
38 :
39 : #define DECL_DLLPRIVATE_STATIC_LINK(Class, Member, ArgType) \
40 : SAL_DLLPRIVATE static sal_IntPtr LinkStub##Member(void *, void *); \
41 : SAL_DLLPRIVATE static sal_IntPtr Member(Class *, ArgType)
42 :
43 : #define IMPL_LINK(Class, Member, ArgType, ArgName) \
44 : sal_IntPtr Class::LinkStub##Member(void * instance, void * data) { \
45 : return static_cast<Class *>(instance)->Member( \
46 : static_cast<ArgType>(data)); \
47 : } \
48 : sal_IntPtr Class::Member(ArgType ArgName)
49 :
50 : #define IMPL_LINK_NOARG(Class, Member) \
51 : sal_IntPtr Class::LinkStub##Member(void * instance, void * data) { \
52 : return static_cast<Class *>(instance)->Member(data); \
53 : } \
54 : sal_IntPtr Class::Member(SAL_UNUSED_PARAMETER void *)
55 :
56 : #define IMPL_STATIC_LINK(Class, Member, ArgType, ArgName) \
57 : sal_IntPtr Class::LinkStub##Member(void * instance, void * data) { \
58 : return Member( \
59 : static_cast<Class *>(instance), static_cast<ArgType>(data)); \
60 : } \
61 : sal_IntPtr Class::Member(SAL_UNUSED_PARAMETER Class *, ArgType ArgName)
62 :
63 : #define IMPL_STATIC_LINK_NOARG(Class, Member) \
64 : sal_IntPtr Class::LinkStub##Member(void * instance, void * data) { \
65 : return Member(static_cast<Class *>(instance), data); \
66 : } \
67 : sal_IntPtr Class::Member( \
68 : SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER void *)
69 :
70 : #define DECL_LINK_TYPED(Member, ArgType, RetType) \
71 : static RetType LinkStub##Member(void *, ArgType); \
72 : RetType Member(ArgType)
73 :
74 : #define DECL_STATIC_LINK_TYPED(Class, Member, ArgType, RetType) \
75 : static RetType LinkStub##Member(void *, ArgType); \
76 : static RetType Member(Class *, ArgType)
77 :
78 : #define DECL_DLLPRIVATE_LINK_TYPED(Member, ArgType, RetType) \
79 : SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \
80 : SAL_DLLPRIVATE RetType Member(ArgType)
81 :
82 : #define DECL_DLLPRIVATE_STATIC_LINK_TYPED(Class, Member, ArgType, RetType) \
83 : SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \
84 : SAL_DLLPRIVATE static RetType Member(Class *, ArgType)
85 :
86 : #define IMPL_LINK_TYPED(Class, Member, ArgType, ArgName, RetType) \
87 : RetType Class::LinkStub##Member(void * instance, ArgType data) { \
88 : return static_cast<Class *>(instance)->Member(data); \
89 : } \
90 : RetType Class::Member(ArgType ArgName)
91 :
92 : #define IMPL_LINK_NOARG_TYPED(Class, Member, ArgType, RetType) \
93 : RetType Class::LinkStub##Member(void * instance, ArgType data) { \
94 : return static_cast<Class *>(instance)->Member(data); \
95 : } \
96 : RetType Class::Member(SAL_UNUSED_PARAMETER ArgType)
97 :
98 : #define IMPL_STATIC_LINK_TYPED( \
99 : Class, Member, ArgType, ArgName, RetType) \
100 : RetType Class::LinkStub##Member(void * instance, ArgType data) { \
101 : return Member(static_cast<Class *>(instance), data); \
102 : } \
103 : RetType Class::Member(SAL_UNUSED_PARAMETER Class *, ArgType ArgName)
104 :
105 : #define IMPL_STATIC_LINK_NOARG_TYPED( \
106 : Class, Member, ArgType, RetType) \
107 : RetType Class::LinkStub##Member(void * instance, ArgType data) { \
108 : return Member(static_cast<Class *>(instance), data); \
109 : } \
110 : RetType Class::Member( \
111 : SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER ArgType)
112 :
113 : #define LINK(Instance, Class, Member) ::tools::detail::makeLink( \
114 : static_cast<Class *>(Instance), &Class::LinkStub##Member)
115 :
116 : template<typename Arg = void *, typename Ret = sal_IntPtr>
117 : class SAL_WARN_UNUSED Link {
118 : public:
119 : typedef Ret Stub(void *, Arg);
120 :
121 3560970 : Link(): function_(nullptr), instance_(nullptr) {}
122 :
123 1793551 : Link(void * instance, Stub * function):
124 1793551 : function_(function), instance_(instance) {}
125 :
126 39946404 : Ret Call(Arg data) const
127 39946404 : { return function_ == nullptr ? Ret() : (*function_)(instance_, data); }
128 :
129 5727346 : bool IsSet() const { return function_ != nullptr; }
130 :
131 0 : bool operator !() const { return !IsSet(); }
132 :
133 : bool operator <(Link const & other) const {
134 : return reinterpret_cast<sal_uIntPtr>(function_)
135 : < reinterpret_cast<sal_uIntPtr>(other.function_);
136 : };
137 :
138 15787925 : bool operator ==(Link const & other) const
139 15787925 : { return function_ == other.function_ && instance_ == other.instance_; };
140 :
141 : bool operator !=(Link const & other) const { return !operator ==(other); };
142 68676 : void *GetInstance() const { return instance_; }
143 :
144 : private:
145 : Stub * function_;
146 : void * instance_;
147 : };
148 :
149 : namespace tools { namespace detail {
150 :
151 : template<typename Arg, typename Ret>
152 1790938 : Link<Arg, Ret> makeLink(void * instance, Ret (* function)(void *, Arg)) {
153 1790938 : return Link<Arg, Ret>(instance, function);
154 : }
155 :
156 : } }
157 :
158 : #endif
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|