如何获取枚举类型中的元素数量 [英] How to get number of elements in enumerated type

查看:77
本文介绍了如何获取枚举类型中的元素数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于枚举类型,如下所示,是否有一种很好的方法来获取枚举类型中元素的数量enum_t:

type enum_t is (ALFA, BRAVO, CHARLIE);-- 元素数为 3-- 不工作:长度不是 enum_t 的有效属性常量 ENUM_LENGTH : 自然 := enum_t'length;-  非法的!

根据 David Koontz 的回答,可以这样做:

constant ENUM_LENGTH : natural := enum_t'pos(enum_t'right) + 1;

解决方案

先找到它的POSitional值,然后你就可以得到VHDL来告诉你它是什么:

entity enum_length 是最终实体;enum_length 的架构 foo 是类型 enum_t 是 (ALFA, BRAVO, CHARLIE);常量 enum_left: natural := enum_t'POS(ALFA);常量 enum_right: 自然 := enum_t'POS(CHARLIE);开始断言假报告查理 POS =" &自然'图像(枚举右);终端架构;

<块引用>

ghdl -r enum_length
enum_length.vhdl:9:5:@0ms:(断言错误):CHARLIE POS = 2

请参阅 IEEE Std 1076-2008 5.2.2.1(枚举类型)一般,第 6 段:

<块引用>

每个枚举文字产生不同的枚举值.这枚举值之间的预定义顺序关系遵循顺序对应的位置编号.值的位置编号第一个列出的枚举文字为零;位置编号每个额外的枚举文字都比它的枚举文字多一个列表中的前身.

所以最左边的枚举值的位置是0.最右边的枚举值的位置比元素数少1.您还可以找到最右边的 'VAL 并找到它的位置:

entity enum_length 是最终实体;enum_length 的架构 foo 是类型 enum_t 是 (ALFA, BRAVO, CHARLIE);常量 enum_left: natural := enum_t'POS(ALFA);常量 enum_right: 自然 := enum_t'POS(CHARLIE);常量 enum_t_elems: 自然:= enum_t'POS(enum_t'RIGHT) + 1;开始-- 断言假-- 报告 "CHARLIE POS = " &自然'图像(枚举右);断言假报告 "enum_t 元素数 = " &自然'图像(enum_t_elems);终端架构;

<块引用><块引用>

ghdl -r enum_length
enum_length.vhdl:13:5:@0ms:(assertion error): enum_t 元素数 = 3

With an enumerated type, like the below, is there a nice way to get the number of elements in the enumerated type enum_t:

type enum_t is (ALFA, BRAVO, CHARLIE);  -- Number of elements is 3

-- Don't work: length is not valid attribute for enum_t
constant ENUM_LENGTH : natural := enum_t'length;  -- illegal!

Based on answer from David Koontz it can be done as:

constant ENUM_LENGTH : natural := enum_t'pos(enum_t'right) + 1;

解决方案

First find it's POSitional value, then you can get VHDL to tell you what it is:

entity enum_length is
end entity;

architecture foo of enum_length is
    type enum_t is (ALFA, BRAVO, CHARLIE);
    constant enum_left:     natural := enum_t'POS(ALFA);
    constant enum_right:    natural := enum_t'POS(CHARLIE);
begin
    assert FALSE 
         Report "CHARLIE POS = " & natural'IMAGE(enum_right);
end architecture;

ghdl -r enum_length
enum_length.vhdl:9:5:@0ms:(assertion error): CHARLIE POS = 2

See IEEE Std 1076-2008 5.2.2.1 (Enumeration types) General, para 6:

Each enumeration literal yields a different enumeration value. The predefined order relations between enumeration values follow the order of corresponding position numbers. The position number of the value of the first listed enumeration literal is zero; the position number for each additional enumeration literal is one more than that of its predecessor in the list.

So the Leftmost enumeration value's position is 0. The rightmost enumeration value's position is one less than the number of elements. You can also find the rightmost 'VAL and find it's position:

entity enum_length is
end entity;

architecture foo of enum_length is
    type enum_t is (ALFA, BRAVO, CHARLIE);
    constant enum_left:     natural := enum_t'POS(ALFA);
    constant enum_right:    natural := enum_t'POS(CHARLIE);
    constant enum_t_elems:  natural:= enum_t'POS(enum_t'RIGHT) + 1;
begin
--    assert FALSE 
--        Report "CHARLIE POS = " & natural'IMAGE(enum_right);

    assert FALSE 
        Report "enum_t number of elements = " & natural'IMAGE(enum_t_elems);
end architecture;

ghdl -r enum_length
enum_length.vhdl:13:5:@0ms:(assertion error): enum_t number of elements = 3

这篇关于如何获取枚举类型中的元素数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆