Concat将字段分隔为一个 [英] Concat separated fields in one
问题描述
我在Ada工作,我有一个很难修改的丑陋类型,我想做些易于使用的东西。
I'm working in Ada, I have a very ugly type that I can't modify and I want to do something easy to use.
该类型是像这样的东西:
The type is something like this :
for T_Ugly_Type'Alignment use 4;
for T_Ugly_Type'Size use 48;
for T_Ugly_Type use record
Value_Bits_00_07 at 16#00# range 0 .. 7; -- 8 bits
Field1 at 16#00# range 8 .. 15; -- 8 bits
Field2 at 16#02# range 0 .. 11; -- 12 bits
Value_Bits_08_11 at 16#02# range 12 .. 15; -- 4 bits
Value_Bits_12_15 at 16#03# range 0 .. 3; -- 4 bits
Field3 at 16#03# range 4 .. 15; -- 12 bits
end record;
此结构由一条消息填充,并且地址无法更改。但是我的 Value
是16位类型,分为3部分。我不想用我想要使用的每种类型的所有这些零件。
This structure is filled by a message and the addresses cant's be changed. But my Value
is a 16 bits type divided in 3 parts. I don't want to concat all theses parts each type I want to use it.
是否可以使用 T_Ugly_Type.Value
并得到 Value_Bits_00_07 + 2 ** 8 * Value_Bits_08_12 + 2 ** 12 * Value_Bits_12_15
?
推荐答案
那么你可以做这样的事情:
Well you can do something like this:
type T_Wrapper is tagged record
Values : T_Ugly_Type;
end record;
function Value (Subject : T_Wrapper) return Uint16 is
(Subject.Values.Value_Bits_00_07 * 2**8 +
Subject.Values.Value_Bits_08_12 * 2**4 +
Subject.Values.Vaule_Bits_13_15);
function Field1 (Subject : T_Wrapper) return Uint12 is
(Subject.Values.Field1);
function Field2 (Subject : T_Wrapper) return Uint12 is
(Subject.Values.Field2);
function Field3 (Subject : T_Wrapper) return Uint12 is
(Subject.Values.Field3);
被标记为
使您可以使用前缀语法在 T_Wrapper
函数上:
Being tagged
enables you to use prefix syntax on the T_Wrapper
functions:
declare
-- assuming a T_Ugly_Type instance named Orig
Wrapped : T_Wrapper := (Values => Orig);
begin
Do_Something (Wrapped.Value);
Do_Something_Elese (Wrapped.Field1);
end;
请注意,由于未标记原始类型,因此无法声明 Value
函数,您可以使用前缀符号来调用。
Note that since the original type is not tagged, you cannot declare a Value
function on it you can call with prefix notation.
一种更好的方法是使用访问
值和一些Ada 2012功能:
A maybe somewhat nicer way to do this would be to use an access
value and some Ada 2012 features:
type Reference (Data : not null access T_Ugly_Type) is tagged limited null record
with Implicit_Dereference => Data;
function Value (Subject : Reference) return Uint16 is
(Subject.Data.Value_Bits_00_07 * 2**8 +
Subject.Data.Value_Bits_08_12 * 2**4 +
Subject.Data.Vaule_Bits_13_15);
Implicit_Dereference
可让您直接访问原始文件字段,虽然您可以使用 Value
函数进行计算。
Implicit_Dereference
allows you to directly access the original fields, while you can use the Value
function for the calculation.
declare
Ref : Reference := (Data => Orig'Access);
begin
Do_Something (Ref.Value); -- calls function
Do_Something (Ref.Field1); -- accesses Field1 of the inner record
end;
这篇关于Concat将字段分隔为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!