comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <bauhaus.rm.tsoh@maps.futureapps.de>
Subject: Re: Self pointer in limited record
Date: Sat, 01 Sep 2007 15:33:09 +0200
Date: 2007-09-01T15:28:14+02:00	[thread overview]
Message-ID: <46d968ee$0$30368$9b4e6d93@newsspool4.arcor-online.net> (raw)
In-Reply-To: <9fy1xoukz1e3$.h574sqmiauri$.dlg@40tude.net>

Dmitry A. Kazakov wrote:
> On Fri, 31 Aug 2007 16:47:29 -0000, amado.alves@gmail.com wrote:
> 
>> Then you have seen illegal Ada code repeatedly. I wish this were
>> possible myself. Or more simply:
>>
>> type T is -- limited or not
>>    Self : access T := T'Unchecked_Access;
>>    ...
>> end;
> 
> T cannot be non-limited, because otherwise passing it by copy would make
> rubbish out of Self. In any case it would make little sense if not access
> T'Class.


I think there is an interesting use of a .Self pointer of
simple limited records with state variables of packages.

Say a procedure in a package P controls the state of some
variable in the package's body. The state variable is of type
access T, where T  is a limited record type declared in P. The
purpose of the state variable is to remember the object of type T
that will be the target of subsequent package operations.

A natural way to remember a particular object is to point
to the object. But package clients should not have to worry
about this pointing mechanism.  So the package shouldn't
declare a public access type used for internal mechanism only.
Instead there is a procedure of one T argument that can be called
by clients when they want to indicate the object to be used
in subsequent P operations.

This is where .Self can be used. We cannot take 'Access
or 'Unchecked_Access of subprogram arguments unless they
are aliased (such as those of type T'Class).
But the .Self component of the subprogram's argument supplies
the access value that is needed for storing a pointer to the
argument in the package body.


package P is

       type T is limited private;

       procedure Choose (Selected: in out T);
		-- sets target T for subsequent operations
       procedure Work;
               
private
       type Bits is array(0 .. 15) of Boolean;
       type T_Access is access all T;
       type T is limited record
               Self: T_Access := T'unchecked_access;
               Slots: Bits;
       end record;
           
end P;

package body P is

       Current: T_Access;  -- state variable, target object

       procedure Choose (Selected: in out T) is
       begin
               Current := Selected.Self;  -- here
       end Choose;

       procedure Work is
       begin
               Current.Slots(3) := not Current.Slots(3);
       end Work;
end P;


(The true O-O programmer might suggest that we should simply
pass an additional object-as-module parameter to every
package subprogram... )



  reply	other threads:[~2007-09-01 13:33 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-04 19:31 Self pointer in limited record Maciej Sobczak
2007-07-05  8:22 ` Dmitry A. Kazakov
2007-07-05 10:35   ` Maciej Sobczak
2007-07-05 11:01     ` Pascal Obry
2007-07-05 11:14     ` Georg Bauhaus
2007-07-05 12:36     ` Dmitry A. Kazakov
2007-08-31 16:47 ` amado.alves
2007-08-31 17:09   ` Pascal Obry
2007-08-31 17:37   ` Adam Beneschan
2007-08-31 18:26   ` Jeffrey R. Carter
2007-08-31 19:33   ` Dmitry A. Kazakov
2007-09-01 13:33     ` Georg Bauhaus [this message]
2007-09-01 13:46       ` Dmitry A. Kazakov
2007-09-01 14:15         ` Georg Bauhaus
2007-09-01 16:03           ` Dmitry A. Kazakov
2007-09-01 19:49             ` Georg Bauhaus
2007-09-01 20:09               ` Dmitry A. Kazakov
2007-09-02 21:37                 ` Georg Bauhaus
     [not found]                   ` <re7ei5lc7dzf$.11qtcnh35jmzg$.dlg@40tude.net>
2007-09-03 10:51                     ` Georg Bauhaus
2007-09-03 14:17                       ` Dmitry A. Kazakov
2007-09-03 15:55                         ` Jean-Pierre Rosen
2007-09-03 19:17                           ` Dmitry A. Kazakov
2007-09-03 19:32                             ` Markus E L
2007-09-03 20:14                             ` Georg Bauhaus
2007-09-04  8:24                               ` Dmitry A. Kazakov
2007-09-04  9:36                                 ` Jean-Pierre Rosen
2007-09-04 10:14                                   ` Dmitry A. Kazakov
2007-09-05 10:49                                 ` Georg Bauhaus
2007-09-05 12:04                                   ` Dmitry A. Kazakov
2007-09-05 13:12                                     ` Jean-Pierre Rosen
2007-09-05 15:10                                       ` Dmitry A. Kazakov
2007-09-05 16:25                                         ` Jean-Pierre Rosen
2007-09-05 19:52                                           ` Dmitry A. Kazakov
2007-09-06  7:19                                             ` Jean-Pierre Rosen
2007-09-06  9:28                                               ` Dmitry A. Kazakov
2007-09-06 11:53                                                 ` Jean-Pierre Rosen
2007-09-06 15:35                                                   ` Dmitry A. Kazakov
2007-09-05 18:31                                     ` Georg Bauhaus
2007-09-05 19:52                                       ` Dmitry A. Kazakov
2007-09-05 21:38                                         ` Georg Bauhaus
2007-09-06  7:37                                           ` Dmitry A. Kazakov
2007-09-06 10:26                                             ` Georg Bauhaus
2007-09-06 12:25                                               ` Dmitry A. Kazakov
2007-09-08  1:27                                               ` Randy Brukardt
2007-09-06  9:14                                         ` Markus E L
2007-09-06  9:48                                           ` Dmitry A. Kazakov
2007-09-04  8:23                             ` Jean-Pierre Rosen
2007-10-31 23:59                           ` adaworks
2007-09-03 20:38                         ` Georg Bauhaus
2007-09-04  8:24                           ` Dmitry A. Kazakov
2007-09-03  7:54             ` Jean-Pierre Rosen
2007-09-01 15:33         ` Markus E L
2007-09-04 14:55           ` Adam Beneschan
2007-09-04 15:09             ` Jean-Pierre Rosen
2007-09-08  1:36               ` Randy Brukardt
2007-09-04 17:31             ` Georg Bauhaus
2007-09-08  1:16     ` Randy Brukardt
2007-09-10 16:27       ` amado.alves
2007-09-10 17:13         ` Adam Beneschan
2007-09-10 19:00         ` Dmitry A. Kazakov
2007-09-11  3:12           ` Randy Brukardt
2007-09-11  9:38             ` Dmitry A. Kazakov
2007-09-12 21:57               ` Randy Brukardt
2007-09-13  8:03                 ` Dmitry A. Kazakov
2007-09-13 21:37                   ` Randy Brukardt
replies disabled

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