Question

I'm trying to have a device register as an enumeration. Reading from the register has 2 values -> 0 means Done, 1 means Pending. Likewise, writing into the register has 2 values -> 0 has no action and 1 does a reset. So, I wrote the following code

   type Soft_Reset is (Done, Pending, No_Action, Reset);
   for Soft_Reset use
     (Done      => 0,
      Pending   => 1,
      No_Action => 0,
      Reset     => 1);

But this throws an error

gcc-4.6 -c -g -gnatg -ggdb -I- -gnatA /home/sid/tmp/device.adb
device.ads:93:20: enumeration value for "No_Action" not ordered
gnatmake: "/home/sid/tmp/device.adb" compilation error

Is it possible for a enumeration to have duplicate values?

Was it helpful?

Solution

I don't think so. But I think it would be more elegant for you to create two enumerated types indicating the one that corresponds to the possible readable values of the register and the other one which corresponds to the writable values.

Something like:

type Register_Status is (Done, Pending)    -- Values that can be read
type Soft_Reset      is (No_Action, Reset) -- Values that can be written

Gneuromante's post at the bottom is a direct answer to your question.

OTHER TIPS

Another option would be to rename the values. An enumeration value can be renamed as a function:

type Soft_Reset is (Done, Pending);
for Soft_Reset use
     (Done      => 0,
      Pending   => 1);

function No_Action return Soft_Reset renames Done;
function Reset return Soft_Reset renames Pending;

No, you can't use the same values for different enumerations representations. But there's a bit more to it than just not being able to use the same values, as the values must be "distinct" and "satisfy the predefined ordering relation of the type", i.e. be in ascending order. RM 13.4(6/2).

Arjun's suggestion of using two distinct types for this purpose is excellent. It both eliminates your specific problem and makes clear the distinction between read codes and write codes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top