1 /* Copyright (C) 1992-1998 The Geometry Center
2  * Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips
3  *
4  * This file is part of Geomview.
5  *
6  * Geomview is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Geomview is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Geomview; see the file COPYING.  If not, write
18  * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19  * USA, or visit http://www.gnu.org.
20  */
21 
22 
23 /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
24 
25 #ifndef _GV_REFERENCE_H_
26 #define _GV_REFERENCE_H_
27 
28 #include "ooglutil.h"
29 #include "dbllist.h"
30 
31 /*
32  * Public definitions for Reference & Communications package
33  */
34 
35 #define REFERENCEFIELDS				\
36   unsigned magic;				\
37   int ref_count;				\
38   DblListNode handles
39 
40 typedef struct Ref  {
41   REFERENCEFIELDS;
42 } Ref ;
43 
44 #define REFCNT(obj)       RefCount((Ref *)(obj))
45 #define	REFINCR(obj)      RefIncr((Ref *)(obj))
46 #define	REFDECR(obj)      RefDecr((Ref *)(obj))
47 #define REFGET(type, obj) (REFINCR((obj)), (type *)(obj))
48 #define REFPUT(obj)       REFDECR(obj)
49 
50 /* Initializes a Reference */
RefInit(Ref * ref,int magic)51 static inline void RefInit(Ref *ref, int magic)
52 {
53   ref->ref_count = 1;
54   ref->magic = magic;
55   DblListInit(&ref->handles);
56 }
57 
58 /* Returns current ref count */
RefCount(Ref * ref)59 static inline int RefCount(Ref *ref)
60 {
61   return ref != NULL ? ref->ref_count : 0;
62 }
63 
64 /* Increments count, returns object */
RefIncr(Ref * ref)65 static inline int RefIncr(Ref *ref)
66 {
67   if (ref != NULL) {
68     return ++ref->ref_count;
69   } else {
70     return 0;
71   }
72 }
73 
74 /* Decrements count, returns it  */
RefDecr(Ref * ref)75 static inline int RefDecr(Ref *ref)
76 {
77   if (ref == NULL) {
78     return 0;
79   }
80   if (--ref->ref_count < 0) {
81     OOGLError(1, "RefDecr: ref %x count %d < 0!", ref, ref->ref_count);
82     abort();
83   }
84   return ref->ref_count;
85 }
86 
87 /* Returns magic number */
RefMagic(Ref * ref)88 static inline int RefMagic(Ref *ref) {
89   return ref->magic;
90 }
91 
92 #endif
93 
94 /*
95  * Local Variables: ***
96  * c-basic-offset: 2 ***
97  * End: ***
98  */
99