android: Update app-specific/MIME type icons
[LibreOffice.git] / include / tools / link.hxx
blobec88bf95335218deea33bcfc4fd000fb579fc763
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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); \
47 } \
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); \
53 } \
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); \
60 } \
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); \
67 } \
68 RetType Class::Member( \
69 SAL_UNUSED_PARAMETER Class *, SAL_UNUSED_PARAMETER ArgType)
71 #ifdef DBG_UTIL
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))
74 #else
75 #define LINK(Instance, Class, Member) ::tools::detail::makeLink( \
76 ::tools::detail::castTo<Class *>(Instance), &Class::LinkStub##Member)
77 #endif
79 template<typename Arg, typename Ret>
80 class SAL_WARN_UNUSED Link {
81 public:
82 typedef Ret Stub(void *, Arg);
84 #ifdef DBG_UTIL
85 Link()
86 : function_(nullptr)
87 , instance_(nullptr)
88 , file_("unknown")
89 , line_(0)
90 , target_("unknown")
94 Link(void* instance, Stub* function, const char* const file = "unknown", const int line = 0,
95 const char* const target = "unknown")
96 : function_(function)
97 , instance_(instance)
98 , file_(file)
99 , line_(line)
100 , target_(target)
103 #else
104 Link(): function_(nullptr), instance_(nullptr) {}
106 Link(void * instance, Stub * function):
107 function_(function), instance_(instance) {}
108 #endif
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_);
120 if (ptr1 < ptr2)
121 return true;
122 else if (ptr1 > ptr2)
123 return false;
124 else
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_; }
133 #ifdef DBG_UTIL
134 const char* getSourceFilename() const { return file_; }
135 int getSourceLineNumber() const { return line_; }
136 const char* getTargetName() const { return target_; }
137 #endif
139 private:
140 Stub * function_;
141 void * instance_;
143 #ifdef DBG_UTIL
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).
148 const char* file_;
149 int line_;
150 const char* target_;
151 #endif
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); }
164 #ifdef DBG_UTIL
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);
169 #else
170 template<typename Arg, typename Ret>
171 Link<Arg, Ret> makeLink(void * instance, Ret (* function)(void *, Arg)) {
172 return Link<Arg, Ret>(instance, function);
174 #endif
178 #endif
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */