Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

set_index and and end_of_file with just a stream reference

63 views
Skip to first unread message

Mehdi Saada

unread,
Feb 20, 2021, 10:26:17 AM2/20/21
to
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 ?

Mehdi Saada

unread,
Feb 20, 2021, 10:35:01 AM2/20/21
to
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 ?

Simon Wright

unread,
Feb 20, 2021, 11:01:14 AM2/20/21
to
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).

Dmitry A. Kazakov

unread,
Feb 20, 2021, 11:04:54 AM2/20/21
to
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

Mehdi Saada

unread,
Feb 20, 2021, 11:22:15 AM2/20/21
to
"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.

Mehdi Saada

unread,
Feb 20, 2021, 11:30:25 AM2/20/21
to
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 ?

Dmitry A. Kazakov

unread,
Feb 20, 2021, 12:52:11 PM2/20/21
to
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.

Dmitry A. Kazakov

unread,
Feb 20, 2021, 12:59:22 PM2/20/21
to
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.

Mehdi Saada

unread,
Feb 20, 2021, 2:08:16 PM2/20/21
to
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.

Simon Wright

unread,
Feb 20, 2021, 4:41:09 PM2/20/21
to
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.

Dmitry A. Kazakov

unread,
Feb 20, 2021, 7:22:38 PM2/20/21
to
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.

Randy Brukardt

unread,
Feb 20, 2021, 8:56:38 PM2/20/21
to
"Dmitry A. Kazakov" <mai...@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.


Shark8

unread,
Feb 23, 2021, 12:21:12 PM2/23/21
to
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;
--...


J-P. Rosen

unread,
Feb 23, 2021, 12:56:50 PM2/23/21
to
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
0 new messages