Providing read-only access to a variable may (of course?) be achieved through abstraction. For example, I could make the variable an in mode parameter of a callable entity, or of a generic. The uses of the variable (through these constant views) would then be confined to the callable or generic instance.
This structure is not easy to add to an existing program, I'd think, since the program already has been structured; also, it is not an independent solution as it requires coupling between the “read-only-ness” and the structure.
Another option is to make the variable private and export a function that returns its value. However, I wanted direct exposure, e.g. of a volatile constant that still is a variable from a different point of view.
I came up with an overlay:
with Interfaces;
package Read_Only is
subtype Pins is Interfaces.Unsigned_16;
V : constant Pins with Volatile, Import;
private
Backing : Pins with Volatile;
for V'Address use Backing'Address;
procedure Reset;
end Read_Only;
This shields V
so that only the package body (and children) can modify its value, while clients of the package can read V
. But “hiding” all this behind aspects and addresses makes me think: Is there some other, more obvious way?
Aucun commentaire:
Enregistrer un commentaire