summaryrefslogtreecommitdiff
path: root/src/player/backends/utility/com_ptr.h
blob: 519caefe6da620e40ce86495c56a996f57eaa1fe (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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# pragma once

template <typename T>
class com_ptr
{
//private:
//    typedef void (com_ptr::*bool_type)() const;
//    void safe_bool() const {}
//
//
public:
    com_ptr():
        _ptr(nullptr)
    {
    }

    com_ptr(T* ptr, bool add_ref = false):
        _ptr(ptr)
    {
        if (_ptr && add_ref)
            _ptr->AddRef();
    }

    com_ptr(const com_ptr& rhs):
        _ptr(rhs._ptr)
    {
        if (_ptr)
            _ptr->AddRef();
    }

    com_ptr(com_ptr&& rhs):
        _ptr(rhs._ptr)
    {
        rhs._ptr = nullptr;
    }

    template <typename U>
    com_ptr(const com_ptr<U>& rhs):
        _ptr(rhs._ptr)
    {
        if (_ptr)
            _ptr->AddRef();
    }

    ~com_ptr()
    {
        if (_ptr)
            _ptr->Release();
    }

    com_ptr& operator = (const com_ptr& rhs)
    {
        com_ptr(rhs).swap(*this);
        return *this;
    }

    com_ptr& operator = (com_ptr&& rhs)
    {
        com_ptr(static_cast<com_ptr&&>(rhs)).swap(*this);
        return *this;
    }

    template <typename U>
    com_ptr& operator = (const com_ptr<U>& rhs)
    {
        com_ptr(rhs).swap(*this):
        return *this;
    }

    com_ptr& operator = (T* rhs)
    {
        com_ptr(rhs).swap(*this);
        return *this;
    }

    //operator bool_type() const
    //{
    //    return _ptr ? &com_ptr::safe_bool : nullptr;
    //}

    //bool_type operator !() const
    //{
    //    return !((bool_type)*this);
    //}

    void reset()
    {
        com_ptr().swap(*this);
    }

    void reset(T* rhs)
    {
        com_ptr(rhs).swap(*this);
    }

    void reset(T* rhs, bool add_ref)
    {
        com_ptr(rhs, add_ref).swap(*this);
    }

    T* get() const
    {
        return _ptr;
    }

    T* detach()
    {
        auto result = _ptr;
        _ptr = nullptr;
        return result;
    }

    void swap(com_ptr& rhs)
    {
        T* temp = rhs._ptr;
        rhs._ptr = _ptr;
        _ptr = temp;
    }

    T&  operator *  () const { return *_ptr; }
    T*  operator -> () const { return  _ptr; }

        operator T* () const { return  _ptr; }
    T** operator &  ()       { return &_ptr; }

private:
    T*      _ptr;
};

template <typename T, typename U> inline bool operator==(const com_ptr<T>& a, const com_ptr<U>& b) { return a.get() == b.get(); }
template <typename T, typename U> inline bool operator!=(const com_ptr<T>& a, const com_ptr<U>& b) { return a.get() != b.get(); }
template <typename T, typename U> inline bool operator==(const com_ptr<T>& a, U* b) { return a.get() == b; }
template <typename T, typename U> inline bool operator!=(const com_ptr<T>& a, U* b) { return a.get() != b; }
template <typename T, typename U> inline bool operator==(T* a, const com_ptr<U>& b) { return a == b.get(); }
template <typename T, typename U> inline bool operator!=(T* a, const com_ptr<U>& b) { return a != b.get(); }
template <typename T> inline bool operator==(const com_ptr<T>& p, std::nullptr_t) { return p.get() == nullptr; }
template <typename T> inline bool operator==(std::nullptr_t, const com_ptr<T>& p) { return p.get() == nullptr; }
template <typename T> inline bool operator!=(const com_ptr<T>& p, std::nullptr_t) { return p.get() != nullptr; }
template <typename T> inline bool operator!=(std::nullptr_t, const com_ptr<T>& p) { return p.get() != nullptr; }
template <typename T> inline bool operator<(const com_ptr<T>& a, const com_ptr<T>& b) { return std::less<T*>()(a.get(), b.get()); }
template <typename T> inline bool operator<=(const com_ptr<T>& a, const com_ptr<T>& b) { return std::less_equal<T*>()(a.get(), b.get()); }
template <typename T> inline bool operator>(const com_ptr<T>& a, const com_ptr<T>& b) { return std::greater<T*>()(a.get(), b.get()); }
template <typename T> inline bool operator>=(const com_ptr<T>& a, const com_ptr<T>& b) { return std::greater_equal<T*>()(a.get(), b.get()); }
template <typename T> void swap(com_ptr<T> & lhs, com_ptr<T> & rhs) { lhs.swap(rhs); }