uLib-0.2
Main Page
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
SmartPointer.h
Go to the documentation of this file.
1
/*//////////////////////////////////////////////////////////////////////////////
2
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
4
5
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6
All rights reserved
7
8
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9
10
------------------------------------------------------------------
11
This library is free software; you can redistribute it and/or
12
modify it under the terms of the GNU Lesser General Public
13
License as published by the Free Software Foundation; either
14
version 3.0 of the License, or (at your option) any later version.
15
16
This library is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
Lesser General Public License for more details.
20
21
You should have received a copy of the GNU Lesser General Public
22
License along with this library.
23
25
26
27
28
#ifndef U_CORE_SMARTPOINTER_H
29
#define U_CORE_SMARTPOINTER_H
30
31
32
namespace uLib {
33
34
template <typename T>
35
class SmartPointer {
36
typedef T element_type;
37
public:
38
39
explicit
40
SmartPointer(T* ptr = NULL) : m_counter(0) {
41
if(!ptr) ptr = new T;
42
if (ptr) m_counter = new ReferenceCounter(ptr);
43
}
44
45
// TakeReference //
46
SmartPointer(T &ref) : m_counter(new ReferenceCounter(&ref,0)) { }
47
48
SmartPointer(const SmartPointer& copy) throw () {
49
acquire(copy.m_counter);
50
}
51
52
SmartPointer(SmartPointer * copy) throw () {
53
acquire(copy->m_counter);
54
}
55
56
virtual ~SmartPointer() { release(); }
57
58
SmartPointer & operator=(const SmartPointer& copy) {
59
if (this != ©)
60
{
61
release();
62
acquire(copy.m_counter);
63
}
64
return *this;
65
}
66
67
T & operator*() const throw () { return *m_counter->ptr; }
68
T * operator->() const throw () { return m_counter->ptr; }
69
70
T * get() const throw () {
71
return m_counter ? m_counter->ptr : 0; }
72
bool unique() const throw () {
73
return (m_counter ? m_counter->count == 1 : true); }
74
75
76
private:
77
78
struct ReferenceCounter
79
{
80
ReferenceCounter(T* ptr = 0, unsigned c = 1) : ptr(ptr), count(c) { }
81
T* ptr;
82
unsigned count;
83
} * m_counter;
84
85
// increment the count
86
void acquire(ReferenceCounter* c) throw ()
87
{
88
m_counter = c;
89
if (c && c->count>0) ++c->count;
90
}
91
92
// decrement the count, delete if it is 0
93
void release() {
94
if (m_counter) {
95
if (--m_counter->count == 0) {
96
delete m_counter->ptr;
97
}
98
if (m_counter->count <= 0) {
99
delete m_counter;
100
m_counter = NULL;
101
}
102
}
103
}
104
105
};
106
107
108
109
}
110
111
#endif // SMARTPOINTER_H
Core
SmartPointer.h
Generated by
1.8.5