summaryrefslogtreecommitdiff
path: root/freebsdtvision/lib/tvobjs.h
blob: e6c69046ebd6d6d5d6788e2f12dc29408bf2dff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
 * @file tvobjs.h
 *
 * Turbo Vision - Version 2.0
 *
 * Copyright (c) 1994 by Borland International
 * All Rights Reserved.
 *
 * Modified by Sergio Sigala <sergio@sigala.it>
 */

#if defined( Uses_TObject ) && !defined( __TObject )
#define __TObject

#include <stddef.h>

/**
 * TObject is the starting point for much of TVision's class hierarchy. It has
 * no parents but many descendants. Apart from @ref TPoint and @ref TRect,
 * most of TVision's standard classes are ultimately derived from TObject.
 *
 * Any object that uses TVision's stream facilities must trace its ancestry
 * back to TObject.
 * @see TView
 * @short The fundamental class
 */
class TObject {
public:
    virtual ~TObject();
    static void destroy(TObject *o);
    virtual void shutDown();
};

#endif  // Uses_TObject

#if defined( Uses_TNSCollection ) && !defined( __TNSCollection )
#define __TNSCollection

/**
 * TNSCollection implements a nonstreamable collection of items. It provides
 * a base class for the streamable collection class, @ref TCollection.
 * TNSCollection provides @ref TCollection with the functions for adding,
 * accessing, and removing items from a collection.
 *
 * This class stores an array of pointers to generic objects. This array may
 * grow or shrink at run-time.
 *
 * Note: type ccIndex is defined in file `ttypes.h' as int.
 * @short Handles a non-streamable collection of objects
 */
class TNSCollection : public TObject
{
public:
    /**
     * Constructor.
     *
     * Creates a collection with @ref limit set to `aLimit' and @ref delta
     * set to `aDelta'. @ref count and @ref items data members are both set
     * to 0.
     * @ref shouldDelete is set True.
     *
     * The initial number of items will be limited to `aLimit', but the
     * collection is allowed to grow in increments of `aDelta' until memory
     * runs out or the number of items reaches @ref maxCollectionSize.
     *
     * @ref maxCollectionSize is defined in `tvconfig.h' as:
     *
     * <pre>
     * const int maxCollectionSize = INT_MAX / sizeof(void *);
     * </pre>
     * @see maxCollectionSize
     */
    TNSCollection( ccIndex aLimit, ccIndex aDelta );
    /**
     * Destructor.
     *
     * If @ref shouldDelete is True, the destructor removes and destroys all
     * items in the collection by calling @ref freeAll and setting @ref limit
     * to 0.
     *
     * If @ref shouldDelete is False, the destructor sets @ref limit to 0 but
     * does not destroy the collection.
     */
    ~TNSCollection();
    /**
     * Releases all the resources allocated by this class.
     *
     * If class flag @ref shouldDelete is True the function @ref freeAll() is
     * called. This will delete each object of the array.
     */
    virtual void shutDown();
    /**
     * Returns a pointer to the item indexed by `index' in the collection. If
     * `index' is less than 0 or greater than or equal to count, @ref error()
     * is called with an argument of coIndexError, and 0 is then returned.
     */
    void *at( ccIndex index );
    /**
     * Returns the index of the given item; that is, the converse operation
     * to @ref at(). If the item is not in the collection, @ref indexOf()
     * calls @ref error().
     *
     * The address of the item is passed in the `item' parameter.
     */
    virtual ccIndex indexOf( void *item );
    /**
     * Removes the object at position `index' from the array.
     *
     * Then calls delete on the object.
     */
    void atFree( ccIndex index );
    /**
     * Removes the object at position `index' from the array.
     *
     * Removes the item at the position `index' by moving the following items
     * up by one position. @ref count is decremented by 1, but the memory
     * allocated to the collection is not reduced. If `index' is greater than
     * or equal to @ref count, @ref error() is called.
     *
     * The item itself is not destroyed.
     */
    void atRemove( ccIndex index );
    /**
     * Removes the item given by `item' from the collection.
     *
     * Equivalent to `atRemove(indexOf(item))'. Does not call delete on the
     * object.
     * @see TNSCollection::atRemove
     * @see TNSCollection::indexOf
     */
    void remove( void *item );
    /**
     * Removes all items from the collection by just setting @ref count to 0.
     * @see TNSCollection::count
     */
    void removeAll();
    /**
     * Removes and destroys the given item.
     *
     * It just does `atRemove(indexOf(item))'. Then calls delete on the
     * object.
     * @see TNSCollection::atRemove
     * @see TNSCollection::indexOf
     */
    void free( void *item );
    /**
     * Removes and destroys all items in the collection and sets @ref count
     * to 0.
     *
     * The array is cleared out but not deleted.
     */
    void freeAll();
    /**
     * Inserts a new object at position `index'.
     *
     * Moves the following items down by one position, then inserts `item' at
     * the `index' position. If `index' is less than 0 or greater than
     * @ref count data member, @ref error() is called with an argument of
     * coIndexError and the new item is not inserted.
     *
     * If @ref count is equal to @ref limit data member before the call to
     * atInsert(), the allocated size of the collection is expanded by
     * @ref delta items using a call to @ref setLimit().
     *
     * If the @ref setLimit() call fails to expand the collection, the
     * @ref error() member function is called with an argument of coOverflow
     * and the new item is not inserted.
     */
    void atInsert( ccIndex index, void *item );
    /**
     * Replaces the object at position `index'.
     *
     * Replaces the item at position `index' with the given `item'. If `index'
     * is less than 0 or greater than or equal to @ref count,
     * @ref error() is called with an argument of coIndexError.
     *
     * Old object is lost.
     */
    void atPut( ccIndex index, void *item );
    /**
     * Inserts `item' into the collection, and adjusts other indexes if
     * necessary. By default, insertions are made at the end of the collection
     * by calling @ref atInsert().
     */
    virtual ccIndex insert( void *item );
#ifndef __UNPATCHED
    /**
     * This function is called on error conditions.
     *
     * By default calls function exit() to terminate the program.
     */
    virtual void error( ccIndex code, ccIndex info );
#else
    /**
     * Called whenever a collection error is encountered. By default, this
     * member function produces a run-time error of (212 - `code').
     */
    static void error( ccIndex code, ccIndex info );
#endif
    /**
     * firstThat() applies a @ref Boolean function `Test', along with an
     * argument list given by `arg' to each item in the collection until the
     * tested function returns True.
     * The result is the item pointer for which the call returns True, or 0 if
     * the call returned False for all items.
     *
     * `Test' is a pointer to a function whose type ccTestFunc is defined
     * as:
     *
     * <pre>
     * typedef Boolean (*ccTestFunc)(void *, void *)
     * </pre>
     *
     * This method returns when one object of the array passes the test or
     * when each object is tested without success. In the first case it
     * returns the address of the object. In the latter case it returns 0.
     * `arg' stores the argument of the function (if any).
     *
     * This method scans the array forward. This is an example:
     *
     * <pre>
     * #define Uses_TNSCollection
     *
     * #include "tv.h"
     *
     * class XObject {
     *     int value;
     * public:
     *     XObject(int aValue): value(aValue) {}
     *     int getValue() { return value; }
     * };
     *
     * Boolean matchTest(void *obj, void *value)
     * {
     *     if (((XObject *) obj)->getValue() == *((int *) value)) return True;
     *     return False;
     * }
     *
     * void main()
     * {
     *     TNSCollection array(10, 5);
     *     array.insert(new XObject(14));
     *     array.insert(new XObject(32));
     *     array.insert(new XObject(23));
     *     array.insert(new XObject(41));
     *     int find = 23;
     *     XObject *p = (XObject *) array.firstThat(&matchTest, &find);
     *     if (p != 0) array.free(p);
     * }
     * </pre>
     * @see TNSCollection::forEach
     * @see TNSCollection::lastThat
     */
    void *firstThat( ccTestFunc Test, void *arg );
    /**
     * lastThat() applies the @ref Boolean function `Test', together with
     * the `arg' argument list to each item in the collection, starting at
     * the last item, and scanning in reverse order until the tested function
     * returns True.
     * The result is the item pointer for which the call returns True, or 0 if
     * the call returned False for all items.
     *
     * This method scans the array backward.
     * @see TNSCollection::firstThat
     * @see TNSCollection::forEach
     */
    void *lastThat( ccTestFunc Test, void *arg );
    /**
     * The forEach() iterator applies an action, given by the function
     * `action', to each item in the collection. The `arg' pointer can be used
     * to pass additional arguments to the action.
     *
     * `action' is a pointer to a function whose type ccAppFunc is defined as:
     *
     * <pre>
     * typedef void (*ccAppFunc)(void *, void *);
     * </pre>
     *
     * This method scans the array forward.
     * @see TNSCollection::firstThat
     * @see TNSCollection::lastThat
     */
    void forEach( ccAppFunc action, void *arg );
    /**
     * Packs the array by removing null pointers from it.
     *
     * Deletes all null pointers in the collection and moves items up to fill
     * any gaps.
     */
    void pack();
    /**
     * Expands or shrinks the collection by changing the allocated size to
     * `aLimit'.
     *
     * -# If `aLimit' is less than @ref count, it is set to @ref count.
     * -# if `aLimit' is greater than @ref maxCollectionSize, it is set to
     *    @ref maxCollectionSize. Integer constant maxCollectionSize is
     *    defined in file `tvconfig.h' as:
     *
     * <pre>
     * const int maxCollectionSize = INT_MAX / sizeof(void *);
     * </pre>
     *
     * Then, if `aLimit' is different from the current @ref limit, a new items
     * array of `aLimit' elements is allocated, the old @ref items array is
     * copied into the new array, and the old array is deleted.
     */
    virtual void setLimit( ccIndex aLimit );
    /**
     * Returns the number of items stored in the collection, up to
     * @ref maxCollectionSize.
     */
    ccIndex getCount()
        { return count; }
protected:
    /**
     * Constructor.
     *
     * This constructor sets variable @ref shouldDelete to True and variables
     * @ref count, @ref limit and @ref delta to 0.
     */
    TNSCollection();
    /**
     * A pointer to an array of generic item pointers.
     * This variable stores the array starting address.
     */
    void **items;
    /**
     * This variable stores the number of objects in the array.
     * @see TNSCollection::items
     * @see TNSCollection::limit
     */
    ccIndex count;
    /**
     * The currently allocated size (in elements) of the @ref items list.
     * Current size of the array. Greater or equal than @ref count.
     */
    ccIndex limit;
    /**
     * This value is used every time the array must be enlarged. In this case
     * a number of delta pointers will be added to the array.
     *
     * delta is the number of items by which to increase the @ref items list
     * whenever it becomes full. If delta is zero, the collection cannot grow
     * beyond the size set by @ref limit.
     */
    ccIndex delta;
    /**
     * If set True (the default), the TNSCollection destructor will call
     * @ref freeAll() before setting @ref limit to 0. All objects will be
     * deleted when method @ref shutDown() is called.
     *
     * If set False, the destructor simply sets @ref limit to 0.
     */
    Boolean shouldDelete;
private:
    /**
     * Undocumented.
     */
    virtual void freeItem( void *item );
};

#endif  // Uses_TNSCollection

#if defined( Uses_TNSSortedCollection ) && !defined( __TNSSortedCollection )
#define __TNSSortedCollection

/**
 * The abstract class TNSSortedCollection is a specialized derivative of
 * @ref TNSCollection implementing non-streamable collections sorted by a
 * key (with or without duplicates). No instances of TNSSortedCollection are
 * allowed. It exists solely as a base for other standard or user-defined
 * derived classes.
 * @short Handles a non-streamable collection sorted by a key (with or without
 * duplicates)
 */
class TNSSortedCollection: public virtual TNSCollection
{
public:
    /**
     * Invokes the TNSCollection constructor to set @ref count, @ref items
     * and @ref limit to 0; calls setLimit(aLimit) to set the collection
     * @ref limit to `aLimit', then sets @ref delta to `aDelta'.
     * @see TNSCollection::setLimit
     *
     * Sets @ref duplicates data member to False. If you want to allow
     * duplicate keys, you must set @ref duplicates to True.
     */
    TNSSortedCollection( ccIndex aLimit, ccIndex aDelta) :
        TNSCollection( aLimit, aDelta ), duplicates(False)
            { delta = aDelta; setLimit( aLimit ); }
    /**
     * Returns True if the item identified by `key' is found in the sorted
     * collection. If the item is found, `index' is set to the found index;
     * otherwise `index' is set to the index where the item would be placed if
     * inserted and False is returned.
     */
    virtual Boolean search( void *key, ccIndex& index );
    virtual ccIndex indexOf( void *item );
    /**
     * If @ref duplicates data member is False, insert works as follows: if
     * the target item is not found in the sorted collection, it is inserted
     * at the correct index position. Calls @ref search() to determine if the
     * item exists, and if not, where to insert it.
     *
     * If @ref duplicates is True, the item is inserted ahead of any items
     * (if any) with the same key.
     */
    virtual ccIndex insert( void *item );
    /**
     * Set to True if duplicate indexes are allowed; otherwise set to False.
     * The default is False.
     *
     * If @ref duplicates data member is True methods @ref search(),
     * @ref insert() and @ref indexOf() work differently.
     */
    Boolean duplicates;
    /**
     * Undocumented.
     */
    virtual void *keyOf( void *item );
protected:
    /**
     * Undocumented.
     */
    TNSSortedCollection() : duplicates(False) {}
private:
    /**
     * compare() is a pure virtual function that must be overridden in all
     * derived classes. compare() should compare the two key values, and
     * return a result.
     */
    virtual int compare( void *key1, void *key2 ) = 0;
};

#endif  // Uses_TNSSortedCollection