Ada 枚举到值 [英] Ada Enums to Values

查看:24
本文介绍了Ada 枚举到值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用另一种方式处理这个问题,但不是那么干净,但希望能够利用以下优势:

I can handle this another, not as clean way, but was hoping to be able to take advantage of the following:

type Prefix is (Yocto, Zepto, Atto, Femto, Pico, Nano,
    Micro, Milli, Centi, Deci, None, Deca, Hecto, Kilo,
    Mega, Giga, Tera, Peta, Exa, Zetta, Yotta);
for Prefix use (
    Yocto => -24,
    Zepto => -21,
    Atto => -18,
    Femto => -15,
    Pico => -12,
    Nano => -9,
    Micro => -6,
    Milli => -3,
    Centi => -2,
    Deci => -1,
    None => 0,
    Deca => 1,
    Hecto => 2,
    Kilo => 3,
    Mega => 6,
    Giga => 9,
    Tera => 12,
    Peta => 15,
    Exa => 18,
    Zetta => 21,
    Yotta => 24);

GNAT 不会抱怨代表条款.但是,我似乎无法实际获取值,因为我知道与此相关的唯一属性与位置有关,而不是分配的值.

GNAT doesn't complain about the representation clause. However, I can't seem to actually get the value out, since the only attributes related to this, that I am aware of, have to do with position, not assigned value.

推荐答案

枚举从未用于此类目的,您不应该尝试将它们用于此目的.(我认为主要目的是在某些外部表示或硬件寄存器或其他东西中定义具有 0、1、... 以外的值的枚举.)但是,您实际上可以在保持代码几乎相同的同时解决这个问题:

Enumerations were never intended for this sort of purpose, and you shouldn't try to use them for that. (I think the main purpose was to define enumerations that had values other than 0, 1, ... in some external representation or a hardware register or something.) However, you can actually fix this while keeping your code almost the same:

type Prefix is (Yocto, Zepto, Atto, Femto, Pico, Nano,
    Micro, Milli, Centi, Deci, None, Deca, Hecto, Kilo,
    Mega, Giga, Tera, Peta, Exa, Zetta, Yotta);
type Prefix_To_Integer_Map is array (Prefix) of Integer;
Power_of_Ten : constant Prefix_To_Integer_Map := (
    Yocto => -24,
    Zepto => -21,
    Atto => -18,
    Femto => -15,
    Pico => -12,
    Nano => -9,
    Micro => -6,
    Milli => -3,
    Centi => -2,
    Deci => -1,
    None => 0,
    Deca => 1,
    Hecto => 2,
    Kilo => 3,
    Mega => 6,
    Giga => 9,
    Tera => 12,
    Peta => 15,
    Exa => 18,
    Zetta => 21,
    Yotta => 24);

应该和你所拥有的一样干净.并且说 Power_Of_Ten (My_Prefix)My_Prefix'Enum_RepPrefix'Enum_Rep(My_Prefix) 或其他任何东西更具描述性.

Should be about as clean as what you had. And saying Power_Of_Ten (My_Prefix) is more descriptive than My_Prefix'Enum_Rep or Prefix'Enum_Rep(My_Prefix) or whatever it is.

这篇关于Ada 枚举到值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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