具有重复值的Ada枚举

Sid*_*oor 1 enums ada gnat

我正在尝试将设备注册为枚举.从寄存器读取有2个值 - > 0表示完成,1表示待定.同样,写入寄存器有2个值 - > 0没有动作,1个复位.所以,我写了下面的代码

   type Soft_Reset is (Done, Pending, No_Action, Reset);
   for Soft_Reset use
     (Done      => 0,
      Pending   => 1,
      No_Action => 0,
      Reset     => 1);
Run Code Online (Sandbox Code Playgroud)

但这会引发错误

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
Run Code Online (Sandbox Code Playgroud)

枚举是否可能具有重复值?

Arj*_*jun 9

我不这么认为.但是我认为创建两个枚举类型会更加优雅,这两个类型指示对应于寄存器的可能可读值的一个类型,另一个对应于可写值.

就像是:

type Register_Status is (Done, Pending)    -- Values that can be read
type Soft_Reset      is (No_Action, Reset) -- Values that can be written
Run Code Online (Sandbox Code Playgroud)

Gneuromante在帖子底部的帖子直接回答了你的问题.


Gne*_*nte 5

另一种选择是重命名这些值。枚举值可以重命名为函数:

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;
Run Code Online (Sandbox Code Playgroud)