comp.lang.ada
 help / color / mirror / Atom feed
* Extending Nested Tagged Records
@ 1999-12-06  0:00 Robert B. Love 
  1999-12-06  0:00 ` tmoran
  0 siblings, 1 reply; 3+ messages in thread
From: Robert B. Love  @ 1999-12-06  0:00 UTC (permalink / raw)



Can a tagged record owning a tagged record type have the inner
record extended?  In trying to understand how the type extension
mechanism in Ada 95 works I'm replacing nested variant records with 
tagged types.  Does this even make sense?  Or is a complete redesign
the better way to go?

Here is a code snippet--

type telemetry_type is tagged 
	record
		speed: real;
		error_flag: boolean;
	end record;


type motorized_device is tagged
	record
		speed: real;
		torque : real;
		current_cmd : cmd_type;
		telemetry: telemetry_type;   -- this is what I want 
	end record;                         -- to extend

type device_1 is new motorized_device with
	record
		shaft_position: real;
		shaft_load : real;
	end record;

type device_2 is new motorized_device with
	record
		latch_angle: real;
		end_of_travel_switch: boolean;
	end record

In the definitions of device_1 and device_2 how can I extend the
telemetry type?



--
----------------------------------------------------------------
 Bob Love                                   
 rlove@neosoft.com                            
----------------------------------------------------------------





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Extending Nested Tagged Records
  1999-12-06  0:00 Extending Nested Tagged Records Robert B. Love 
@ 1999-12-06  0:00 ` tmoran
  0 siblings, 0 replies; 3+ messages in thread
From: tmoran @ 1999-12-06  0:00 UTC (permalink / raw)


>type telemetry_type is tagged  ...
>
>type motorized_device is tagged record
>          telemetry: telemetry_type;   -- this is what I want to extend
>...
>type device_1 is new motorized_device with
>...
  So the telemetry module is not a black box, but may have some things
added when it's in a device_1 or different things when it's in a device_2?
One approach is to change "telemetry_type" to "basic_telemetry_type",
indicating that it's the rock bottom minimum that any motorized_device
contains, and then have add-ons like
  type device_1 is new motorized_device with
    extra_telemetry : ...

Alternatively, drop telemetry entirely from "motorized_device" and instead
put appropriate kinds of telemetry in different devices.
  type telemetry_type_1 is new telemetry_type with ... -- used in device_1
  type telemetry_type_2 is new telemetry_type with ... -- used in device_2
  type device_1 is new motorized_device with
    telemetry : telemetry_type_1;
    ...

You could of course include an "access telemetry_type'class" instead
of a "telemetry_type" in motorized_device, and then install the
appropriate kind in each kind of device.  That would also let you
have an arbitrary telemetry package in each device type - which
perhaps you don't want.

What are you trying to accomplish?




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Extending Nested Tagged Records
       [not found] <84E639D0D9829A88.356169F32CD3866A.7936C8200D458BB1@lp.airnews. net>
@ 1999-12-06  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1999-12-06  0:00 UTC (permalink / raw)


In article 
<84E639D0D9829A88.356169F32CD3866A.7936C8200D458BB1@lp.airnews.net> ,
rlove@antispam.neosoft.com (Robert B. Love ) wrote:

>
> Can a tagged record owning a tagged record type have the inner
> record extended?  In trying to understand how the type extension
> mechanism in Ada 95 works I'm replacing nested variant records with
> tagged types.
>
> Here is a code snippet--

[code snipped]

Here is one idea.  Basically, you let types in motorized_device'class
extend themselves with extensions of Telemetry_Type, but let clients of
the record get at the extension by calling a function that returns a
pointer to the telemetry component.

There's some code below that shows what I mean.

> Does this even make sense?  Or is a complete redesign
> the better way to go?

Do clients of the types use the unencapsulated record?  Or is the type
private, and clients call primitive operations of the type?

The code below assumes the former.  However, if clients call primitive
operations, then there's no real problem (because you can implement the
(private) type anyway you want, and clients are oblivious.)

Maybe a mixin approach can be used.  Or maybe record composition using
access discriminants.


--STX
package P is

   type root_motorized_device is
     abstract tagged record
         speed: Float;
         torque : Float;
         current_cmd : string (1..10);
     end record;

   type telemetry_type is
     tagged record
         speed: Float;
         error_flag: boolean;
      end record;

   type Telemetry_Access is access all Telemetry_Type'Class;

   function Telemetry (Device : access Root_Motorized_Device)
     return Telemetry_Access is abstract;


   type motorized_device is
     new root_motorized_device with record
        Telemetry : aliased Telemetry_Type;
     end record;

   function Telemetry (Device : access Motorized_Device)
     return Telemetry_Access;


   type Device_1_Telemetry_Type is
     new Telemetry_Type with record
        I : Integer;
     end record;

   type device_1 is new root_motorized_device with
        record
                shaft_position: Float;
                shaft_load : Float;
                telemetry: aliased Device_1_Telemetry_Type;
        end record;

   function Telemetry (Device : access Device_1)
     return Telemetry_Access;


   type Device_2_Telemetry_Type is
     new Telemetry_Type with record
        F : Float;
     end record;

   type device_2 is new root_motorized_device with
        record
                latch_angle: Float;
                end_of_travel_switch: boolean;
                 telemetry: aliased Device_2_Telemetry_Type;
        end record;

   function Telemetry (Device : access Device_2)
     return Telemetry_Access;

end P;


package body P is

   function Telemetry
     (Device : access Motorized_Device)
      return Telemetry_Access is
   begin
      return Device.Telemetry'Access;
   end Telemetry;

   function Telemetry
     (Device : access Device_1)
      return Telemetry_Access is
   begin
      return Device.Telemetry'Access;
   end Telemetry;

   function Telemetry
     (Device : access Device_2)
      return Telemetry_Access is
   begin
      return Device.Telemetry'Access;
   end Telemetry;

end P;





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-12-06  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-06  0:00 Extending Nested Tagged Records Robert B. Love 
1999-12-06  0:00 ` tmoran
     [not found] <84E639D0D9829A88.356169F32CD3866A.7936C8200D458BB1@lp.airnews. net>
1999-12-06  0:00 ` Matthew Heaney

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