Defining the data structure

For any model you need to determine what data the model will maintain. The data model for the Tiles program, TTilesModel, is based on a set of instances of the class TTile. TTile is really like a struct, representing pure data. Each TTile instance contains data members defining the kind of tile, the color, and the size and position of the tile:

TTile provides public accessor functions for manipulating these data members, as shown:


NOTE The notation used in the CommonPoint documentation is described in "Reading notation diagrams" on page x of the Preface.

TaligentTypeExtension

The model for this program, TTilesModel, stores TTile instances in a collection and streams them in and out as part of the model. Because instances of the TTile class are saved as a persistent part of a Tiles document component, the TTile class needs to use TaligentTypeExtension, a facility that supports RTTI (Run Time Type Identification).

Classes that use RTTI require a set of utility functions. You can provide these supporting functions easily by calling the macro TaligentTypeExtensionDeclarationsMacro in your class header file, and the macro TaligentTypeExtensionMacro in the source file. These macros define functions that support versioning and that allow objects to be streamed in and out of persistent storage.

Classes that require RTTI must also define the following functions:

NOTE Often you can declare the assignment operator as a private function because it will never be used.


In addition to a default and copy constructor, TTile defines a constructor that lets you create a tile of a specific kind, color, size and position. This diagram shows the complete interface for the class TTile:


Storing the TTile objects

TTilesModel stores the collection of TTile instances in a TArrayOf<TTile>. TArrayOf<AType> is a collection class that encapsulates an indexed sequence of objects of the type specified when the TArrayOf<AType> class template is instantiated. You'll see in the TTilesModel code that TArrayOf requires two parameters when it is constructed--a comparator, used to compare any two elements in the array, and a streamer, used by the collection to save itself as a persistent object. The TArrayOf<AType> constructor requires values for these two parameters, even if both values are Nil.

The TArrayOf<TTile> used by TTilesModel is constructed with a Nil comparator and a TMonomorphicStreamer<TTile>. It requires a streamer because the contents of the array are streamed when the model is streamed. See Chapter 1, "Collections," in Foundation Services for more information on these classes and on using collections.

To provide flexibility, it makes sense to allow the array to contain an arbitrary number of TTile instances. TTilesModel has two functions, AdoptTile and OrphanTile, that add a tile to or remove a tile from the model's array. In the first stage of developing this model, however, the number of tiles in the array is defined in the default constructor and is fixed at three. In a later step you'll see how to add the ability for the user to control the number of tiles in the array. (See "Ownership semantics" below for information on naming conventions for the AdoptTile and OrphanTile functions.)




[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker