|
a/src/utils/refcntr.h |
|
b/src/utils/refcntr.h |
1 |
#ifndef _REFCNTR_H_
|
1 |
#ifndef _REFCNTR_H_
|
2 |
#define _REFCNTR_H_
|
2 |
#define _REFCNTR_H_
|
|
|
3 |
/* Copyright (C) 2014 J.F.Dockes
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the
|
|
|
16 |
* Free Software Foundation, Inc.,
|
|
|
17 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
18 |
*/
|
3 |
|
19 |
|
4 |
// See Stroustrup C++ 3rd ed, p. 783
|
20 |
// See Stroustrup C++ 3rd ed, p. 783
|
5 |
// This is only used if std::shared_ptr is not available
|
21 |
// This is only used if std::shared_ptr is not available
|
6 |
template <class X> class RefCntr {
|
22 |
template <class X> class RefCntr {
|
7 |
X *rep;
|
23 |
X *rep;
|
|
... |
|
... |
31 |
pcount = r.pcount;
|
47 |
pcount = r.pcount;
|
32 |
if (pcount)
|
48 |
if (pcount)
|
33 |
(*pcount)++;
|
49 |
(*pcount)++;
|
34 |
return *this;
|
50 |
return *this;
|
35 |
}
|
51 |
}
|
36 |
void release()
|
52 |
void reset() {
|
37 |
{
|
|
|
38 |
if (pcount && --(*pcount) == 0) {
|
53 |
if (pcount && --(*pcount) == 0) {
|
39 |
delete rep;
|
54 |
delete rep;
|
40 |
delete pcount;
|
55 |
delete pcount;
|
41 |
}
|
56 |
}
|
42 |
rep = 0;
|
57 |
rep = 0;
|
43 |
pcount = 0;
|
58 |
pcount = 0;
|
44 |
}
|
59 |
}
|
45 |
void reset() {
|
|
|
46 |
release();
|
|
|
47 |
}
|
|
|
48 |
~RefCntr()
|
60 |
~RefCntr()
|
49 |
{
|
61 |
{
|
50 |
release();
|
62 |
reset();
|
51 |
}
|
63 |
}
|
52 |
X *operator->() {return rep;}
|
64 |
X *operator->() {return rep;}
|
53 |
X *getptr() const {return rep;}
|
|
|
54 |
X *get() const {return rep;}
|
65 |
X *get() const {return rep;}
|
55 |
const X *getconstptr() const {return rep;}
|
|
|
56 |
int getcnt() const {return pcount ? *pcount : 0;}
|
66 |
int use_count() const {return pcount ? *pcount : 0;}
|
57 |
bool isNull() const {return rep == 0;}
|
|
|
58 |
bool isNotNull() const {return rep != 0;}
|
|
|
59 |
operator bool() const {return rep != 0;}
|
67 |
operator bool() const {return rep != 0;}
|
60 |
};
|
68 |
};
|
61 |
|
69 |
|
62 |
|
|
|
63 |
#endif /*_REFCNTR_H_ */
|
70 |
#endif /*_REFCNTR_H_ */
|