luna-sysmgr-common
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
sptr.h
Go to the documentation of this file.
1
/* @@@LICENSE
2
*
3
* Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*
17
* LICENSE@@@ */
30
/* ============================================================
31
* Code derived from smart_ptr.h by Thatcher Ulrich, 2003.
32
* Original copyright follows:
33
*
34
* smart_ptr.h -- by Thatcher Ulrich <tu@tulrich.com> 2003
35
*
36
* This source code has been donated to the Public Domain. Do
37
* whatever you want with it.
38
* ============================================================ */
39
40
#ifndef SPTR_H
41
#define SPTR_H
42
43
#include "
Common.h
"
44
45
#include <glib.h>
46
69
class
RefCounted
70
{
71
public
:
80
RefCounted
() : m_refCount(0) {}
81
90
virtual
~RefCounted
() {}
91
100
void
ref
()
101
{
102
g_atomic_int_inc(&m_refCount);
103
}
104
111
void
deref
()
112
{
113
if
(g_atomic_int_dec_and_test(&m_refCount)) {
114
delete
this
;
115
}
116
}
117
118
private
:
122
gint m_refCount;
123
140
RefCounted
(
const
RefCounted
&);
141
158
RefCounted
& operator=(
const
RefCounted
&);
159
};
160
161
162
// A smart (strong) pointer asserts that the pointed-to object will
163
// not go away as long as the strong pointer is valid. "Owners" of an
164
// object should keep strong pointers; other objects should use a
165
// strong pointer temporarily while they are actively using the
166
// object, to prevent the object from being deleted.
184
template
<
class
T>
185
class
sptr
186
{
187
public
:
197
sptr
(T* ptr) : m_ptr(ptr)
198
{
199
if
(m_ptr)
200
{
201
m_ptr->ref();
202
}
203
}
204
212
sptr
() : m_ptr(0) {}
213
224
sptr
(
const
sptr<T>
& s) : m_ptr(s.m_ptr)
225
{
226
if
(m_ptr)
227
{
228
m_ptr->ref();
229
}
230
}
231
240
~sptr
()
241
{
242
if
(m_ptr)
243
{
244
m_ptr->deref();
245
}
246
}
247
260
void
operator=
(
const
sptr<T>
& s) { setRef(s.m_ptr); }
261
273
void
operator=
(T* ptr) { setRef(ptr); }
274
285
T*
operator->
()
const
{
return
m_ptr; }
286
296
T*
get
()
const
{
return
m_ptr; }
297
305
bool
operator==
(
const
sptr<T>
& p)
const
{
return
m_ptr == p.m_ptr; }
306
314
bool
operator!=
(
const
sptr<T>
& p)
const
{
return
m_ptr != p.m_ptr; }
315
323
bool
operator==
(T* p)
const
{
return
m_ptr == p; }
324
332
bool
operator!=
(T* p)
const
{
return
m_ptr != p; }
333
334
private
:
347
void
setRef(T* ptr)
348
{
349
if
(ptr != m_ptr)
350
{
351
if
(m_ptr)
352
{
353
m_ptr->deref();
354
}
355
m_ptr = ptr;
356
357
if
(m_ptr)
358
{
359
m_ptr->ref();
360
}
361
}
362
}
363
367
T* m_ptr;
368
};
369
370
#if 0
371
// Usage
372
class
MyClass :
public
RefCounted
{
373
374
int
array[100];
375
};
376
377
void
func(
sptr<MyClass>
abc) {
378
abc->array[10] = 20;
379
}
380
381
int
main() {
382
sptr<MyClass>
a =
new
MyClass;
383
func(a);
384
}
385
386
#endif // 0
387
388
#endif
/* SPTR_H */
luna-sysmgr-common
include
sptr.h
Generated on Fri Jun 21 2013 00:48:48 for luna-sysmgr-common by
1.8.3.1