forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCefRefPtr.h
141 lines (122 loc) · 3.49 KB
/
MCefRefPtr.h
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
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#pragma once
namespace CefSharp
{
namespace Internals
{
// This class appears to be required in order
// to continue reference counting in managed classes
// that the compiler won't let contain CefRefPtr<...>
template <typename T>
ref class MCefRefPtr sealed
{
private:
T* _ptr;
public:
MCefRefPtr() : _ptr(NULL) {}
MCefRefPtr(T* p) : _ptr(p)
{
if (_ptr)
{
_ptr->AddRef();
}
}
MCefRefPtr(const MCefRefPtr<T>% r) : _ptr(r._ptr)
{
if (_ptr)
{
_ptr->AddRef();
}
}
MCefRefPtr(const CefRefPtr<T>& r) : _ptr(r.get())
{
if (_ptr)
{
_ptr->AddRef();
}
}
!MCefRefPtr()
{
if (_ptr)
{
_ptr->Release();
// Be paranoid about preventing a double release
// from this managed instance.
_ptr = nullptr;
}
}
~MCefRefPtr()
{
// Normally, we would invoke the finalizer method here
// via !classname, however... the overloaded -> operator
// prevents that from being feasible.
if (_ptr)
{
_ptr->Release();
// Be paranoid about preventing a double release
// from this managed instance.
_ptr = nullptr;
}
}
T* get()
{
return _ptr;
}
/*
* commented out for now as this operator interferes with the
* return statement of MCefRefPtr<T>% operator=(T* p)
operator T*()
{
return _ptr;
}
*/
T* operator->()
{
return _ptr;
}
MCefRefPtr<T>% operator=(T* p)
{
// AddRef first so that self assignment should work
if (p)
{
p->AddRef();
}
if (_ptr)
{
_ptr->Release();
}
_ptr = p;
return *this;
}
MCefRefPtr<T>% operator=(const MCefRefPtr<T>% r)
{
return %this = r._ptr;
}
void swap(T** pp)
{
T* p = _ptr;
_ptr = *pp;
*pp = p;
}
void swap(MCefRefPtr<T>% r)
{
swap(%r._ptr);
}
virtual bool Equals(Object^ obj) override
{
if (obj->GetType() == GetType())
{
MCefRefPtr^ other = safe_cast<MCefRefPtr^>(obj);
return (*other)._ptr == _ptr;
}
return false;
}
virtual int GetHashCode() override
{
return (int)_ptr;
}
};
}
}