comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Creating several types from a base type and conversion
Date: Fri, 24 Jan 2020 15:54:42 +0000
Date: 2020-01-24T15:54:42+00:00	[thread overview]
Message-ID: <lyd0b8onnh.fsf@pushface.org> (raw)
In-Reply-To: c9dc4033-5a17-4ded-8f8f-86edb1321d70@googlegroups.com

Optikos <ZUERCHER_Andreas@outlook.com> writes:

> Instead of depending on a big-language solution of a very-feature-rich
> compiler, there is a common industrial practice in both Ada and other
> languages that I mentioned in my 2 replies: use 2 different packages
> with same-named identifiers (especially functions or procedures) to
> link in either the little-endian implementation or big-endian
> implementation

An alternative approach (which I've now abandoned in favour of the GNAT
Scalar_Storage_Order) was, given e.g.

   type SNTP_Timestamp is delta 2.0 ** (-32) range -2.0 ** 31 .. 2.0 ** 31;
   for SNTP_Timestamp'Size use 64;

   subtype Timestamp_Slice is Ada.Streams.Stream_Element_Array (1 .. 8);

to provide conversion functions like

   function To_Timestamp_Slice
     (T : SNTP_Timestamp) return Timestamp_Slice is
      function Convert
      is new Ada.Unchecked_Conversion (SNTP_Timestamp,
                                       Timestamp_Slice);
      Tmp : constant Timestamp_Slice := Convert (T);
   begin
      if Big_Endian then
         return Tmp;
      else
         return (1 => Tmp (8),
                 2 => Tmp (7),
                 3 => Tmp (6),
                 4 => Tmp (5),
                 5 => Tmp (4),
                 6 => Tmp (3),
                 7 => Tmp (2),
                 8 => Tmp (1));
      end if;
   end To_Timestamp_Slice;

(Big_Endian was a compile-time constant; GNAT is clever enough not to
generate any object code for the "other" branch).

It got a bit more interesting where there were bit fields involved; the
two branches can declare appropriately-represented derived types and use
type conversion between base and derived type to get the compiler to do
the hard work. See e.g. [1] starting at line 43.

[1] https://sourceforge.net/p/coldframe/adasntp/code/ci/Rel_20070311/tree/SNTP.impl/sntp_support.adb


  parent reply	other threads:[~2020-01-24 15:54 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-18  7:32 Creating several types from a base type and conversion Ken Roberts
2020-01-18 12:16 ` Simon Wright
2020-01-18 12:49   ` Ken Roberts
2020-01-18 14:56     ` Bill Findlay
2020-01-18 16:13       ` Jeffrey R. Carter
2020-01-18 18:20         ` Bill Findlay
2020-01-18 18:32           ` Jeffrey R. Carter
2020-01-18 20:34             ` Simon Wright
2020-01-20 16:38               ` Bill Findlay
2020-01-18 22:20       ` Ken Roberts
2020-01-18 15:09     ` Simon Wright
2020-01-18 22:16       ` Ken Roberts
2020-01-18 22:35         ` Simon Wright
2020-01-18 23:03           ` Ken Roberts
2020-01-18 23:38             ` Simon Wright
2020-01-19  0:12               ` Ken Roberts
2020-01-19  9:37                 ` Simon Wright
2020-01-19 11:48                   ` AdaMagica
2020-01-19 14:51                     ` Simon Wright
2020-01-19 15:24                       ` Niklas Holsti
2020-01-19 16:11                     ` Optikos
2020-01-19  0:33               ` Ken Roberts
2020-01-19  0:07         ` Niklas Holsti
2020-01-18 15:47     ` Simon Wright
2020-01-21 21:35     ` Shark8
2020-01-21 23:06       ` Niklas Holsti
2020-01-22  1:08         ` Ken Roberts
2020-01-22 14:18           ` Ken Roberts
2020-01-22  8:37       ` Simon Wright
2020-01-22 14:32         ` Shark8
2020-01-22 15:40           ` Simon Wright
2020-01-18 14:17   ` Optikos
2020-01-18 17:57 ` Niklas Holsti
2020-01-18 22:59   ` Ken Roberts
2020-01-19  0:30     ` Niklas Holsti
2020-01-19  1:07       ` Ken Roberts
2020-01-19  3:37 ` Ken Roberts
2020-01-23 21:39 ` Optikos
2020-01-24  9:35   ` Ken Roberts
2020-01-24 10:04     ` AdaMagica
2020-01-24 12:38     ` Optikos
2020-01-24 15:01       ` Ken Roberts
2020-01-24 15:22         ` Simon Wright
2020-01-24 15:40           ` Ken Roberts
2020-01-24 15:54   ` Simon Wright [this message]
2020-01-25 10:37 ` Ken Roberts
2020-01-25 10:44   ` Ken Roberts
2020-01-25 20:26   ` Shark8
2020-01-27 14:10 ` Ken Roberts
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox