Top Up Prev Next Bottom Contents Index Search

1.10 Attributes

Attributes represent logical properties that an object may or may not have. Certain classes such as State and Porthole contain attributes and provide interfaces for setting and clearing attributes. For the State class, for instance, the initial value may or may not be settable by the user; this is indicated by an Attribute. In code generation classes, attributes may indicate whether an assembly-language buffer should be allocated to ROM or RAM, fast memory or slow memory, etc. The set of attributes of an object is stored in an entity called a bitWord. At present, a bitWord is represented as an unsigned long, which restricts the number of distinct attributes to 32; this may be changed in future releases. An Attribute object represents a request to turn certain attributes of an object off, and to turn other attributes on. As a rule, constants of class Attribute are used to represent attributes, and users have no need to know whether a given property is represented by a true or false bit in the bitWord. Although we would prefer to have a constructor for Attribute objects of the form

Attribute(bitWord bitsOn, bitWord bitsOff); 
it has turned out that doing so presents severe problems with order of construction, since a number of global Attribute objects are used and there is no simple, portable way of guaranteeing that these objects are constructed before any use. As a result, the bitsOn and bitsOff members are public, but we forbid use of that fact except in one place: constant Attribute objects can be initialized by the C "aggregate form", as in the following example:

extern const Attribute P_HIDDEN = {PB_HIDDEN, 0}; 
(This particular attribute is used by Porthole to indicate that a port should not be visible to the user, i.e. should not appear on an icon.) The first word specified is the bitsOn field, PB_HIDDEN, and the second word specified is the bitsOff field. Other than to initialize objects, we pretend that these data members are private.

1.10.1 Attribute member functions

Attribute& operator |= (const Attribute& arg);
Attribute& operator &= (const Attribute& arg);
These operations combine attributes, by applying the |= and &= operators to the bitsOn and bitsOff fields. The first operation, as attributes are commonly used, represents a requirement that two sets of attributes be met, so it has been argued that it really should be the "and" operation. However, the current scheme has the virtue of consistency.

bitWord eval(bitWord defaultVal) const; 
Evaluate an attribute given a default value. Essentially, bits corresponding to bitsOn are turned on, and then bits corresponding to bitsOff are turned off.

bitWord clearAttribs(bitWord defaultVal) const; 
This method essentially applies the attribute backwards, reversing the roles of bitsOn and bitsOff in eval.

bitWord on() const; 
bitWord off() const;
Retrieve the bitsOn and bitsOff values, respectively. Inline definitions of operators & and | are also defined to implement nondestructive forms of the &= and |= operations.

Top Up Prev Next Bottom Contents Index Search
Copyright © 1990-1997, University of California. All rights reserved.