comptr.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 
31 
32 namespace clan
33 {
34 
35 template <typename Type>
37 class ComPtr
38 {
39 public:
40  ComPtr() : ptr(0) { }
41  explicit ComPtr(Type *ptr) : ptr(ptr) { }
42  ComPtr(const ComPtr &copy) : ptr(copy.ptr) { if (ptr) ptr->AddRef(); }
43  ~ComPtr() { clear(); }
44  ComPtr &operator =(const ComPtr &copy)
45  {
46  if (this == &copy)
47  return *this;
48  if (copy.ptr)
49  copy.ptr->AddRef();
50  if (ptr)
51  ptr->Release();
52  ptr = copy.ptr;
53  return *this;
54  }
55 
56  template<typename That>
57  explicit ComPtr(const ComPtr<That> &that)
58  : ptr(static_cast<Type*>(that.ptr))
59  {
60  if (ptr)
61  ptr->AddRef();
62  }
63 
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  bool operator >(const ComPtr &other) const { return ptr > other.ptr; }
69  bool operator >=(const ComPtr &other) const { return ptr >= other.ptr; }
70 
71  // const does not exist in COM, so we drop the const qualifier on returned objects to avoid needing mutable variables elsewhere
72 
73  Type * const operator ->() const { return const_cast<Type*>(ptr); }
74  Type *operator ->() { return ptr; }
75  operator Type *() const { return const_cast<Type*>(ptr); }
76  operator bool() const { return ptr != 0; }
77 
78  bool is_null() const { return ptr == 0; }
79  void clear() { if (ptr) ptr->Release(); ptr = 0; }
80  Type *get() const { return const_cast<Type*>(ptr); }
81  Type **output_variable() { clear(); return &ptr; }
82 
83  Type *ptr;
84 };
85 
86 }
~ComPtr()
Definition: comptr.h:43
ComPtr(Type *ptr)
Definition: comptr.h:41
bool operator<=(const ComPtr &other) const
Definition: comptr.h:67
Type * ptr
Definition: comptr.h:83
ComPtr & operator=(const ComPtr &copy)
Definition: comptr.h:44
Type ** output_variable()
Definition: comptr.h:81
ComPtr(const ComPtr &copy)
Definition: comptr.h:42
bool operator==(const ComPtr &other) const
Definition: comptr.h:64
void clear()
Definition: comptr.h:79
bool operator!=(const ComPtr &other) const
Definition: comptr.h:65
bool operator<(const ComPtr &other) const
Definition: comptr.h:66
Type * get() const
Definition: comptr.h:80
bool operator>(const ComPtr &other) const
Definition: comptr.h:68
bool is_null() const
Definition: comptr.h:78
Type *const operator->() const
Definition: comptr.h:73
Definition: clanapp.h:36
ComPtr(const ComPtr< That > &that)
Definition: comptr.h:57
bool operator>=(const ComPtr &other) const
Definition: comptr.h:69
ComPtr()
Definition: comptr.h:40
ComPtr.
Definition: comptr.h:38