File: CComPtr.h

package info (click to toggle)
procdump 2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,900 kB
  • sloc: cpp: 42,512; ansic: 4,610; sh: 1,447; makefile: 107; cs: 76
file content (81 lines) | stat: -rw-r--r-- 1,578 bytes parent folder | download | duplicates (2)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma once

template<class TInterface>
class CComPtr
{
private:
    TInterface* pointer;
public:
    CComPtr(const CComPtr&) = delete; // Copy constructor
    CComPtr& operator= (const CComPtr&) = delete; // Copy assignment
    CComPtr(CComPtr&&) = delete; // Move constructor
    CComPtr& operator= (CComPtr&&) = delete; // Move assignment

    void* operator new(std::size_t) = delete;
    void* operator new[](std::size_t) = delete;

    void operator delete(void *ptr) = delete;
    void operator delete[](void *ptr) = delete;

    CComPtr()
    {
        this->pointer = nullptr;
    }

    ~CComPtr()
    {
        if (this->pointer)
        {
            this->pointer->Release();
            this->pointer = nullptr;
        }
    }

    operator TInterface*()
    {
        return this->pointer;
    }

    operator TInterface*() const
    {
        return this->pointer;
    }

    TInterface& operator *()
    {
        return *this->pointer;
    }

    TInterface& operator *() const
    {
        return *this->pointer;
    }

    TInterface** operator&()
    {
        return &this->pointer;
    }

    TInterface** operator&() const
    {
        return &this->pointer;
    }

    TInterface* operator->()
    {
        return this->pointer;
    }

    TInterface* operator->() const
    {
        return this->pointer;
    }

    void Release()
    {
        this->~CComPtr();
    }
};