From: Adam Beneschan <adam@irvine.com>
Subject: Re: User defined type conversion
Date: Thu, 9 May 2013 07:50:26 -0700 (PDT)
Date: 2013-05-09T07:50:26-07:00 [thread overview]
Message-ID: <58b02858-3292-438b-bb21-4105f85ada0d@googlegroups.com> (raw)
In-Reply-To: <c9c8b269-fb9e-4715-b48c-40047fff1658@googlegroups.com>
On Thursday, May 9, 2013 7:07:33 AM UTC-7, Vinícius Franchini wrote:
> 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.
Yes, I'd often like the program to do what I want done without having to write any code. Unfortunately, Ada is not expected to have mind-reading capabilities until at least Ada 2030.
I don't know what you mean by "direct conversion". Whatever you're doing, it's not all that direct. Obviously, using Unchecked_Conversion on the entire record type is a very bad idea since these are tagged records. There's going to be at least one piece of data in Y that is not the same as any data in X, i.e whatever data the compiler sets aside for "tag" information. So you'll have to define your own function
function To_AB (Source : ABCD) return AB is ...
How you write it depends on what you're trying to do. If the Unsigned_* types are the ones defined in Interfaces, and you're trying to combine Source.a and Source.b into an A field, and Source.c and Source.d into a B field, I'd probably just use the "or" operator and Shift_Left to produce the results, something like
begin
return (A => Shift_Left(Unsigned_16(Source.a), 8) or
Unsigned_16(Source.b),
B => Shift_Left(Unsigned_16(Source.c), 8) or
Unsigned_16(Source.d));
end;
or something similar depending on exactly what you're trying to do. I haven't tested the above.
-- Adam
next prev parent reply other threads:[~2013-05-09 14:50 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 [this message]
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox