From: anon@att.net
Subject: Re: User defined type conversion
Date: Sun, 12 May 2013 15:02:40 +0000 (UTC)
Date: 2013-05-12T15:02:40+00:00 [thread overview]
Message-ID: <kmoauf$lm1$1@speranza.aioe.org> (raw)
In-Reply-To: c9c8b269-fb9e-4715-b48c-40047fff1658@googlegroups.com
with Interfaces ;
with Text_IO ;
with Unchecked_Conversion ;
procedure RTest is
use Interfaces ;
use Text_IO ;
-- Method One: Use pragma Unchecked_Union
type Type_08 is record
A8 : Unsigned_8 ;
B8 : Unsigned_8 ;
C8 : Unsigned_8 ;
D8 : Unsigned_8 ;
end record ;
type Type_16 is record
A16 : Unsigned_16 ;
B16 : Unsigned_16 ;
end record ;
type Rec is ( bit32, bit16, bit8 ) ;
type ABC ( Option : Rec := bit32 ) is record
case Option is
when bit8 =>
Data_08 : Type_08 ;
when bit16 =>
Data_16 : Type_16 ;
when bit32 =>
Data_32 : Unsigned_32 ;
end case;
end record;
pragma Unchecked_Union ( ABC ) ;
Test : ABC ;
-- Method Two: Use arrays with Unchecked_Conversion function
type Array_08 is array ( 0 .. 3 ) of Unsigned_8 ;
type Array_16 is array ( 0 .. 1 ) of Unsigned_16 ;
Data_08 : Array_08 ;
Data_16 : Array_16 ;
function Convert is new Unchecked_Conversion
( Source => Array_08,
Target => Array_16 ) ;
function Convert is new Unchecked_Conversion
( Source => Array_16,
Target => Array_08 ) ;
begin
----------------------------------------------
-- Method One: Use pragma Unchecked_Union --
----------------------------------------------
--
-- To access 32 bit variables
--
Test.Data_32 := 0 ;
--
-- To access 16 bit variables
--
Test.Data_16.A16 := 0 ;
Test.Data_16.B16 := 1023 ;
--
-- To access 8 bit variables
--
Put ( Unsigned_8 ' Image ( Test.Data_08.A8 ) ) ;
Put ( Unsigned_8 ' Image ( Test.Data_08.B8 ) ) ;
Put ( Unsigned_8 ' Image ( Test.Data_08.C8 ) ) ;
Put ( Unsigned_8 ' Image ( Test.Data_08.D8 ) ) ;
New_Line ;
-----------------------------------------------------------------
-- Method Two: Use arrays with Unchecked_Conversion function --
-----------------------------------------------------------------
Data_08 := ( 16#FE#, 16#FF#, 16#FF#, 16#FE# ) ;
Data_16 := Convert ( Data_08 ) ;
Put ( Unsigned_16 ' Image ( Data_16 ( 0 ) ) ) ;
Put ( Unsigned_16 ' Image ( Data_16 ( 1 ) ) ) ;
New_Line ;
end ;
In <c9c8b269-fb9e-4715-b48c-40047fff1658@googlegroups.com>, =?ISO-8859-1?Q?Vin=EDcius_Franchini?= <viniciusnf@gmail.com> writes:
>Hi Folks,
> I have a problem with a type conversion. Hereby is what I'm trying to do:
>
>type AB is tagged
> record
> A : Unsigned_16;
> B : Unsigned_16;
> end record;
>
>type abcde is tagged
> record
> a: Unsigned_8;
> b: Unsigned_8;
> c: Unsigned_8;
> d: Unsigned_8;
> end record;
>
>X : AB;
>Y : abcd;
>
>X := Y;
>
>I'd like to do a direct conversion without creating a method for it.
prev parent reply other threads:[~2013-05-12 15:02 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
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 [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox