summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa/qcocoaintrospection.mm
blob: 279c8332719fef16f62cf0618cf1f1f6b40425c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (c) 2007-2008, Apple, Inc.
// SPDX-License-Identifier: BSD-3-Clause
// Qt-Security score:significant reason:default

#include "qcocoaintrospection.h"

QT_BEGIN_NAMESPACE

void qt_cocoa_change_implementation(Class baseClass, SEL originalSel, Class proxyClass, SEL replacementSel, SEL backupSel)
{
    // The following code replaces the _implementation_ for the selector we want to hack
    // (originalSel) with the implementation found in proxyClass. Then it creates
    // a new 'backup' method inside baseClass containing the old, original,
    // implementation (fakeSel). You can let the proxy implementation of originalSel
    // call fakeSel if needed (similar approach to calling a super class implementation).
    // fakeSel must also be implemented in proxyClass, as the signature is used
    // as template for the method one we add into baseClass.
    // NB: You will typically never create any instances of proxyClass; we use it
    // only for stealing its contents and put it into baseClass.
    if (!replacementSel)
        replacementSel = originalSel;

    Method originalMethod = class_getInstanceMethod(baseClass, originalSel);
    Method replacementMethod = class_getInstanceMethod(proxyClass, replacementSel);
    IMP originalImp = method_setImplementation(originalMethod, method_getImplementation(replacementMethod));

    if (backupSel) {
        Method backupMethod = class_getInstanceMethod(proxyClass, backupSel);
        class_addMethod(baseClass, backupSel, originalImp, method_getTypeEncoding(backupMethod));
    }
}

void qt_cocoa_change_back_implementation(Class baseClass, SEL originalSel, SEL backupSel)
{
    Method originalMethod = class_getInstanceMethod(baseClass, originalSel);
    Method backupMethodInBaseClass = class_getInstanceMethod(baseClass, backupSel);
    method_setImplementation(originalMethod, method_getImplementation(backupMethodInBaseClass));
}

QT_END_NAMESPACE