comp.lang.ada
 help / color / Atom feed
* General circular buffer example not tied to any specific type
@ 2020-07-04 17:00 Daniel
  2020-07-04 17:25 ` Dmitry A. Kazakov
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Daniel @ 2020-07-04 17:00 UTC (permalink / raw)


Hello, any theoric 
 example of buffer i can find is always tied to an specific type.

I'm looking for any example of ravenscar buffer able for using any type of data at the same time.

I suppose it will need to serialize all  data and manipulate it as a group of bytes.

Does any body knows any example of this written in Ada?

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

* Re: General circular buffer example not tied to any specific type
  2020-07-04 17:00 General circular buffer example not tied to any specific type Daniel
@ 2020-07-04 17:25 ` Dmitry A. Kazakov
  2020-07-05  9:37   ` Simon Wright
  2020-09-03 10:31 ` riya patel
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2020-07-04 17:25 UTC (permalink / raw)


On 04/07/2020 19:00, Daniel wrote:

> I'm looking for any example of ravenscar buffer able for using any type of data at the same time.
> 
> I suppose it will need to serialize all  data and manipulate it as a group of bytes.
> 
> Does any body knows any example of this written in Ada?

Ring buffer of indefinite elements would be OK. As an element you can 
use this:

    type Item (Size : Stream_Element_Count) is record
        Data : Stream_Element_Array (1..Size);
    end record;

Instantiate the generic buffer with this type. Use stream attributes to 
serialize/deserialize.

Alternatively you can do it with Storage_Element in the above and use a 
fake storage pool to store/restore objects. Or a combination "for 
X'Address use Y" with pragma Import (Ada, X);

If the type set is somewhat statically known you can use a variant 
record as an element too.

In some cases you can have a ring buffer of type tags and a set of ring 
buffers. For each type tag you would keep values in a separate ring buffer.

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

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

* Re: General circular buffer example not tied to any specific type
  2020-07-04 17:25 ` Dmitry A. Kazakov
@ 2020-07-05  9:37   ` Simon Wright
  2020-07-05  9:52     ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: Simon Wright @ 2020-07-05  9:37 UTC (permalink / raw)


"Dmitry A. Kazakov" <[email protected]> writes:

> Ring buffer of indefinite elements would be OK. As an element you can
> use this:
>
>    type Item (Size : Stream_Element_Count) is record
>        Data : Stream_Element_Array (1..Size);
>    end record;

For the Size, see ARM 13.13.2(2).

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05  9:37   ` Simon Wright
@ 2020-07-05  9:52     ` Simon Wright
  2020-07-05 13:25       ` Jeffrey R. Carter
  0 siblings, 1 reply; 16+ messages in thread
From: Simon Wright @ 2020-07-05  9:52 UTC (permalink / raw)


Simon Wright <[email protected]> writes:

> For the Size, see ARM 13.13.2(2).

13.13.2(1.2), sorry

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05  9:52     ` Simon Wright
@ 2020-07-05 13:25       ` Jeffrey R. Carter
  2020-07-05 15:36         ` Simon Wright
  2020-07-06  8:11         ` Simon Wright
  0 siblings, 2 replies; 16+ messages in thread
From: Jeffrey R. Carter @ 2020-07-05 13:25 UTC (permalink / raw)


On 7/5/20 11:52 AM, Simon Wright wrote:
> Simon Wright <[email protected]> writes:
> 
>> For the Size, see ARM 13.13.2(2).
> 
> 13.13.2(1.2), sorry

That only works for elementary types. For any type, a better approach is generic 
package Ada.Storage_IO, ARM A.9 
(http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html).

-- 
Jeff Carter
"C++: The power, elegance and simplicity of a hand grenade."
Ole-Hjalmar Kristensen
90

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05 13:25       ` Jeffrey R. Carter
@ 2020-07-05 15:36         ` Simon Wright
  2020-07-05 18:46           ` Dmitry A. Kazakov
  2020-07-06  8:11         ` Simon Wright
  1 sibling, 1 reply; 16+ messages in thread
From: Simon Wright @ 2020-07-05 15:36 UTC (permalink / raw)


"Jeffrey R. Carter" <[email protected]> writes:

> On 7/5/20 11:52 AM, Simon Wright wrote:
>> Simon Wright <[email protected]> writes:
>>
>>> For the Size, see ARM 13.13.2(2).
>>
>> 13.13.2(1.2), sorry
>
> That only works for elementary types. For any type, a better approach
> is generic package Ada.Storage_IO, ARM A.9
> (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html).

As you say, this has the advantage of actually working.

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05 15:36         ` Simon Wright
@ 2020-07-05 18:46           ` Dmitry A. Kazakov
  2020-07-05 19:51             ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2020-07-05 18:46 UTC (permalink / raw)


On 05/07/2020 17:36, Simon Wright wrote:
> "Jeffrey R. Carter" <[email protected]> writes:
> 
>> On 7/5/20 11:52 AM, Simon Wright wrote:
>>> Simon Wright <[email protected]> writes:
>>>
>>>> For the Size, see ARM 13.13.2(2).
>>>
>>> 13.13.2(1.2), sorry
>>
>> That only works for elementary types. For any type, a better approach
>> is generic package Ada.Storage_IO, ARM A.9
>> (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html).
> 
> As you say, this has the advantage of actually working.

I see no difference. In both cases some kind of type tag need to be 
written and the object's size with it if not derived from the tag.

Whether stream Read/Write or storage elements copy is then used changes 
little.

P.S. I never used Storage_IO, I did storage pools instead.

With a ring buffer you go as follows. Make a LIFO of Storage_Elements. 
It could be a descendant of Root_Storage_Pool or else if true parent 
type is already spent (Ada has no full multiple inheritance) use an 
access discriminant in a mix-in:

    type LIFO_Pool (Buffer : not null access LIFO'Class)
       with null record;

The implementation of Allocate with reserve place in Buffer. Allocate 
already has parameter Size_In_Storage_Elements.

You should only pay attention to Alignment and to atomic kicking off old 
elements when new allocation overlaps old elements in the buffer. So the 
element sizes must be stored in the LIFO. Writing into the LIFO will become:

    procedure Push (Buffer : in out LIFO'Class S : String) is
       Pool : LIFO_Pool (Buffer'Unchecked_Access);
       type String_Ptr is access String;
       for String_Ptr'Storage_Pool use Pool;
       Ptr : String_Ptr;
    begin
       Ptr := new String'("Some text");
    end Push;

P.P.S. Beware Ada finalization design bug ARM 7.6.1 (11.1/3): no 
controlled types, also.

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

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05 18:46           ` Dmitry A. Kazakov
@ 2020-07-05 19:51             ` Simon Wright
  0 siblings, 0 replies; 16+ messages in thread
From: Simon Wright @ 2020-07-05 19:51 UTC (permalink / raw)


"Dmitry A. Kazakov" <[email protected]> writes:

> On 05/07/2020 17:36, Simon Wright wrote:
>> "Jeffrey R. Carter" <[email protected]> writes:
>>
>>> On 7/5/20 11:52 AM, Simon Wright wrote:
>>>> Simon Wright <[email protected]> writes:
>>>>
>>>>> For the Size, see ARM 13.13.2(2).
>>>>
>>>> 13.13.2(1.2), sorry
>>>
>>> That only works for elementary types. For any type, a better approach
>>> is generic package Ada.Storage_IO, ARM A.9
>>> (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html).
>>
>> As you say, this has the advantage of actually working.
>
> I see no difference. In both cases some kind of type tag need to be
> written and the object's size with it if not derived from the tag.

I was talking about the fact that 'Storage_Size only works for elementary
types.

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

* Re: General circular buffer example not tied to any specific type
  2020-07-05 13:25       ` Jeffrey R. Carter
  2020-07-05 15:36         ` Simon Wright
@ 2020-07-06  8:11         ` Simon Wright
  2020-07-06  8:58           ` Jeffrey R. Carter
  1 sibling, 1 reply; 16+ messages in thread
From: Simon Wright @ 2020-07-06  8:11 UTC (permalink / raw)


"Jeffrey R. Carter" <[email protected]> writes:

> For any type, a better approach is generic package Ada.Storage_IO, ARM
> A.9 (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-9.html).

Any *definite* type. I don't remember whether that would suit the
original requirement in this thread.

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

* Re: General circular buffer example not tied to any specific type
  2020-07-06  8:11         ` Simon Wright
@ 2020-07-06  8:58           ` Jeffrey R. Carter
  2020-07-06 10:34             ` Simon Wright
  0 siblings, 1 reply; 16+ messages in thread
From: Jeffrey R. Carter @ 2020-07-06  8:58 UTC (permalink / raw)


On 7/6/20 10:11 AM, Simon Wright wrote:
> 
> Any *definite* type. I don't remember whether that would suit the
> original requirement in this thread.

I don't think "requirement" applies. "Circular buffer of untyped values" is at 
best a design decision. Trying to bypass the type system is usually an 
indication of a design problem, in my experience.

-- 
Jeff Carter
"In the frozen land of Nador they were forced to
eat Robin's minstrels, and there was much rejoicing."
Monty Python & the Holy Grail
70

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

* Re: General circular buffer example not tied to any specific type
  2020-07-06  8:58           ` Jeffrey R. Carter
@ 2020-07-06 10:34             ` Simon Wright
  2020-07-06 11:07               ` Dmitry A. Kazakov
  0 siblings, 1 reply; 16+ messages in thread
From: Simon Wright @ 2020-07-06 10:34 UTC (permalink / raw)


"Jeffrey R. Carter" <[email protected]> writes:

> On 7/6/20 10:11 AM, Simon Wright wrote:
>>
>> Any *definite* type. I don't remember whether that would suit the
>> original requirement in this thread.
>
> I don't think "requirement" applies. "Circular buffer of untyped
> values" is at best a design decision. Trying to bypass the type system
> is usually an indication of a design problem, in my experience.

Yes. There would have to be some mechanism for the receiving end to
decide what to do with the next lot of bytes!

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

* Re: General circular buffer example not tied to any specific type
  2020-07-06 10:34             ` Simon Wright
@ 2020-07-06 11:07               ` Dmitry A. Kazakov
  0 siblings, 0 replies; 16+ messages in thread
From: Dmitry A. Kazakov @ 2020-07-06 11:07 UTC (permalink / raw)


On 06/07/2020 12:34, Simon Wright wrote:
> "Jeffrey R. Carter" <[email protected]> writes:
> 
>> On 7/6/20 10:11 AM, Simon Wright wrote:
>>>
>>> Any *definite* type. I don't remember whether that would suit the
>>> original requirement in this thread.
>>
>> I don't think "requirement" applies. "Circular buffer of untyped
>> values" is at best a design decision. Trying to bypass the type system
>> is usually an indication of a design problem, in my experience.
> 
> Yes. There would have to be some mechanism for the receiving end to
> decide what to do with the next lot of bytes!

Sure. An example would be implementation of interprocess RPC. The 
parameters of a RPC would be passed via some pipe implemented as a ring 
buffer with stream interface on top of it. Since RPC parameters could be 
of almost any type, here you go.

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

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

* Re: General circular buffer example not tied to any specific type
  2020-07-04 17:00 General circular buffer example not tied to any specific type Daniel
  2020-07-04 17:25 ` Dmitry A. Kazakov
@ 2020-09-03 10:31 ` riya patel
  2020-09-24  4:10 ` nimaopatel121
  2020-09-25 14:32 ` Simon Wright
  3 siblings, 0 replies; 16+ messages in thread
From: riya patel @ 2020-09-03 10:31 UTC (permalink / raw)


On Saturday, 4 July 2020 22:30:28 UTC+5:30, Daniel  wrote:
> Hello, any theoric 
>  example of buffer i can find is always tied to an specific type.
> 
> I'm looking for any example of ravenscar buffer able for using any type of data at the same time.
> 
> I suppose it will need to serialize all  data and manipulate it as a group of bytes.
> 
> Does any body knows any example of this written in Ada?

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

* Re: General circular buffer example not tied to any specific type
  2020-07-04 17:00 General circular buffer example not tied to any specific type Daniel
  2020-07-04 17:25 ` Dmitry A. Kazakov
  2020-09-03 10:31 ` riya patel
@ 2020-09-24  4:10 ` nimaopatel121
  2020-09-24  4:39   ` J-P. Rosen
  2020-09-25 14:32 ` Simon Wright
  3 siblings, 1 reply; 16+ messages in thread
From: nimaopatel121 @ 2020-09-24  4:10 UTC (permalink / raw)


On Saturday, 4 July 2020 22:30:28 UTC+5:30, Daniel  wrote:
> Hello, any theoric 
>  example of buffer i can find is always tied to an specific type.
> 
> I'm looking for any example of ravenscar buffer able for using any type of data at the same time.
> 
> I suppose it will need to serialize all  data and manipulate it as a group of bytes.
> 
> Does any body knows any example of this written in Ada?

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

* Re: General circular buffer example not tied to any specific type
  2020-09-24  4:10 ` nimaopatel121
@ 2020-09-24  4:39   ` J-P. Rosen
  0 siblings, 0 replies; 16+ messages in thread
From: J-P. Rosen @ 2020-09-24  4:39 UTC (permalink / raw)


Le 24/09/2020 à 06:10, [email protected] a écrit :
> On Saturday, 4 July 2020 22:30:28 UTC+5:30, Daniel  wrote:
>> Hello, any theoric 
>>  example of buffer i can find is always tied to an specific type.
>>
>> I'm looking for any example of ravenscar buffer able for using any type of data at the same time.
>>
>> I suppose it will need to serialize all  data and manipulate it as a group of bytes.
>>
>> Does any body knows any example of this written in Ada?
> 
Hmmm, you know, Ada is a strongly typed language, therefore what you put
in a buffer must have a well defined type.

There are two possibilities:
1) If you can accept several buffers, one for each type, make it generic
and instantiate it as many times as you need

2) Make a buffer of Stream_Elements, and use the streaming attributes
('Read, 'Write) to turn any type into stream elements.

Ada.Streams.Stream_IO can also be handy in some cases.

-- 
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] 16+ messages in thread

* Re: General circular buffer example not tied to any specific type
  2020-07-04 17:00 General circular buffer example not tied to any specific type Daniel
                   ` (2 preceding siblings ...)
  2020-09-24  4:10 ` nimaopatel121
@ 2020-09-25 14:32 ` Simon Wright
  3 siblings, 0 replies; 16+ messages in thread
From: Simon Wright @ 2020-09-25 14:32 UTC (permalink / raw)


Daniel <[email protected]> writes:

> Hello, any theoric 
>  example of buffer i can find is always tied to an specific type.
>
> I'm looking for any example of ravenscar buffer able for using any
> type of data at the same time.
>
> I suppose it will need to serialize all data and manipulate it as a
> group of bytes.

As J-P has said, you could use 'Write and 'Read (or better, 'Output and
'Input) to write to a stream.

The beginnings of an alternative, which I last worked on a while ago, is
at [1]; it's an Ada implementation of part of MessagePack[2] (boolean,
integer, float, string). Still a way to go!

Writing arbitrary data to a stream using 'Write/'Output suffers from the
disadvantage that the reading side won't know what to expect unless you
have some protocol in place. This Message_Pack doesn't eliminate this at
all.

For a while, I supported a scheme where all the data to be transmitted
had to be instances of a tagged type e.g. Base; as far as I can
remember, you output the data using Base'Class'Output and read it in
using Base'Class'Input.

[1]
https://sourceforge.net/u/simonjwright/msgpack-ada/code/ci/master/tree/
[2] https://en.wikipedia.org/wiki/MessagePack

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

end of thread, other threads:[~2020-09-25 14:32 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-04 17:00 General circular buffer example not tied to any specific type Daniel
2020-07-04 17:25 ` Dmitry A. Kazakov
2020-07-05  9:37   ` Simon Wright
2020-07-05  9:52     ` Simon Wright
2020-07-05 13:25       ` Jeffrey R. Carter
2020-07-05 15:36         ` Simon Wright
2020-07-05 18:46           ` Dmitry A. Kazakov
2020-07-05 19:51             ` Simon Wright
2020-07-06  8:11         ` Simon Wright
2020-07-06  8:58           ` Jeffrey R. Carter
2020-07-06 10:34             ` Simon Wright
2020-07-06 11:07               ` Dmitry A. Kazakov
2020-09-03 10:31 ` riya patel
2020-09-24  4:10 ` nimaopatel121
2020-09-24  4:39   ` J-P. Rosen
2020-09-25 14:32 ` Simon Wright

comp.lang.ada

Archives are clonable: git clone --mirror https://archive.legitdata.co/comp.lang.ada

Example config snippet for mirrors


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git