Library of Assembled Shared Sources
smart_i.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @class lass::util::SmartI
44 * @ingroup SmartPtr
45 * @brief smart pointer to COM interfaces
46 * @author Bram de Greve [Bramz]
47 *
48 * @section interface_requirements Requirements for COM interface
49 *
50 * COM interface @a I must implement two methods @c AddRef() and @c Release():
51 * - @c AddRef() should increase reference count to interface
52 * - @c Release() should decrease reference count
53 */
54
55#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_SMART_I_H
56#define LASS_GUARDIAN_OF_INCLUSION_UTIL_SMART_I_H
57
58#include "util_common.h"
59
60namespace lass
61{
62namespace util
63{
64
65template <typename I>
66class SmartI
67{
68public:
69
70
71 typedef SmartI<I> TSelf; /**< type of @c *this */
72
73 typedef I TInterface;
74 typedef I* TPointer;
75 typedef I** TAddress;
76
77 /** Rebind to another interface type.
78 */
79 template <typename I>
80 struct Rebind
81 {
82 typedef SmartI<I> Type;
83 };
84
85
86
87 /** Default constructor initializes to NULL interface.
88 */
90 interface_(0)
91 {
92 }
93
94 /** Constructor won't do an extra AddRef() on construction but will Release() on destruction.
95 */
96 explicit SmartI(TInterface* iInterface):
97 interface_(iInterface)
98 {
99 }
100
101 /** Copy constructor performs AddRef() on interface.
102 */
103 SmartI(const TSelf& iOther):
104 interface_(iOther.interface_)
105 {
106 safeAddRef();
107 }
108
109 /** Move constructor.
110 */
111 SmartI(TSelf&& iOther) noexcept:
112 interface_(iOther.interface_)
113 {
114 iOther.interface_ = nullptr;
115 }
116
117 /** Destructor performs Release() on interface.
118 */
120 {
121 safeRelease();
122 }
123
124 /** Reinitialize to NULL interface.
125 * Performs Release() on old interface.
126 */
127 void reset()
128 {
129 TSelf temp;
130 swap(temp);
131 }
132
133 /** Reinitialize to an interface, performs no AddRef on new interface, but will Release() it
134 * in destructor.
135 * Performs Release() on old interface.
136 */
137 void reset(TInterface* iInterface)
138 {
139 TSelf temp(iInterface);
140 swap(temp);
141 }
142
143 /** assign the SmartI to another one, performing AddRef() on interface.
144 */
145 TSelf& operator=(const TSelf& iOther)
146 {
147 TSelf temp(iOther);
148 swap(temp);
149 return *this;
150 }
151
152 /** Move assignment
153 */
154 TSelf& operator=(TSelf&& iOther) noexcept
155 {
156 interface_ = iOther.interface_;
157 iOther.interface_ = nullptr;
158 return *this;
159 }
160
161 /** return pointer to interface
162 */
163 TPointer get() const
164 {
165 return interface_;
166 }
167
168 /** access pointee as a pointer
169 */
170 TPointer operator->() const
171 {
172 LASS_ASSERT(interface_);
173 return interface_;
174 }
175
176 /** access address of interface pointer.
177 */
178 TAddress address()
179 {
180 return &interface_;
181 }
182
183 /** return true if pointer doesn't own any interface.
184 * @return this->get() == 0
185 */
186 bool isEmpty() const
187 {
188 return interface_ == 0;
189 }
190
191 /** return true if pointer doesn't own any interface (same as isEmpty).
192 * @return this->get() == 0.
193 */
194 bool operator!() const
195 {
196 return interface_ == 0;
197 }
198
199 /** evaluates to true if pointer owns an interface (only in boolean context)
200 * @return this->get() != 0.
201 */
202 explicit operator bool() const
203 {
204 return interface_ != 0;
205 }
206
207 /** exchange the pointees (and there reference counts) between two shared pointers
208 */
209 void swap(TSelf& iOther)
210 {
211 std::swap(interface_, iOther.interface_);
212 }
213
214private:
215
216 void safeAddRef()
217 {
218 if (interface_)
219 {
220 interface_->AddRef();
221 }
222 }
223
224 void safeRelease()
225 {
226 if (interface_)
227 {
228 interface_->Release();
229 interface_ = 0;
230 }
231 }
232
233 TPointer interface_;
234};
235
236}
237
238}
239
240#endif
241
242// EOF
void reset()
Reinitialize to NULL interface.
Definition smart_i.h:127
TPointer operator->() const
access pointee as a pointer
Definition smart_i.h:170
SmartI(TSelf &&iOther) noexcept
Move constructor.
Definition smart_i.h:111
TAddress address()
access address of interface pointer.
Definition smart_i.h:178
TPointer get() const
return pointer to interface
Definition smart_i.h:163
bool isEmpty() const
return true if pointer doesn't own any interface.
Definition smart_i.h:186
SmartI(TInterface *iInterface)
Constructor won't do an extra AddRef() on construction but will Release() on destruction.
Definition smart_i.h:96
SmartI(const TSelf &iOther)
Copy constructor performs AddRef() on interface.
Definition smart_i.h:103
~SmartI()
Destructor performs Release() on interface.
Definition smart_i.h:119
TSelf & operator=(const TSelf &iOther)
assign the SmartI to another one, performing AddRef() on interface.
Definition smart_i.h:145
void swap(TSelf &iOther)
exchange the pointees (and there reference counts) between two shared pointers
Definition smart_i.h:209
SmartI()
Default constructor initializes to NULL interface.
Definition smart_i.h:89
TSelf & operator=(TSelf &&iOther) noexcept
Move assignment.
Definition smart_i.h:154
SmartI< I > TSelf
type of *this
Definition smart_i.h:71
void reset(TInterface *iInterface)
Reinitialize to an interface, performs no AddRef on new interface, but will Release() it in destructo...
Definition smart_i.h:137
bool operator!() const
return true if pointer doesn't own any interface (same as isEmpty).
Definition smart_i.h:194
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53
Rebind to another interface type.
Definition smart_i.h:81