/*
 * tvobjs.h
 *
 * Turbo Vision - Version 2.0
 *
 * Copyright (c) 1994 by Borland International
 * All Rights Reserved.
 *
 * Modified by Sergio Sigala <ssigala@globalnet.it>
 */

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

#include <stddef.h>

/**
 * A very basic class.
 *
 * Inherited by most TVision classes.
 * @see TView
 * @short A very basic class
 */
class TObject
{
public:
    /**
     * Destructor.
     *
     * Does nothing.
     */
    virtual ~TObject();
    /**
     * Destroys the object pointed by `o'.
     *
     * It calls `o->shutDown()' and after does `delete o'.
     */
    static void destroy( TObject *o );
    /**
     * Releases TObject resources.
     *
     * Does nothing. Redefined in the derived classes.
     */ 
    virtual void shutDown();
private:
};

inline void TObject::destroy( TObject *o )
{
    if( o != 0 )
        o->shutDown();
    delete o;
}

#endif  // Uses_TObject

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

/**
 * A non-streamable collection of objects.
 *
 * 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 A non-streamable collection of objects
 */
class TNSCollection : public TObject
{
public:
    /**
     * Constructor.
     *
     * `aLimit' is the initial size of the array of pointers. `aDelta' is used
     * every time the array must be enlarged. In this case a number of
     * `aDelta' pointers will be added to the array.
     */
    TNSCollection( ccIndex aLimit, ccIndex aDelta );
    /**
     * Destructor.
     *
     * Deletes the array of pointers. Does not delete the objects.
     */
    ~TNSCollection();
    /**
     * Releases all the resources allocated by this class.
     *
     * If class flag `shouldDelete' is True the function freeAll() is called.
     * This will delete each object of the array.
     * @see TNSCollection::freeAll
     * @see TNSCollection::shouldDelete
     */
    virtual void shutDown();
    /**
     * Returns the address of the object at position `index'.
     */
    void *at( ccIndex index );
    /**
     * Returns the position of an object in the array.
     *
     * The address is passed in the `item' parameter. If the object does not
     * exist the function error() will be called.
     * @see TNSCollection::error
     */
    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.
     *
     * Does not call delete on the object.
     */
    void atRemove( ccIndex index );
    /**
     * Removes the object at address `item' from the array.
     *
     * It just does `atRemove(indexOf(item))'. Does not call delete on the
     * object.
     */
    void remove( void *item );
    /**
     * It just sets class variable `count' to 0.
     * @see TNSCollection::count
     */
    void removeAll();
    /**
     * Removes the object at address `index' from the array.
     *
     * It just does `atRemove(indexOf(item))'. Then calls delete on the
     * object.
     */
    void free( void *item );
    /**
     * Deletes all the objects in the array.
     *
     * The array is cleared out but not deleted.
     */
    void freeAll();
    /**
     * Inserts a new object at position `index'.
     *
     * Objects starting from position `index' to the end of the array are
     * moved one position forward.
     */
    void atInsert( ccIndex index, void *item );
    /**
     * Replaces the object at position `index'.
     *
     * Old object is lost.
     */
    void atPut( ccIndex index, void *item );
    /**
     * Inserts an object at the end of the array.
     */
    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
    /**
     * @internal
     */
    static void error( ccIndex code, ccIndex info );
#endif
    /**
     * Applies a test function to each object in the array.
     *
     * `Test' is a pointer to a function whose type `ccTestFunc' is defined
     * as:
     *
     * typedef Boolean (*ccTestFunc)(void *, void *)
     *
     * 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 you need it).
     *
     * This method scans the array forward. This is an example:
     *
     * #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);
     *
     * }
     * @see TNSCollection::lastThat
     * @see TNSCollection::forEach
     */
    void *firstThat( ccTestFunc Test, void *arg );
    /**
     * Applies a test function to each object in the array.
     *
     * This method scans the array backward.
     * @see TNSCollection::firstThat
     * @see TNSCollection::forEach
     */
    void *lastThat( ccTestFunc Test, void *arg );
    /**
     * Applies a function to each object in the array.
     *
     * `action' is a pointer to a function whose type `ccAppFunc' is defined
     * as:
     *
     * typedef void (*ccAppFunc)(void *, void *);
     *
     * 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.
     */
    void pack();
    /**
     * Resizes the array.
     *
     * `aLimit' is the new size. If it is lesser than `count' the new size
     * will be forced to be `count'. It it is grater than `maxCollectionSize'
     * the new size will be forced to be `maxCollectionSize'. Integer constant
     * `maxCollectionSize' is defined in `tvconfig.h' as
     * `INT_MAX / sizeof(void *)'.
     * @see TNSCollection::count
     */
    virtual void setLimit( ccIndex aLimit );
    /**
     * Returns the number of pointers stored in the array.
     */
    ccIndex getCount()
        { return count; }
protected:
    /**
     * Constructor.
     */
    TNSCollection();
    /**
     * This variable stores the array starting address.
     */
    void **items;
    /**
     * This variable stores the number of objects in the array.
     * @see TNSCollection::limit
     */
    ccIndex count;
    /**
     * Current size of the array. Greater or equal to `count'.
     * @see TNSCollection::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.
     */
    ccIndex delta;
    /**
     * If this flag is True, all objects will be deleted when method
     * shutDown() is called. Its initial value is True.
     * @see TNSCollection::shutDown
     */
    Boolean shouldDelete;
private:
    virtual void freeItem( void *item );
};

#endif  // Uses_TNSCollection

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

class TNSSortedCollection: public virtual TNSCollection
{

public:

    TNSSortedCollection( ccIndex aLimit, ccIndex aDelta) :
        TNSCollection( aLimit, aDelta ), duplicates(False)
            { delta = aDelta; setLimit( aLimit ); }

    virtual Boolean search( void *key, ccIndex& index );

    virtual ccIndex indexOf( void *item );
    virtual ccIndex insert( void *item );

    Boolean duplicates;
    virtual void *keyOf( void *item );

protected:

    TNSSortedCollection() : duplicates(False) {}

private:

    virtual int compare( void *key1, void *key2 ) = 0;

};

#endif  // Uses_TNSSortedCollection

Documentation generated by sergio@athena.milk.it on Wed Feb 10 22:11:47 CET 1999