From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,97439a4c062cf25d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.224.217.195 with SMTP id hn3mr10107773qab.5.1368115961502; Thu, 09 May 2013 09:12:41 -0700 (PDT) X-Received: by 10.182.129.116 with SMTP id nv20mr138267obb.10.1368115961441; Thu, 09 May 2013 09:12:41 -0700 (PDT) Path: y6ni20871qax.0!nntp.google.com!m7no4060414qam.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 9 May 2013 09:12:41 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.20.190.126; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 69.20.190.126 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: User defined type conversion From: Shark8 Injection-Date: Thu, 09 May 2013 16:12:41 +0000 Content-Type: text/plain; charset=ISO-8859-1 Date: 2013-05-09T09:12:41-07:00 List-Id: 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;