comptr.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 The ClanLib Team
4**
5** This software is provided 'as-is', without any express or implied
6** warranty. In no event will the authors be held liable for any damages
7** arising from the use of this software.
8**
9** Permission is granted to anyone to use this software for any purpose,
10** including commercial applications, and to alter it and redistribute it
11** freely, subject to the following restrictions:
12**
13** 1. The origin of this software must not be misrepresented; you must not
14** claim that you wrote the original software. If you use this software
15** in a product, an acknowledgment in the product documentation would be
16** appreciated but is not required.
17** 2. Altered source versions must be plainly marked as such, and must not be
18** misrepresented as being the original software.
19** 3. This notice may not be removed or altered from any source distribution.
20**
21** Note: Some of the libraries ClanLib may link to may have additional
22** requirements or restrictions.
23**
24** File Author(s):
25**
26** Magnus Norddahl
27*/
28
29#pragma once
30
31namespace clan
32{
34 template <typename Type>
35 class ComPtr
36 {
37 public:
38 ComPtr() : ptr(0) { }
39 explicit ComPtr(Type *ptr) : ptr(ptr) { }
40 ComPtr(const ComPtr &copy) : ptr(copy.ptr) { if (ptr) ptr->AddRef(); }
41 ~ComPtr() { clear(); }
42 ComPtr &operator =(const ComPtr &copy)
43 {
44 if (this == &copy)
45 return *this;
46 if (copy.ptr)
47 copy.ptr->AddRef();
48 if (ptr)
49 ptr->Release();
50 ptr = copy.ptr;
51 return *this;
52 }
53
54 template<typename That>
55 explicit ComPtr(const ComPtr<That> &that)
56 : ptr(static_cast<Type*>(that.ptr))
57 {
58 if (ptr)
59 ptr->AddRef();
60 }
61
62 bool operator ==(const ComPtr &other) const { return ptr == other.ptr; }
63 bool operator !=(const ComPtr &other) const { return ptr != other.ptr; }
64 bool operator <(const ComPtr &other) const { return ptr < other.ptr; }
65 bool operator <=(const ComPtr &other) const { return ptr <= other.ptr; }
66 bool operator >(const ComPtr &other) const { return ptr > other.ptr; }
67 bool operator >=(const ComPtr &other) const { return ptr >= other.ptr; }
68
69 // const does not exist in COM, so we drop the const qualifier on returned objects to avoid needing mutable variables elsewhere
70
71 Type * const operator ->() const { return const_cast<Type*>(ptr); }
72 Type *operator ->() { return ptr; }
73 operator Type *() const { return const_cast<Type*>(ptr); }
74 operator bool() const { return ptr != 0; }
75
76 bool is_null() const { return ptr == 0; }
77 void clear() { if (ptr) ptr->Release(); ptr = 0; }
78 Type *get() const { return const_cast<Type*>(ptr); }
79 Type **output_variable() { clear(); return &ptr; }
80
81 Type *ptr;
82 };
83}
ComPtr.
Definition: comptr.h:36
ComPtr(Type *ptr)
Definition: comptr.h:39
bool operator<=(const ComPtr &other) const
Definition: comptr.h:65
void clear()
Definition: comptr.h:77
bool operator==(const ComPtr &other) const
Definition: comptr.h:62
bool is_null() const
Definition: comptr.h:76
bool operator!=(const ComPtr &other) const
Definition: comptr.h:63
bool operator<(const ComPtr &other) const
Definition: comptr.h:64
ComPtr & operator=(const ComPtr &copy)
Definition: comptr.h:42
ComPtr(const ComPtr &copy)
Definition: comptr.h:40
~ComPtr()
Definition: comptr.h:41
bool operator>(const ComPtr &other) const
Definition: comptr.h:66
Type * get() const
Definition: comptr.h:78
Type * ptr
Definition: comptr.h:81
ComPtr()
Definition: comptr.h:38
Type *const operator->() const
Definition: comptr.h:71
ComPtr(const ComPtr< That > &that)
Definition: comptr.h:55
bool operator>=(const ComPtr &other) const
Definition: comptr.h:67
Type ** output_variable()
Definition: comptr.h:79
Definition: clanapp.h:36