comp.lang.ada
 help / color / mirror / Atom feed
* set_index and and end_of_file with just a stream reference
@ 2021-02-20 15:26 Mehdi Saada
  2021-02-20 15:35 ` Mehdi Saada
  2021-02-20 16:04 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 14+ messages in thread
From: Mehdi Saada @ 2021-02-20 15:26 UTC (permalink / raw)


In term of design, I have a file inside a protected type, to ensure validity for concurrrent readings/writings.
An entry guarded by "Is_Open" provide with an "out" stream_access, a means to read/write from the stream file.
But in the procedure I wish to also use End_Of_File and Set_Index, which require a File_Type. Which cannot be passed as "out" parameter nor through an access type, since stream_access is not an access type.
Unguarded protected functions would go against the design.
Should have I have the protected object provides both in the form of entries, the first with something like Is_End_Of_File: boolean := object.End_Of_File ?

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 15:26 set_index and and end_of_file with just a stream reference Mehdi Saada
@ 2021-02-20 15:35 ` Mehdi Saada
  2021-02-20 16:01   ` Simon Wright
  2021-02-20 16:04 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 14+ messages in thread
From: Mehdi Saada @ 2021-02-20 15:35 UTC (permalink / raw)


By the way, what was the rational behind this  entry format ?
Why don't we just have protected procedure ... when ... is and protected function ... when ... is ?

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 15:35 ` Mehdi Saada
@ 2021-02-20 16:01   ` Simon Wright
  0 siblings, 0 replies; 14+ messages in thread
From: Simon Wright @ 2021-02-20 16:01 UTC (permalink / raw)


Mehdi Saada <00120260a@gmail.com> writes:

> By the way, what was the rational behind this  entry format ?
> Why don't we just have protected procedure ... when ... is and
> protected function ... when ... is ?

With protected subprograms, the caller only has to wait until no one
else is using the PO.

With entries, the caller has also to wait until the entry condition is
met (subprograms can continue to be called, from other tasks of course).

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 15:26 set_index and and end_of_file with just a stream reference Mehdi Saada
  2021-02-20 15:35 ` Mehdi Saada
@ 2021-02-20 16:04 ` Dmitry A. Kazakov
  2021-02-20 16:22   ` Mehdi Saada
  2021-02-21  1:56   ` Randy Brukardt
  1 sibling, 2 replies; 14+ messages in thread
From: Dmitry A. Kazakov @ 2021-02-20 16:04 UTC (permalink / raw)


On 2021-02-20 16:26, Mehdi Saada wrote:
> In term of design, I have a file inside a protected type, to ensure validity for concurrrent readings/writings.

This is illegal ARM 9.5.1 (8)

Use a mutex (based on a protected object) or a monitor (based on a task) 
for mutually exclusion of blocking operations.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 16:04 ` Dmitry A. Kazakov
@ 2021-02-20 16:22   ` Mehdi Saada
  2021-02-20 16:30     ` Mehdi Saada
  2021-02-20 17:52     ` Dmitry A. Kazakov
  2021-02-21  1:56   ` Randy Brukardt
  1 sibling, 2 replies; 14+ messages in thread
From: Mehdi Saada @ 2021-02-20 16:22 UTC (permalink / raw)


"With protected subprograms, the caller only has to wait until no one
else is using the PO. "
Still, wouldn't an entry with no guard condition have the same semantic/effect ?

Thanks. Maybe an ebook on how to do with files/streams, not just the grammar but the semantics intended to go along with it, and strategies, would be great. Unless it already exists.

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 16:22   ` Mehdi Saada
@ 2021-02-20 16:30     ` Mehdi Saada
  2021-02-20 17:59       ` Dmitry A. Kazakov
  2021-02-20 17:52     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 14+ messages in thread
From: Mehdi Saada @ 2021-02-20 16:30 UTC (permalink / raw)


Le samedi 20 février 2021 à 17:22:15 UTC+1, Mehdi Saada a écrit :
> Still, wouldn't an entry with no guard condition have the same semantic/effect ? 
May I answer my own question ?
-> "the semantics is very different and non-intuitive with task calling on entries or proctected procedures, but I just don't know about it much now."
Ok ok ^_^'

Now talking about streams, since there is no stream_size for composite types, how do I get the equivalent data for the default implementation of input/output attributes for composite types ?
Must I compute it with offset size or whatnot ?

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 16:22   ` Mehdi Saada
  2021-02-20 16:30     ` Mehdi Saada
@ 2021-02-20 17:52     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 14+ messages in thread
From: Dmitry A. Kazakov @ 2021-02-20 17:52 UTC (permalink / raw)


On 2021-02-20 17:22, Mehdi Saada wrote:
> "With protected subprograms, the caller only has to wait until no one
> else is using the PO. "
> Still, wouldn't an entry with no guard condition have the same semantic/effect ?

With the guard "when True", yes.

Regardless, it is still illegal to do I/O in from a PO.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 16:30     ` Mehdi Saada
@ 2021-02-20 17:59       ` Dmitry A. Kazakov
  2021-02-20 19:08         ` Mehdi Saada
  0 siblings, 1 reply; 14+ messages in thread
From: Dmitry A. Kazakov @ 2021-02-20 17:59 UTC (permalink / raw)


On 2021-02-20 17:30, Mehdi Saada wrote:

> Now talking about streams, since there is no stream_size for composite types, how do I get the equivalent data for the default implementation of input/output attributes for composite types ?

You don't, it is useless.

> Must I compute it with offset size or whatnot ?

You must not.

As a general advice, instead of asking for fixing wrong solutions, 
first, try to state the problem at hand. Counting sizes and offsets of 
external representations is not a problem, it is a [wrong] solution of 
some other problem.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 17:59       ` Dmitry A. Kazakov
@ 2021-02-20 19:08         ` Mehdi Saada
  2021-02-20 21:41           ` Simon Wright
                             ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Mehdi Saada @ 2021-02-20 19:08 UTC (permalink / raw)


Okay :-)
what I wanted is:
I read an acronyme in the stream file, if good I input the adjacent record type, otherwise I would advance on the stream until the next acronyme with set_index(stream_access, index(stream_access) + composite_type_stream_size) and read the next acronyme (unbounded_string).
Now I just input both objects and verify the acronyme.
But I don't like writing an object that maybe won't be used.

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 19:08         ` Mehdi Saada
@ 2021-02-20 21:41           ` Simon Wright
  2021-02-21  0:22           ` Dmitry A. Kazakov
                             ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Simon Wright @ 2021-02-20 21:41 UTC (permalink / raw)


Mehdi Saada <00120260a@gmail.com> writes:

> Okay :-)
> what I wanted is:
> I read an acronyme in the stream file, if good I input the adjacent
> record type, otherwise I would advance on the stream until the next
> acronyme with set_index(stream_access, index(stream_access) +
> composite_type_stream_size) and read the next acronyme
> (unbounded_string).
> Now I just input both objects and verify the acronyme.
> But I don't like writing an object that maybe won't be used.

Unless your objects are megabytes in size, I'd strongly suggest you just
read the thing in anyway. It'd stand a chance of working, then you can
benchmark and see whether it needs improvement.

I thought you wanted to manage concurrent access to this stream? in
which case you're going to need to read the tag and the object together,
even if it turns out you don't need the object after all.

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 19:08         ` Mehdi Saada
  2021-02-20 21:41           ` Simon Wright
@ 2021-02-21  0:22           ` Dmitry A. Kazakov
  2021-02-23 17:21           ` Shark8
  2021-02-23 17:56           ` J-P. Rosen
  3 siblings, 0 replies; 14+ messages in thread
From: Dmitry A. Kazakov @ 2021-02-21  0:22 UTC (permalink / raw)


On 2021-02-20 20:08, Mehdi Saada wrote:
> Okay :-)
> what I wanted is:
> I read an acronyme in the stream file, if good I input the adjacent record type, otherwise I would advance on the stream until the next acronyme with set_index(stream_access, index(stream_access) + composite_type_stream_size) and read the next acronyme (unbounded_string).
> Now I just input both objects and verify the acronyme.
> But I don't like writing an object that maybe won't be used.

So, the problem is "compartmentalization" of a stream.

[As Simon said you better read/write everything as is]

If you really want to implement it, one technique is chained "virtual" 
stream. You create a stream type like this:

    type Compartment_Stream
         (  Source : not null access Root_Stream_Type'Class
         )  is new Root_Stream_Type with ...

The Read/Write operations call to Read/Write of Source, but add 
delimiters separating "compartments." E.g. the stream elements 0..254 
are encoded as is. The element 255 is encoded as a pair (255,255). The 
pairs (255,*) are treated as an "end of a compartment" and ignored. This 
way you can add a Skip operation to Compartment_Stream that reads 
everything until first (255,*). You can even create a nested structure 
of compartments using this schema. E.g. encode start of a compartment as 
(255,0) and its end as (255,1) etc.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 16:04 ` Dmitry A. Kazakov
  2021-02-20 16:22   ` Mehdi Saada
@ 2021-02-21  1:56   ` Randy Brukardt
  1 sibling, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2021-02-21  1:56 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:s0rbv0$dss$1@gioia.aioe.org...
> On 2021-02-20 16:26, Mehdi Saada wrote:
>> In term of design, I have a file inside a protected type, to ensure 
>> validity for concurrrent readings/writings.
>
> This is illegal ARM 9.5.1 (8)

Formally, it is legal, but it isn't required to work - it might raise an 
exception if you do it. It can be made illegal in Ada 202x by setting 
Nonblocking => True on the protected type (that would have been better as 
the default from a programming perspective, but it would break a lot of 
existing code).

> Use a mutex (based on a protected object) or a monitor (based on a task) 
> for mutually exclusion of blocking operations.

Right.

                         Randy. 


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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 19:08         ` Mehdi Saada
  2021-02-20 21:41           ` Simon Wright
  2021-02-21  0:22           ` Dmitry A. Kazakov
@ 2021-02-23 17:21           ` Shark8
  2021-02-23 17:56           ` J-P. Rosen
  3 siblings, 0 replies; 14+ messages in thread
From: Shark8 @ 2021-02-23 17:21 UTC (permalink / raw)


On Saturday, February 20, 2021 at 12:08:16 PM UTC-7, 0012com wrote:
> Okay :-) 
> what I wanted is: 
> I read an acronyme in the stream file, if good I input the adjacent record type, otherwise I would advance on the stream until the next acronyme with set_index(stream_access, index(stream_access) + composite_type_stream_size) and read the next acronyme (unbounded_string). 
> Now I just input both objects and verify the acronyme. 
> But I don't like writing an object that maybe won't be used.
Hm, what are your datatypes? Is this ONLY text, or are you able to impose your own structure?
You could have something like this:

-- Instantiated Container Packages.
Package String_Holder is new Ada.Containers.Indefinite_Holders(
       Element_Type => String,
       "="          => Ada.Strings.Equal_Case_Insensitive
      );
Package String_Map is new Ada.Containers.Indefinite_Ordered_Maps(
       "<"          => Ada.Strings.Less_Case_Insensitive,
       "="          => Ada.Strings.Equal_Case_Insensitive,
       Key_Type     => String,
       Element_Type => String
      );

-- The heart of the operating program.
With String_Holder, String_Map;
Package Acronyms is
  -- Because this is it's own type, you can put other things in the record, like a link to the place that it's defined, if needed.
  Type Initialism is new String_Holder.Holder with null record;

  Function Expand( Acronym : Initialism ) return String;
  Procedure Register( Acronym, Expansion : String );
--...
End Acronyms;

Package Body Acronyms is
    Acronym_Map : String_Map.Map;
  Procedure Register( Acronym, Expansion : String ) is
  Begin
    Acronym_Map.Insert( New_Item => Expansion, Key => Acronym );
  End Register;  

  Function Expand( Acronym : Initialism ) return String is
  Begin
    Return Acronym_Map( Acronym );
  Exception
    when others => Return Acronym; -- I forget if it's CONSTRAINT_ERROR or ASSERT_ERROR when the element is not present.
  End Expand;
End Acronyms;

-- in your main Acronym-Stream package...
-- Text_Soup is a variant record for handling portions of an acronym-expanding string; your main data-structure would probably be an Indefinite_Vector of Text_Soup'Class,
-- you might not need Input or output, depending on your usage, but for automatically expanding the initialism you'd need to use Acronyms.Expand.

    Type Text_Soup(<>) is tagged private;
    procedure Output(
       Stream : not null access Ada.Streams.Root_Stream_Type'Class;
       Item   : in Text_Soup'Class);
    function Input(
       Stream : not null access Ada.Streams.Root_Stream_Type'Class)
       return Text_Soup'Class;

   -- Other public operations.
PRIVATE
    For Text_Soup'Class'Input  use Input;
    For Text_Soup'Class'Output use Output;

Type Text_Soup(Length : Natural) is record
  case Length is
    when 0 => Acronym : Initialism;
    when others => Text : String(1..Length);
  end case;
end record;
--...


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

* Re: set_index and and end_of_file with just a stream reference
  2021-02-20 19:08         ` Mehdi Saada
                             ` (2 preceding siblings ...)
  2021-02-23 17:21           ` Shark8
@ 2021-02-23 17:56           ` J-P. Rosen
  3 siblings, 0 replies; 14+ messages in thread
From: J-P. Rosen @ 2021-02-23 17:56 UTC (permalink / raw)


Le 20/02/2021 à 20:08, Mehdi Saada a écrit :
> Okay :-)
> what I wanted is:
> I read an acronyme in the stream file, if good I input the adjacent record type, otherwise I would advance on the stream until the next acronyme with set_index(stream_access, index(stream_access) + composite_type_stream_size) and read the next acronyme (unbounded_string).
> Now I just input both objects and verify the acronyme.
> But I don't like writing an object that maybe won't be used.
> 
Assuming:
1) the file is a text file
2) it is created with an Ada program

There is a simple solution:
Separate your entries (Acronym+Data) with a call to New_Page

When you read an acronym that you don't want, call Skip_Page; this will 
put you in front of the next entry.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

end of thread, other threads:[~2021-02-23 17:56 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-20 15:26 set_index and and end_of_file with just a stream reference Mehdi Saada
2021-02-20 15:35 ` Mehdi Saada
2021-02-20 16:01   ` Simon Wright
2021-02-20 16:04 ` Dmitry A. Kazakov
2021-02-20 16:22   ` Mehdi Saada
2021-02-20 16:30     ` Mehdi Saada
2021-02-20 17:59       ` Dmitry A. Kazakov
2021-02-20 19:08         ` Mehdi Saada
2021-02-20 21:41           ` Simon Wright
2021-02-21  0:22           ` Dmitry A. Kazakov
2021-02-23 17:21           ` Shark8
2021-02-23 17:56           ` J-P. Rosen
2021-02-20 17:52     ` Dmitry A. Kazakov
2021-02-21  1:56   ` Randy Brukardt

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