comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: DYNAMIC ADA TASK CREATION?
Date: 1 Jul 2003 07:21:08 -0700
Date: 2003-07-01T14:21:08+00:00	[thread overview]
Message-ID: <1ec946d1.0307010621.e1de68d@posting.google.com> (raw)
In-Reply-To: tnc2gvkr3fu9rpkd0tu56095ttbuflkgi9@4ax.com

Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote in message news:<tnc2gvkr3fu9rpkd0tu56095ttbuflkgi9@4ax.com>...
> 
> I thought the container has only pointers to instances which are
> created by caller, but maybe I missed something.
> 
> [ An advantage in having (<>) for Element is that it could then have
> T'Class as actual. For example:
> 
> type Operand is new Ada.Finalization.Limited_Controlled with private;
> type Operand_Ptr is access all Operand'Class;
> ...
> package Argument_Stack is new Limited_Stack
>    (Element => Operand'Class, Pointer => Operand_Ptr);
> ]

Then we are comparing apples and oranges.  The containers I was
talking about are declared like this:

generic
   type Element_Type is limited private;
package Generic_Container is ...;

Strictly speaking your container doesn't have elements -- it has
pointers to elements.

The model in Charles is that an element is a component of an internal
node of storage, which is designated by an iterator.  If you want an
access object that designates the element, then you can use
Generic_Element to convert ("dereference") the iterator to the access
type.

Your container seems to have two levels of indirection: a pointer to
an internal node contains a pointer to the element.  In Charles there
is only a single level of indirection.

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

Of course, there is still the problem of a container of elements of
indefinite type T'Class, which your container more or less solves.

There are at least a couple of ways to do that in Charles.  One way is
to use a nonlimited container of pointers:

package T_Class_Lists is 
  new Charles.Lists.Double.Unbounded (T_Class_Access);

Insert (List, New_Item => new T'Class'(...), Before => I);

This implies that you have to manually deallocate the items prior to
destruction of the container object, which you can do easily enough by
passing an instantiation of Unchecked_Deallocation as the generic
actual to Generic_Modify_Element.

If you want to automate the finalization of container elements then
you can use an Access_Control object as the element of a limited list:

package T_Class_Lists is
   new Charles.Lists.Double.Limited_Unbounded (Pointer_Type);

where Pointer_Type is an instantiation of Charles.Access_Control. 
(That package is analogous to the auto_ptr in C++.)

You'd do something like:

Insert (List, Before => I, Iterator => J);

declare
   P : Pointer_Type renames To_Access (J).all;
begin
   Initialize (P, new T'Class'(...));
end;

So now when you delete the item:

Delete (List, Iterator => J);

then Free will be invoked automatically as a side-effect of
finalization of the Pointer_Type object.

One of the things that is lacking in the C++ STL is that you can't use
an auto_ptr as a container element.  The limited containers in Charles
were partly motivated by a desire to provide that ability.  The syntax
is admittedly heavy, but you can ease the pain as desired by creating
a thin layer (either over just the insertion operations as in the
example above, or over the entire container).

If there is interest then perhaps we can generalize your original idea
by creating another container form:

generic
  type Element_Type (<>) is limited private;
  type Handle_Type is private;
  with procedure Finalize (Handle : in out Handle_Type) is <>;
package Generic_Indefinite_Containers is

  type Container_Type is limited private;

  procedure Insert 
    (Container : in out Container_Type;
     New_Item  : in     Handle_Type);

  ...
end Generic_Indefinite_Containers;

Of course we could be more specific, by using an access type directly
as the handle:

generic
  type Element_Type (<>) is limited private;
  type Element_Access is access all Element_Type;
  with procedure Free (X : in out Element_Access) is <>;
package Generic_Indefinite_Containers is ...;

It would be trivial to implement either of these as a thin layer over
the existing components in Charles, which is largely why I haven't
bothered creating a separate component for indefinite types.



  reply	other threads:[~2003-07-01 14:21 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-17 19:53 DYNAMIC ADA TASK CREATION? avram
2003-06-17 20:24 ` Simon Wright
2003-06-18  6:11   ` Robert I. Eachus
2003-06-18  9:20     ` Georg Bauhaus
2003-06-18 11:13       ` John McCabe
2003-06-18 12:43         ` John McCabe
2003-06-18 14:17           ` Georg Bauhaus
2003-06-19  5:07         ` Simon Wright
2003-06-19  6:05           ` Robert I. Eachus
2003-06-19 23:30             ` Jeffrey Carter
2003-06-18 19:15     ` avram
2003-06-18 21:17       ` Craig Carey
2003-06-20 18:56         ` avram
2003-06-21 13:06           ` Pascal Obry
2003-06-18 23:28       ` Robert I. Eachus
2003-06-19  5:09         ` Simon Wright
2003-06-19  6:39           ` Robert I. Eachus
2003-06-19 20:34             ` Simon Wright
2003-06-28  0:54             ` Matthew Heaney
2003-06-28  7:25               ` Robert I. Eachus
2003-06-30  7:35                 ` Dmitry A. Kazakov
2003-06-30 15:01                   ` Matthew Heaney
2003-07-01  7:19                     ` Dmitry A. Kazakov
2003-07-01 14:21                       ` Matthew Heaney [this message]
2003-07-02 12:05                     ` Mário Amado Alves
2003-07-02 19:41                       ` Simon Wright
2003-07-03 21:31                         ` Mário Amado Alves
2003-07-03  9:53                   ` Robert I. Eachus
replies disabled

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