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