6.2. Attributes - JulTob/Ada GitHub Wiki

Types come with certain Attributes by default.

These are “methods” in C languages.

Un atributo es una propiedad de una entidad que puede ser consultada usando una attribute_reference o un range_attribute_reference.

MyType'First;       --Returns the first element
MyType'Last;        --Returns the last element
MyType'Succ(MyVar); --Successive
MyType'Pred(MyVar); --Predecessor 
MyType'Pos(MyVar);  --Position (returns int.) 
MyType'Val(n);      --Value at n# (returns Type)
MyType'Image(Var);  --String of the type
MyType'Value(String);  --If string is an identifier, returns that value

Integer'First      --  valor mínimo del tipo Integer
Float'Digits       --  precisión del tipo Float
Integer'Range  --  equivalente a Integer'First .. Integer'Last
Mat'Range(1)   --  rango 1 .. 5, supuesto que:
               --  Mat : array (1 .. 5, 1 .. 10) of Integer;

State

type State_Type is ( Setting, Waiting, Processing, Stopping);
...
State := State_Type'First;       --Returns Setting
State := State_Type'Succ(State); --Successive
State := State_Type'Pred(State); --Predecessor 
State := State_Type'Pos(State);  --Position (returns int.) 
State := State_Type'Val(1);      --Value at n# (returns Type)
Day:= Day_Type'Last;       --Returns Sun

Integer's attributes

A : Integer := 99;
...
Put_Line (Integer'Image (A) );
A := Integer'Value ("99");

Character's attributes

C : Character := 'a';
...
C := 
   Characte'Val (
      Character'Pos(C) +1  
   );
C := Character'Succ(C);

Ada has a selection of attributes, or built-in functions, that can be applied to types and variables. Attributes are attached to the end of a type or variable name using a single quote.

b: boolean;

-- To string, text image
'img    
'image

-- Size
'size
---- Size in bits of memory
put_Line("Boolean bits size: " & b'size'image);        -- Var size: 8 bits
put_Line("Boolean bits size: " & boolean'size'image);  -- 1 bit

put_Line("Very short bits size: " & short_short_integer'size'image); -- 8 bits
put_Line("Short bits size: " & short_integer'size'image);  -- 16 bits
put_Line("Integer bits size: " & integer'size'image);    -- 32 bits
put_Line("Natural Variable size. " & v'size'image);      -- 32 bits
put_Line("Long bits size: " & Natural'size'image);       -- 31b
put_Line("Positive bits size: " & Positive'size'image);  -- 31 bits
put_Line("Long bits size: " & Long_Integer'size'image);  -- 64
put_Line("Long bits size: " & Long_Long_Integer'size'image); -- 64 or maximum of the system

put_Line("short Float Variable size. " & v'size'image);       -- 32
put_Line("Short float bits size: " & Short_Float'size'image); -- 32
put_Line("Float bits size: " & Float'size'image);         -- 32/64 System
put_Line("Long Float bits size: " & Long_Float'size'image); -- 64
put_Line("Very Long bits size: " & Long_Long_Float'size'image); --128


-- First & Last
put_Line( "First integer is " & integer'first'img );
put_Line( "Last integer is " & integer'last'img );

-- Positions
Put_Line( "First enumerated is " & color'first'img );
Put_Line( "Last enumerated is " & color'last'img );
Put_Line( "Mica is in position" & color'pos( red )'img );
Put_Line( "The third enumerated is " & color'val(2)'img );


-- Float
Put_Line( "The smallest float is" & float'small'img );
Put_Line( "The largest float is" & float'large'img );
Put_Line( "The number of digits in float is" &
    integer'image(float'digits) );
Put_Line( "The size of the mantissa in bits is" &
    float'mantissa'img );
Put_Line( "However, the CPU's mantissa is" &
    float'machine_mantissa'img );

Access - access value for an identifier

Address - address value for an identifier
Adjacent - the floating point value adjacent to the given value
Aft - for fixed types, number of decimal digits after decimal to accommodate a subtype
Alignment - storage value of an identifier
Base - unconstrained subtype of a type
Bit_Order - whether or not bits are high order first
Body_Version [NQS]
Callable - true if task can be called
Ceiling - round up a floating point value
Class - classwide type of an identifier
Component_Size - size of array components in bits
Compose
Constrained
Copy_Sign
Count
Definite
Delta
Denorm
Digits      --  precisión del tipo
Exponent
External_Tag
First - first index in an array
First_Bit
Floor - round down a floating-point value
Fore
Fraction
Identity
Image
Input - convert value to a string
Last - last index in an array
Last_Bit
Leading_Part
Length - number of elements in an array
Machine
Machine_Emax - maximum real type exponent on your hardware
Machine_Emin - minimum real type exponent on your hardware
Machine_Mantissa - size of mantissa on your hardware in bits
Machine_Overflows - true of your machine overflows real types [NQS]
Machine_Radix
Machine_Rounds
Max - maximum value
Max_Size_In_Storage_Elements
Min - minimum value
Model
Modulus
Output
Partition_ID - for distributed processing
Pos - position in a discrete type (such as an enumerated) — opposite of val
Position
Pred - previous value in a discrete type
Range - range of values for a type
Read
Remainder
Round
Rounding
Safe_First
Safe_Last
Scale
Scaling
Signed_Zeros
Size - size of storage in bytes
Small
Stoarge_Pool - used to set the storage pool for a pointer
Storage_Size
Succ - next value in a discrete type
Tag - tag for a tagged record
Terminated - true if task has terminated
Truncation
Unbiased_Rounding
Unchecked_Access - return access type, but ignore scope checks
Val - value of a discrete type at a certain position — opposite of pos
Valid - determine if the expression evaluates to a legal result
Value - convert string to a value — opposite of image
Version
Wide_Image - same as image, but for a 16-bit string
Wide_Value - same as value, but for a 16-bit string
Wide_Width
Write
Here's a list of additional gnat-specific attributes:
[should fold these in above]
Abort_Signal - task abort exception
Address_Size - number of bits in an address
Bit - offset to first bit in object
Default_Bit_Order - whether or not CPU uses high order first
Elab_Body - the procedure that elaborates a package body
Elab_Spec - the procedure that elaborates a package spec
Enum_Rep - the numerical value of an enumerated identifier
Fixed_Value - unchecked conversion of integer to a fixed type
Img - shorthand for ‘image
Integer_Value - the reverse of Fixed_Value
Machine_Bits - for compatibility with other Ada compilers
Max_Interrupt_Priority - the maximum interrupt priority
Max_Priority - the maximum task priority
Maximum_Alignment - determine the bit alignment of an external object
Mechanism_Code - how a parameter is passed to a subprogram
Null_Parameter - for passing null pointer for a composite object
Object_Size - for fixed and discrete types, default allocation size
Passed_By_Reference - true if type is normally passed by reference
Range_Length - number of values in a discrete type
Storage_Unit - same as System.Storage_Unit
Tick - same as System.Tick
Type_Class - return type basic class of an identifier (such an enumerated or array)
Universal_Literal_String - return a string literal for a number
Unrestricted_Access - like access, but has no accessibility or aliased view checks
Value_Size - number of bits to represent a value of a given subtype
Word_Size - same as System.Word_Size