comp.lang.ada
 help / color / mirror / Atom feed
* Booch: missing r/w access through iterators
@ 2002-11-06 14:18 Victor Porton
  2002-11-06 16:42 ` Simon Wright
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Victor Porton @ 2002-11-06 14:18 UTC (permalink / raw)


In Booch generic package BC.Containers there are
function Current_Item which gives read-only access
to the current item. Why a function giving direct
read-write access to the current item (by returning
access to it) is missing?!



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

* Re: Booch: missing r/w access through iterators
  2002-11-06 14:18 Booch: missing r/w access through iterators Victor Porton
@ 2002-11-06 16:42 ` Simon Wright
  2002-11-07  4:21 ` Victor Porton
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Simon Wright @ 2002-11-06 16:42 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> In Booch generic package BC.Containers there are
> function Current_Item which gives read-only access
> to the current item. Why a function giving direct
> read-write access to the current item (by returning
> access to it) is missing?!

Actually Current_Item gives you a copy.

I think you are looking for

   generic
      with procedure Apply (Elem : in out Item);
   procedure Access_Current_Item (In_The_Iterator : Iterator'Class);
   --  Call Apply for the Iterator's current Item.

(on the line after Current_Item).



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

* Re: Booch: missing r/w access through iterators
  2002-11-06 14:18 Booch: missing r/w access through iterators Victor Porton
  2002-11-06 16:42 ` Simon Wright
@ 2002-11-07  4:21 ` Victor Porton
  2002-11-07 21:04   ` Simon Wright
  2002-11-08 20:37   ` Matthew Heaney
  2002-11-08  5:21 ` Victor Porton
  2002-11-08 20:33 ` Matthew Heaney
  3 siblings, 2 replies; 9+ messages in thread
From: Victor Porton @ 2002-11-07  4:21 UTC (permalink / raw)


In article <x7v8z06tzt5.fsf@galadriel.frlngtn.gecm.com>,
	Simon Wright <simon.j.wright@amsjv.com> writes:
> porton@ex-code.com (Victor Porton) writes:
> 
>> In Booch generic package BC.Containers there are
>> function Current_Item which gives read-only access
>> to the current item. Why a function giving direct
>> read-write access to the current item (by returning
>> access to it) is missing?!
> 
> I think you are looking for
> 
>    generic
>       with procedure Apply (Elem : in out Item);
>    procedure Access_Current_Item (In_The_Iterator : Iterator'Class);
>    --  Call Apply for the Iterator's current Item.

I noticed Access_Current_Item, but with it to gain such access one
needs to create a new procedure! Too bad. I want direct access like:

function Current_Item_Access(It: Iterator'Class) return Item_Access;



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

* Re: Booch: missing r/w access through iterators
  2002-11-07  4:21 ` Victor Porton
@ 2002-11-07 21:04   ` Simon Wright
  2002-11-08 20:49     ` Matthew Heaney
  2002-11-08 20:37   ` Matthew Heaney
  1 sibling, 1 reply; 9+ messages in thread
From: Simon Wright @ 2002-11-07 21:04 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> I noticed Access_Current_Item, but with it to gain such access one
> needs to create a new procedure! Too bad. I want direct access like:
> 
> function Current_Item_Access(It: Iterator'Class) return Item_Access;

I can see why you might want it, and at the moment I can't remember
why it's not there. Perhaps something to do with the problem of
creating the access value? (it would force the item in the Container
to be aliased, thus constraining it .. I know this actually happens in
the BCs but I was very unhappy with the necessary kluge).



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

* Re: Booch: missing r/w access through iterators
  2002-11-06 14:18 Booch: missing r/w access through iterators Victor Porton
  2002-11-06 16:42 ` Simon Wright
  2002-11-07  4:21 ` Victor Porton
@ 2002-11-08  5:21 ` Victor Porton
  2002-11-08 20:53   ` Matthew Heaney
  2002-11-08 20:33 ` Matthew Heaney
  3 siblings, 1 reply; 9+ messages in thread
From: Victor Porton @ 2002-11-08  5:21 UTC (permalink / raw)


In article <x7vn0ol148m.fsf@smaug.pushface.org>,
	Simon Wright <simon@pushface.org> writes:
> porton@ex-code.com (Victor Porton) writes:
> 
>> I noticed Access_Current_Item, but with it to gain such access one
>> needs to create a new procedure! Too bad. I want direct access like:
>> 
>> function Current_Item_Access(It: Iterator'Class) return Item_Access;
> 
> I can see why you might want it, and at the moment I can't remember
> why it's not there. Perhaps something to do with the problem of
> creating the access value? (it would force the item in the Container
> to be aliased, thus constraining it .. I know this actually happens in
> the BCs but I was very unhappy with the necessary kluge).

So, if you insist on not aliased container items in general, you 
however can (and please do so):

1. Add procedure to assign a value to the current item:

procedure Assign_Current_Item (It : Iterator; Value : Item) is abstract;
-- Alternatively it can be not abstract but using Iterator_Operations
-- (see below).

2. For every iterator type add the following generic subpackage (in the
same package as the corresponding iterator type):

generic
   It: in out Iterator'Class;
package Iterator_Operations is

   Element: Item renames ...;
   -- The container element to which It refers.
   
end Iterator_Operations;

where "..." is different for each iterator type.



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

* Re: Booch: missing r/w access through iterators
  2002-11-06 14:18 Booch: missing r/w access through iterators Victor Porton
                   ` (2 preceding siblings ...)
  2002-11-08  5:21 ` Victor Porton
@ 2002-11-08 20:33 ` Matthew Heaney
  3 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-08 20:33 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) wrote in message news:<3dc92622$0$308$bed64819@news.gradwell.net>...
> In Booch generic package BC.Containers there are
> function Current_Item which gives read-only access
> to the current item. Why a function giving direct
> read-write access to the current item (by returning
> access to it) is missing?!

The Charles library has such a function:

   generic
      type Element_Access is 
         access all Element_Type;
   function Generic_Element 
     (Iterator : Iterator_Type) return Element_Access;

This allows you to modify elements in place:

   type Element_Access is
     access all Element_Subtype;

   function To_Access is
     new Generic_Element (Element_Access);   

   declare
      E : Element_Type renames To_Access (Iterator).all;
   begin
      <now manipulate actual element object>
   end;

You can get the latest version of Charles at my home page:

http://home.earthlink.net/~matthewjheaney/charles/index.html



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

* Re: Booch: missing r/w access through iterators
  2002-11-07  4:21 ` Victor Porton
  2002-11-07 21:04   ` Simon Wright
@ 2002-11-08 20:37   ` Matthew Heaney
  1 sibling, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-08 20:37 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) wrote in message news:<3dc9ec63$0$300$bed64819@news.gradwell.net>...
> 
> I noticed Access_Current_Item, but with it to gain such access one
> needs to create a new procedure! Too bad. I want direct access like:
> 
> function Current_Item_Access(It: Iterator'Class) return Item_Access;

The Charles library does indeed have such a function:

   generic
      type Element_Access is
        access all Element_Type;
   function Generic_Element 
     (Iterator : Iterator_Type) return Element_Access;

This allows you to do this:

   function To_Access is new Generic_Element (...);

   declare
     E : Element_Subtype renames To_Access (Iterator).all;
   begin
     <now you have access to actual object>
   end;

The library is hosted at my web page:

http://home.earthlink.net/~matthewjheaney/charles/index.html



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

* Re: Booch: missing r/w access through iterators
  2002-11-07 21:04   ` Simon Wright
@ 2002-11-08 20:49     ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-08 20:49 UTC (permalink / raw)


Simon Wright <simon@pushface.org> wrote in message news:<x7vn0ol148m.fsf@smaug.pushface.org>...
> porton@ex-code.com (Victor Porton) writes:
> 
> I can see why you might want it, and at the moment I can't remember
> why it's not there. Perhaps something to do with the problem of
> creating the access value? (it would force the item in the Container
> to be aliased, thus constraining it .. I know this actually happens in
> the BCs but I was very unhappy with the necessary kluge).

Simon is correct that the elements in the container will have to be
aliased.  If you have an element type that is an definite,
unconstrained record, like this:

   type DT is (A, B, C);

   type RT (D : DT := DT'First) is record ... end record;

then the simplest thing to do is wrap this in another record:

   type Element_Type is
      record
         R : RT;
      end record;

and then instantiate the component with the wrapper type:

   package Set_Types is 
     new Charles.Sets.Unbounded (Element_Type, ...);

Now you can do this to get at the element:

   type Element_Access is 
     access all Element_Type;

   function To_Access is
     new Generic_Element (Element_Access);

   declare
      E : Element_Type renames To_Access (Iterator).all;
   begin
      <now modify E.R>
   end;

The Charles library is available at my home page:

http://home.earthlink.net/~matthewjheaney/charles/index.html

The reason this restriction exists is because of access subtypes.  For
example, given our discriminated record type above, we can do this:

   type R_Access is access RT;

   subtype RA_Access is R_Access (A);

The issue is that if you have this:

   RA : RA_Access := new RT'(D => A, ...);

then you always have a guarantee that the discriminated record object
designated by access object RA always has a discriminant with the
value DT'(A).

You're not allowed to change the discriminant of an aliased
unconstrained discriminated record, because that would break the
guarantee.

This is arguably a flaw in the language.  The type model is consistent
in that you it supports subtypes of types, but extending that model to
include access types too was probably too much of a good thing.

The problem is that you need an aliased unconstrained discriminated
record a lot more often than you need an access subtype.  Hence the
problem to which Simon was refering.



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

* Re: Booch: missing r/w access through iterators
  2002-11-08  5:21 ` Victor Porton
@ 2002-11-08 20:53   ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-08 20:53 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) wrote in message news:<3dcb4b43$0$307$bed64819@news.gradwell.net>...
> 
> So, if you insist on not aliased container items in general, you 
> however can (and please do so):
> 
> 1. Add procedure to assign a value to the current item:
> 
> procedure Assign_Current_Item (It : Iterator; Value : Item) is abstract;
> -- Alternatively it can be not abstract but using Iterator_Operations
> -- (see below).

The Charles library already has this operation:

   procedure Replace_Element 
     (Iterator : Iterator_Type;
      Item     : Element_Type);

However, getting a copy of the element out of the container, modifying
the copy, and then replacing the element with the value of the copy is
probably not as efficient as simply modifying the element in place, as
I have illustrated in previous posts.

Charles is available at my home page:

http://home.earthlink.net/~matthewjheaney/charles/index.html



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

end of thread, other threads:[~2002-11-08 20:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-06 14:18 Booch: missing r/w access through iterators Victor Porton
2002-11-06 16:42 ` Simon Wright
2002-11-07  4:21 ` Victor Porton
2002-11-07 21:04   ` Simon Wright
2002-11-08 20:49     ` Matthew Heaney
2002-11-08 20:37   ` Matthew Heaney
2002-11-08  5:21 ` Victor Porton
2002-11-08 20:53   ` Matthew Heaney
2002-11-08 20:33 ` Matthew Heaney

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