comp.lang.ada
 help / color / mirror / Atom feed
From: William FRANCK <william.franck@free.fr>
Subject: Re: How to transfer Class-Wide object to a Task ?
Date: Thu, 17 Oct 2019 11:28:08 +0200
Date: 2019-10-17T11:27:51+02:00	[thread overview]
Message-ID: <5da83417$0$31419$426a34cc@news.free.fr> (raw)
In-Reply-To: 5da777d4$0$21611$426a34cc@news.free.fr

On 2019-10-16 20:04:41 +0000, William FRANCK said:
 I've uploaded my PoC source file on the comp.lang.ada group.

main_ClassWide_to_2Task.adb

"Transfering Class-Wide Objects to task-entries, with 
Ada.Containers.Indefinite_Holders."

-- ==== here it is as a copy =====
with Ada.Containers.Indefinite_Holders,
     Ada.Text_IO;
use
     Ada,
     Ada.Text_IO;

procedure main_ClassWide_to_2Task is
   -- ========================================================================
   -- Demo program showing how to transfer Class-Wide objects to task-entries,
   -- with Ada.Containers.Indefinite_Holders
   -- ========================================================================
   type Geo2D is tagged record
      Y, X : Integer;
      Name : String (1 .. 8) := "Object 0";
   end record;

   type Circle is new Geo2D with record
      Radius : Integer;
   end record;

   package Geo2D_Holders is new Ada.Containers.Indefinite_Holders 
(Geo2D'Class);

   myDataStore : Geo2D_Holders.Holder;

   -- ==========================================================
   package Object_Reading is

      task reading is
         entry Open;
         entry Object (DataStore : in out Geo2D_Holders.Holder);
         entry Stop;
      end reading;
   end Object_Reading;

   -- ==========================================================
   package Object_Writing is

      task writing is
         entry Create;
         entry Object (DataStore : in Geo2D_Holders.Holder);
         entry Stop;
      end writing;
   end Object_Writing;

   -- ==========================================================
   package body Object_Reading is

      task body reading is
         LocalDataStore : Geo2D_Holders.Holder;
         Nb_items       : Natural := 0;  -- for tracing
      begin
         accept Open do -- end go further only after open is complete
            Text_IO.Put_Line (Standard_Error,"Opening file ...");
         end Open;
         Text_IO.Put_Line (Standard_Error,"Input file ready !");
         loop
            select
               accept Object (DataStore : in out Geo2D_Holders.Holder) do
                  DataStore := Geo2D_Holders.Copy (Source => LocalDataStore);
               end Object;
               declare
                  -- (shortcut to simulate real case with Streams and 
*'Class'Input dispatching) ::
                  --Object : constant Geo2D'Class := Dispatching_Input 
(Ada.Tags.Internal_Tag (External_Tag), Stream);
                  O2D : Geo2D'Class := Circle'(0, 0, "Cercle 0", 10); 
-- simulate object creation done by 'Dispatching_Input'
               begin
                  NB_Items := NB_Items +1; -- for demo
                  O2D.Name(7..8) := Natural'Image(Nb_Items); -- for demo
                  Text_IO.Put_Line (Standard_Error,"Reading 2D Object 
#"& Natural'Image(Nb_Items)&": " & O2D.Name); -- debug trace
                  Geo2D_Holders.Replace_Element (Container => 
LocalDataStore, New_Item => O2D);
               end;
            or
               accept Stop do
                  Text_IO.Put_Line (Standard_Error,"Closing Input file...");
               end Stop;
               Text_IO.Put_Line (Standard_Error,"File cloded, Reading 
Stopped !");
               exit;
            end select;
         end loop;
      end reading;

   end Object_Reading;

   -- ==========================================================
   package body Object_Writing is

      task body writing is

         LocalDataStore : Geo2D_Holders.Holder;
         Nb_items  : Natural := 0;

      begin
         accept Create; -- and go ahead (with i.e. opening input file)
         Text_IO.Put_Line (Standard_Error,"Creating file ...");
         Text_IO.Put_Line (Standard_Error,"Output file ready !");
         loop
            select
               accept Object (DataStore : in Geo2D_Holders.Holder) do
                  LocalDataStore := Geo2D_Holders.copy (DataStore );
               end Object; -- go return to the main process, and go 
ahead with reading next record
               if not Geo2D_Holders.Is_Empty(Container => LocalDataStore)
               then
                  declare
                     O2D : Geo2D'Class := Geo2D_Holders.Element 
(Container => LocalDataStore);
                  begin
                     NB_Items := NB_Items +1; -- for demo
                     --  (shortcut to simulate real case with Streams 
and *'Class'Output dispatching) ::
                     Text_IO.Put_Line (Standard_Error,"   Writing 2D 
Object #"& Natural'Image(Nb_Items)&": " & O2D.Name); -- debug trace
                  end;
               end if;
            or
               accept Stop do
                  Text_IO.Put_Line (Standard_Error,"Closing Output file...");
               end Stop;
               Text_IO.Put_Line (Standard_Error,"File Closed, Writing 
Stopped ! ");
               exit;
            end select;
         end loop;
      end writing;
   end Object_Writing;

   -- ==========================================================
   use Object_Reading;
   use Object_Writing;

begin

   Writing.Create;
   Reading.Open;

   for i in 1 .. 9 loop -- While not end_of_file()
      Reading.Object (myDataStore);
      Writing.Object (myDataStore);
   end loop;

   Reading.Stop;
   Writing.Stop;

end main_ClassWide_to_2Task;


  parent reply	other threads:[~2019-10-17  9:28 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-14 19:41 How to transfer Class-Wide object to a Task ? William FRANCK
2019-10-14 19:55 ` Shark8
2019-10-14 20:48   ` William FRANCK
2019-10-14 22:01     ` Shark8
2019-10-15  5:13       ` William FRANCK
2019-10-14 19:58 ` Dmitry A. Kazakov
2019-10-14 20:58   ` William FRANCK
2019-10-15  4:40     ` Per Sandberg
2019-10-15  5:40       ` William FRANCK
2019-10-16 20:04       ` William FRANCK
2019-10-16 23:43         ` Anh Vo
2019-10-17  9:28         ` William FRANCK [this message]
2019-10-17 10:00           ` Dmitry A. Kazakov
2019-10-17 10:45             ` William FRANCK
2019-10-15  7:21     ` Dmitry A. Kazakov
2019-10-15 14:31       ` Optikos
2019-10-15 19:41         ` William FRANCK
2019-10-15 20:03           ` Shark8
2019-10-14 20:21 ` William FRANCK
2019-10-14 20:32   ` Dmitry A. Kazakov
2019-10-14 21:04     ` William FRANCK
2019-10-14 21:57   ` Shark8
2019-10-15  5:43     ` William FRANCK
replies disabled

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