Actually, we've already seen one attribute: 'Class. Given some type named X, the phrase X'Class refers to the class of all types that are descended from X, including X.
Three very common attributes are 'First, 'Last, and 'Range. Given some type named X, the phrase X'First is the first legal value of type X. Attribute 'Last refers to the last legal value of a given type. Attribute 'Range refers to the range between 'First and 'Last, including them, and is often used in loops. For example, if Repair_Status is an enumerated type with many values, you could write a loop as follows:
for I in Repair_Status'Range loop -- Do something here. end loop;
Some attributes are actually subprograms that accept parameters, which you can call the way you'd call any other subprogram. One simple attribute is X'Image; this is a subprogram that takes a value of the type X and returns a String representing that value. X must be a scalar type (i.e. Integer, Float, or an enumerated value). Here's an example:
procedure Demo( Value : Integer) is Text_Rep : String := Integer'Image(Value); begin Put(Text_Rep); end Demo;
Here are some other useful attributes:
There are a several other attributes as well.
Some attributes can be set by the programmer. This is mainly used for supporting low-level facilities, such as making variables refer to hardware interfaces (by setting 'Address) or setting the size of given type. The Ada 95 syntax for doing this can be represented as:
for name'attribute_name use expression;
For example, if your machine can read raw temperatures as an 8-bit value from address FFFF_0000, you can read temperatures just by reading the variable "Current_Temperature" by telling Ada to place Current_Temperature at a specific address. Here's an example of how to do that:
type Temperature_Reading is 0 .. 2**8 - 1; for Temperature_Reading'Size use 8; Current_Temperature : Temperature_Reading; for Current_Temperature'Address use 16#FFFF_0000#; pragma Volatile(Current_Temperature); -- We haven't discussed this. -- Now just read from "Temperature" as a variable.
Although an Ada compiler can handle spaces before and after the tick ('), don't place any spaces around it. That way, tools which don't actually parse Ada (such as semi-smart editors and pretty printers) can tell the difference between attributes and character constants.
What would find the smallest value of two Integers A and B, using a predefined attribute?
|Go back to the previous section||Skip to the next section||Go up to lesson 17 outline|
David A. Wheeler (email@example.com)
The master copy of this file is at
The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s17s1.htm".