From 42dcdc51cd19ff00e3c1f5a51b331a10c01735ba Mon Sep 17 00:00:00 2001 From: dockes Date: Tue, 5 Dec 2006 15:16:53 +0000 Subject: [PATCH] fix pbs with empty object --- src/utils/refcntr.h | 59 +++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/utils/refcntr.h b/src/utils/refcntr.h index 8aba72ed..abe10174 100644 --- a/src/utils/refcntr.h +++ b/src/utils/refcntr.h @@ -3,28 +3,45 @@ // See Stroustrup C++ 3rd ed, p. 783 template class RefCntr { - X *rep; - int *pcount; - public: - X * operator->() {return rep;} - RefCntr() : rep(0), pcount(new int(1)) {} - RefCntr(X *pp) : rep(pp), pcount(new int(1)) {} - RefCntr(const RefCntr &r) :rep(r.rep), pcount(r.pcount) { (*pcount)++;} - RefCntr& operator=(const RefCntr& r) { - if (rep == r.rep) return *this; - if (pcount && --(*pcount) == 0) { - delete rep; - delete pcount; + X *rep; + int *pcount; +public: + RefCntr() + : rep(0), pcount(0) + {} + RefCntr(X *pp) + : rep(pp), pcount(new int(1)) + {} + RefCntr(const RefCntr &r) + : rep(r.rep), pcount(r.pcount) + { + if (pcount) + (*pcount)++; } - rep = r.rep; - pcount = r.pcount; - if (pcount) - (*pcount)++; - return *this; - } - ~RefCntr() {if (--(*pcount) == 0) {delete rep;delete pcount;}} - int getcnt() const {return *pcount;} - const X * getptr() const {return rep;} + RefCntr& operator=(const RefCntr& r) + { + if (rep == r.rep) + return *this; + if (pcount && --(*pcount) == 0) { + delete rep; + delete pcount; + } + rep = r.rep; + pcount = r.pcount; + if (pcount) + (*pcount)++; + return *this; + } + ~RefCntr() + { + if (pcount && --(*pcount) == 0) { + delete rep; + delete pcount; + } + } + X *operator->() {return rep;} + int getcnt() const {return pcount ? *pcount : 0;} + const X *getptr() const {return rep;} };