2009-08-17 12:41:51 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gshare.h
|
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2009-08-17 12:41:51 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2009-08-17 12:41:51 +02:00
|
|
|
|
|
|
|
***************************************************************************/
|
2011-12-31 03:39:20 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#ifndef __GSHARE_H
|
|
|
|
#define __GSHARE_H
|
|
|
|
|
|
|
|
#include "gtag.h"
|
|
|
|
|
2014-01-08 01:00:31 +01:00
|
|
|
//#define DEBUG_SHARE 1
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-01-08 01:00:31 +01:00
|
|
|
#ifdef DEBUG_SHARE
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
class gShare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gShare() { nref = 1; tag = NULL; fprintf(stderr, "gShare: %p (1)\n", this); }
|
|
|
|
virtual ~gShare() { fprintf(stderr, "~gShare: %p\n", this); if (tag) while (nref > 1) nref--, tag->unref(); }
|
|
|
|
|
|
|
|
void ref() { nref++; if (tag) tag->ref(); fprintf(stderr, "gShare::ref: %p (%d)\n", this, nref); }
|
|
|
|
void unref() { nref--; fprintf(stderr, "gShare::unref: %p (%d)\n", this, nref); if (nref <= 0) delete this; else if (tag) tag->unref(); }
|
2008-07-10 10:00:19 +02:00
|
|
|
int refCount() { return nref; }
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void setTag(gTag *t) { tag = t; for (int i = 0; i < (nref - 1); i++) tag->ref(); }
|
|
|
|
gTag *getTag() { return tag; }
|
|
|
|
void *getTagValue() { return tag->get(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void assign(gShare **dst, gShare *src = 0)
|
|
|
|
{
|
|
|
|
if (src) src->ref();
|
|
|
|
if (*dst) (*dst)->unref();
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int nref;
|
|
|
|
gTag *tag;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class gShare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gShare() { nref = 1; tag = NULL; }
|
2008-03-17 16:04:36 +01:00
|
|
|
virtual ~gShare() { if (tag) { while (nref > 1) nref--, tag->unref(); delete tag; } }
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void ref() { nref++; if (tag) tag->ref(); }
|
|
|
|
void unref() { nref--; if (nref <= 0) delete this; else if (tag) tag->unref(); }
|
2008-07-10 10:00:19 +02:00
|
|
|
int refCount() { return nref; }
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void setTag(gTag *t) { tag = t; for (int i = 0; i < (nref - 1); i++) tag->ref(); }
|
|
|
|
gTag *getTag() { return tag; }
|
|
|
|
void *getTagValue() { return tag->get(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void assign(gShare **dst, gShare *src = 0)
|
|
|
|
{
|
|
|
|
if (src) src->ref();
|
|
|
|
if (*dst) (*dst)->unref();
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int nref;
|
|
|
|
gTag *tag;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|