LibreOffice Module tools (master) 1
link.hxx
Go to the documentation of this file.
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/macros.h>
26#include <sal/types.h>
27
28#define DECL_LINK(Member, ArgType, RetType) \
29 static RetType LinkStub##Member(void *, ArgType); \
30 RetType Member(ArgType)
31
32#define DECL_STATIC_LINK(Class, Member, ArgType, RetType) \
33 static RetType LinkStub##Member(void *, ArgType); \
34 static RetType Member(Class *, ArgType)
35
36#define DECL_DLLPRIVATE_LINK(Member, ArgType, RetType) \
37 SAL_DLLPRIVATE static RetType LinkStub##Member(void *, ArgType); \
38 SAL_DLLPRIVATE RetType Member(ArgType)
39
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)
43
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)
49
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)
55
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)
62
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)
70
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
78
79template<typename Arg, typename Ret>
81public:
82 typedef Ret Stub(void *, Arg);
83
84#ifdef DBG_UTIL
85 Link()
86 : function_(nullptr)
87 , instance_(nullptr)
88 , file_("unknown")
89 , line_(0)
90 , target_("unknown")
91 {
92 }
93
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)
101 {
102 }
103#else
104 Link(): function_(nullptr), instance_(nullptr) {}
105
106 Link(void * instance, Stub * function):
107 function_(function), instance_(instance) {}
108#endif
109
110 Ret Call(Arg data) const
111 { return function_ == nullptr ? Ret() : (*function_)(instance_, data); }
112
113 bool IsSet() const { return function_ != nullptr; }
114
115 bool operator !() const { return !IsSet(); }
116
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_;
126 };
127
128 bool operator ==(Link const & other) const
129 { return function_ == other.function_ && instance_ == other.instance_; };
130
131 void *GetInstance() const { return instance_; }
132
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
138
139private:
140 Stub * function_;
141 void * instance_;
142
143#ifdef DBG_UTIL
148 const char* file_;
149 int line_;
150 const char* target_;
151#endif
152};
153
154// Class used to indicate that the Call() parameter is not in use:
155class LinkParamNone { LinkParamNone() = delete; };
156
157namespace tools::detail {
158
159// Avoids loplugin:redundantcast in LINK macro, in the common case that Instance
160// is already of type Class * (instead of a derived type):
161template<typename To, typename From> To castTo(From from)
162{ return static_cast<To>(from); }
163
164#ifdef DBG_UTIL
165template<typename Arg, typename Ret>
166Link<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);
168}
169#else
170template<typename Arg, typename Ret>
171Link<Arg, Ret> makeLink(void * instance, Ret (* function)(void *, Arg)) {
172 return Link<Arg, Ret>(instance, function);
173}
174#endif
175
176}
177
178#endif
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:806
bool operator<(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:818
UBlockCode from
LinkParamNone()=delete
line
Call
Link< Arg, Ret > makeLink(void *instance, Ret(*function)(void *, Arg), const char *file, int line, const char *target)
Definition: link.hxx:166
To castTo(From from)
Definition: link.hxx:161
#define SAL_WARN_UNUSED
rtl::Reference< MappedFile > file_