Coda Distributed File System
dict.h
Go to the documentation of this file.
1/* BLURB gpl
2
3 Coda File System
4 Release 6
5
6 Copyright (c) 1987-2003 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU General Public Licence Version 2, as shown in the
11file LICENSE. The technical and financial contributors to Coda are
12listed in the file CREDITS.
13
14 Additional copyrights
15 none currently
16
17#*/
18
19/*
20 *
21 * Specification of an abstract dictionary facility.
22 *
23 * The main functionality provided by this package over a standard dictionary is
24 * reference counting of dictionary entries. An entry is removed by invoking its
25 * "suicide" member, and then "releasing" the holder's reference by "putting" the
26 * object back into the dictionary. When the last reference to a "dying" entry is
27 * released, the object will physically remove itself from the dictionary and invoke
28 * its destructor.
29 *
30 * A "helper" class (assocrefs) is also provided which maintains variable-sized
31 * arrays of references to dictionary entries. It is useful for maintaining complex
32 * relationships between entries of the same or different type.
33 *
34 * Notes:
35 * 1. This package steals ideas from NIHCL; I did not derive from its base classes
36 * because we would have to buy into the whole library, which is currently
37 * inconvenient. Eventually, we should probably do that.
38 * 2. there is currently no (package-supplied) way to avoid "getting" a dying entry
39 * out of the dictionary or waiting for it to actually die.
40 * 3. the package is NOT multi-thread capable (except for coroutines, of course).
41 *
42 */
43
44#ifndef _UTIL_DICT_H_
45#define _UTIL_DICT_H_ 1
46
47/* Forward declarations. */
48class dictionary;
49class assockey;
50class assocval;
51class assoc;
52class assocrefs;
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59#include <stdlib.h>
60
61#ifdef __cplusplus
62}
63#endif
64
65#include "coda_assert.h"
66#include "dlist.h"
67
68/* Should be built upon hash table rather than linked list? */
69class dictionary : public dlist {
70protected:
71 /* Protected ctor ensures this is abstract class! */
72 dictionary() { ; }
73
74public:
75 /* Derivers should not need to redefine these. */
76 void Add(assoc *);
77 void Remove(assoc *);
79 void Put(assoc **);
80 void Kill(assockey &);
81
82 /* Note that Create() (and a Get() which creates if not found) cannot be defined */
83 /* here since we don't know the type of the object that should be constructed. */
84};
85
86class assockey {
87protected:
88 /* Protected ctor ensures this is abstract class! */
89 assockey() { ; }
90 virtual ~assockey();
91
92public:
93 virtual int operator==(assockey &Key) /* MUST be redefined by deriver! */
94 {
95 CODA_ASSERT(0);
96 return ((char *)this == (char *)&Key);
97 }
98};
99
100class assocval {
101protected:
102 /* Protected ctor ensures this is abstract class! */
103 assocval() { ; }
104
105public:
106};
107
108class assoc : private dlink {
109private:
110 dictionary *dict; /* dictionary this object belongs to */
111 int refcnt; /* keep track of reference holders */
112 unsigned dying : 1; /* T --> nuke this object when refcnt falls to zero */
113
114protected:
117
118 /* Protected ctor ensures this is abstract class! */
120 {
121 dict = &Dict;
122 dict->Add(this);
123 refcnt = 1;
124 dying = 0;
125 }
126 virtual ~assoc(); /* MUST be redefined by deriver! */
127
128public:
129 virtual void Hold();
130 virtual void Release();
131 virtual void Suicide();
132
133 assockey &Key() { return (*key); }
134 assocval &Val() { return (*val); }
135 dictionary *Dict() { return (dict); }
136 int Refcnt() { return (refcnt); }
137 int Dying() { return (dying); }
138};
139
140/* Base class for maintaining collections of references to dictionary entries (assocs). */
141/* Limited ordering of references can be obtained by "attach"'ing with a specified index. */
143const int AR_DefaultGrowSize = 4;
144
146 friend class assocrefs_iterator;
147
148private:
149 int max; /* size of assoc (pointer) array */
150 int count; /* number of non-zero entries */
151 int growsize;
152 assoc **assocs;
153
154public:
156 virtual ~assocrefs();
157
158 virtual void Attach(assoc *,
159 int = -1); /* add a reference (at specified index) */
160 virtual void Detach(assoc * = 0); /* delete a reference (or all) */
161 virtual void
162 Kill(assoc * = 0); /* have a referenced assoc commit suicide (or all) */
163
164 int Max() { return (max); }
165 int Count() { return (count); }
166 int GrowSize() { return (growsize); }
167 assoc **Assocs() { return (assocs); }
168 int Index(assoc *);
169};
170
172 assocrefs *a;
173 int i;
174
175public:
177 assocrefs &); /* parameter used to be a const -- meo 11/27/91 */
178 const assoc *operator()(int * = 0);
179};
180
181#endif /* _UTIL_DICT_H_ */
Definition: dict.h:108
assocval * val
Definition: dict.h:116
int Refcnt()
Definition: dict.h:136
virtual void Release()
Definition: dict.cc:94
virtual ~assoc()
Definition: dict.cc:83
assocval & Val()
Definition: dict.h:134
int Dying()
Definition: dict.h:137
dictionary * Dict()
Definition: dict.h:135
virtual void Hold()
Definition: dict.cc:89
assockey * key
Definition: dict.h:115
assoc(dictionary &Dict)
Definition: dict.h:119
assockey & Key()
Definition: dict.h:133
virtual void Suicide()
Definition: dict.cc:105
Definition: dict.h:86
assockey()
Definition: dict.h:89
virtual ~assockey()
virtual int operator==(assockey &Key)
Definition: dict.h:93
Definition: dict.h:171
assocrefs_iterator(assocrefs &)
Definition: dict.cc:225
const assoc * operator()(int *=0)
Definition: dict.cc:231
Definition: dict.h:145
int Max()
Definition: dict.h:164
assocrefs(int=AR_DefaultInitialSize, int=AR_DefaultGrowSize)
Definition: dict.cc:114
int Index(assoc *)
Definition: dict.cc:217
virtual ~assocrefs()
Definition: dict.cc:122
assoc ** Assocs()
Definition: dict.h:167
int GrowSize()
Definition: dict.h:166
virtual void Kill(assoc *=0)
Definition: dict.cc:194
virtual void Detach(assoc *=0)
Definition: dict.cc:171
virtual void Attach(assoc *, int=-1)
Definition: dict.cc:131
int Count()
Definition: dict.h:165
Definition: dict.h:100
assocval()
Definition: dict.h:103
Definition: dict.h:69
void Add(assoc *)
Definition: dict.cc:37
void Kill(assockey &)
Definition: dict.cc:73
dictionary()
Definition: dict.h:72
assoc * Find(assockey &)
Definition: dict.cc:50
void Remove(assoc *)
Definition: dict.cc:45
void Put(assoc **)
Definition: dict.cc:63
Definition: dlist.h:50
#define CODA_ASSERT(pred)
Definition: coda_assert.h:22
const int AR_DefaultInitialSize
Definition: dict.h:142
const int AR_DefaultGrowSize
Definition: dict.h:143