From: Shark8 <onewingedshark@gmail.com>
Subject: Re: User defined type conversion
Date: Thu, 9 May 2013 09:12:41 -0700 (PDT)
Date: 2013-05-09T09:12:41-07:00 [thread overview]
Message-ID: <cacc4bc3-f564-400b-aa4a-7b4feb21e741@googlegroups.com> (raw)
In-Reply-To: <c9c8b269-fb9e-4715-b48c-40047fff1658@googlegroups.com>
Here's two ways to do it:
type AB is tagged
record
A, B : Unsigned_16;
end record;
type abcd is tagged
record
a,b,c,d: Unsigned_8;
end record;
----------------------------------------------
Function Convert(Input : AB) Return abcd is
-- Note, these records could have dummy variables and layout
-- clauses to exactly match the tagged-record's fields.
type ut_AB is record
A, B : Unsigned_16;
end record;
type ut_abcd is record
a,b,c,d: Unsigned_8;
end record;
Function Convert is new unchecked_conversion
( source => ut_AB, target => ut_abcd);
-- Here we overlay the non-tagged record atop the tagged
-- record's fields.
Working : ut_ab
with import, convention => Ada, address => input.A'Address;
begin
Return Result : abcd do
declare
Result_Subsection : ut_abcd
with Import, Convention => Ada, Address => Result.A'Address;
begin
-- Apply the conversion.
Result_Subsection:= Convert(working);
end;
end return;
end Convert;
-- This is safer than the above.
Function Convert_Fields(Input : AB) Return abcd is
Function High( Input : Unsigned_16 ) Return Unsigned_8 is
begin
Return Unsigned_8(Shift_Right(Input, 8));
end High;
Function Low( Input : Unsigned_16 ) Return Unsigned_8 is
begin
Return Unsigned_8( 16#00FF# and Input );
End Low;
A : Unsigned_8 renames High ( Input.A );
B : Unsigned_8 renames Low ( Input.A );
C : Unsigned_8 renames High ( Input.B );
D : Unsigned_8 renames Low ( Input.B );
begin
Return ( A, B, C, D );
End Convert_Fields;
next prev parent reply other threads:[~2013-05-09 16:12 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-09 14:07 User defined type conversion Vinícius Franchini
2013-05-09 14:50 ` Adam Beneschan
2013-05-09 16:12 ` Shark8 [this message]
2013-05-09 16:50 ` Adam Beneschan
2013-05-09 18:24 ` Jeffrey Carter
2013-05-09 18:39 ` Vinícius Franchini
2013-05-09 19:15 ` Adam Beneschan
2013-05-09 20:00 ` Vinícius Franchini
2013-05-09 20:19 ` Jeffrey Carter
2013-05-12 7:04 ` ldries46
2013-05-12 14:13 ` AdaMagica
2013-05-12 15:02 ` anon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox