File: event.h

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (104 lines) | stat: -rw-r--r-- 2,923 bytes parent folder | download | duplicates (4)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/msw/wrl/event.h
// Purpose:     WRL event callback implementation
// Author:      nns52k
// Created:     2021-02-25
// Copyright:   (c) 2021 wxWidgets team
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_MSW_PRIVATE_WRL_H_
#define _WX_MSW_PRIVATE_WRL_H_

#include <atomic>

template <typename baseT, typename ...argTs>
class CInvokable : public baseT
{
public:
    CInvokable() : m_nRefCount(0) {}
    virtual ~CInvokable() {}
    // IUnknown methods
    HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObj) override
    {
        if (riid == __uuidof(baseT) || riid == IID_IUnknown)
        {
            *ppvObj = this;
            AddRef();
            return S_OK;
        }

        *ppvObj = NULL;
        return E_NOINTERFACE;
    }
    ULONG STDMETHODCALLTYPE AddRef() override
    {
        return ++m_nRefCount;
    }
    ULONG STDMETHODCALLTYPE Release() override
    {
        size_t ret = --m_nRefCount;
        if (ret == 0)
            delete this;
        return ret;
    }
private:
    std::atomic<size_t> m_nRefCount;
};

template <typename baseT, typename ...argTs>
class CInvokableLambda : public CInvokable<baseT, argTs...>
{
public:
    CInvokableLambda(std::function<HRESULT(argTs...)> lambda)
        : m_lambda(lambda)
    {}
    // baseT method
    HRESULT STDMETHODCALLTYPE Invoke(argTs ...args) override
    {
        return m_lambda(args...);
    }
private:
    std::function<HRESULT(argTs...)> m_lambda;
};

template <typename baseT, typename contextT, typename ...argTs>
class CInvokableMethod : public CInvokable<baseT, argTs...>
{
public:
    CInvokableMethod(contextT* ctx, HRESULT(contextT::* mthd)(argTs...))
        : m_ctx(ctx), m_mthd(mthd)
    {}
    // baseT method
    HRESULT STDMETHODCALLTYPE Invoke(argTs ...args) override
    {
        return (m_ctx->*m_mthd)(args...);
    }
private:
    contextT* m_ctx;
    HRESULT(contextT::* m_mthd)(argTs...);
};

// the function templates to generate concrete classes from above class templates
template <
    typename baseT, typename lambdaT, // base type & lambda type
    typename LT, typename ...argTs // for capturing argument types of lambda
>
wxCOMPtr<baseT> Callback_impl(lambdaT&& lambda, HRESULT(LT::*)(argTs...) const)
{
    return wxCOMPtr<baseT>(new CInvokableLambda<baseT, argTs...>(lambda));
}

template <typename baseT, typename lambdaT>
wxCOMPtr<baseT> Callback(lambdaT&& lambda)
{
    return Callback_impl<baseT>(std::move(lambda), &lambdaT::operator());
}

template <typename baseT, typename contextT, typename ...argTs>
wxCOMPtr<baseT> Callback(contextT* ctx, HRESULT(contextT::* mthd)(argTs...))
{
    return wxCOMPtr<baseT>(new CInvokableMethod<baseT, contextT, argTs...>(ctx, mthd));
}

#endif // _WX_MSW_PRIVATE_WRL_H_