comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Generic Embedded List Nodes
Date: Sun, 19 Jun 2016 21:04:03 +0200
Date: 2016-06-19T21:04:03+02:00	[thread overview]
Message-ID: <nk6qb1$8k4$2@gioia.aioe.org> (raw)
In-Reply-To: 15914c54-191c-4f37-b754-282855d1aeaf@googlegroups.com

On 2016-06-19 20:27, Warren wrote:
> On Sunday, 19 June 2016 00:45:54 UTC-4, Simon Wright  wrote:
>> Warren <ve3wwg@gmail.com> writes:
>>
>>    type Node_Type(Obj: access Object_Type) is new Emb_Node
>>      with null record;
>
> I've come to the conclusion that my original idea in Ada terms is too half baked.
>
> What it will come down to in the end is doing an offset calculation
> from the Node back to the Object when its access is required (through a
> function).
>
> Even in C/C++ this is a bit of a problem. While there is an
> offsetof(type,member) macro, this is only usable on non-dynamic types.
> Calculating an offset of a struct is trivial when you have it allocated.
> But when the class has a constructor, then you may not want to invoke
> that overhead only to create one for offset calculations.
>
> In Ada for some record Type R.M where R is the record and M is the
> member, you cannot do R.M'Position when R is just a type. But you can
> cheat this, similar to how I have done it in C/C++:
>
> with System;
> with Ada.Integer_Text_IO;
>
> procedure T is
>    use Ada.Integer_Text_IO;
>
>    type R_Type is
>       record
>          I : Integer;
>          F : Float;
>       end record;
>
>    A:    System.Address;
>
>    R:    R_Type;
>    for R'Address use A;
>
> begin
>    Put(R.I'Position);
>    Put(R.F'Position);
> end;
>
> This never allocates the object, but assumes it already exists at
> address A.. Since the record and its members are never actually used, it
> is possible to have the compiler do the necessary calculations. This
> will complain "warning: variable "A" is read but never assigned", however.
>
> I suppose a tagged R.Initialize could also establish an 'Access value
> in all Emb_Node members, using some other procedure call for Emb_Node
> (perhaps Set_Object). But it is desireable not to have to carry the
> object reference in the Emb_Node at all.

Maybe I don't understand the problem, but it seems you want externally 
linked objects that themselves were of any type. The best way to do it 
is IMO a custom memory pool.

My implementation of linked list uses this approach.

http://www.dmitry-kazakov.de/ada/components.htm#Generic_Doubly_Linked_Web

The object is allocated in the custom pool, which actually takes memory 
from another, e.g. standard, pool. But before that adds links just in 
front of the object. The pool-specific pointer carries the operations 
Next, Previous etc. Yes, it uses address arithmetic to get to the links, 
but no record member offsets or other crazy stuff because you deal with 
pointers to the objects, not the pointers to the list elements.

The only problem you must be aware of is that X'Address attribute is 
broken in Ada when X is an array:

    X'Address = X (X'First)'Address

which if of course not the address of the array. If you are going to 
instantiate your package with an array type, you must calculate the 
offset to the array object beginning.

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

  reply	other threads:[~2016-06-19 19:04 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-18 22:52 Generic Embedded List Nodes Warren
2016-06-18 23:40 ` Jeffrey R. Carter
2016-06-19  2:15   ` Warren
2016-06-19  3:04     ` Jeffrey R. Carter
2016-06-19  2:14 ` Jeremiah
2016-06-19  2:21   ` Warren
2016-06-19  2:50     ` Warren
2016-06-19  4:45       ` Simon Wright
2016-06-19 18:27         ` Warren
2016-06-19 19:04           ` Dmitry A. Kazakov [this message]
2016-06-19 20:13             ` Warren
2016-06-19 20:35               ` Dmitry A. Kazakov
2016-06-20  2:42                 ` Warren
2016-06-20  7:25                   ` Dmitry A. Kazakov
2016-06-20 12:26                     ` Warren
2016-06-20 19:33                       ` Niklas Holsti
2016-06-21  2:20                         ` Warren
2016-06-21  5:52                           ` Niklas Holsti
2016-06-21  7:15                             ` Dmitry A. Kazakov
2016-06-21 18:54                               ` Niklas Holsti
2016-06-21 19:54                                 ` Dmitry A. Kazakov
2016-06-21 10:31                             ` Warren
2016-06-21 17:13                               ` Jeffrey R. Carter
2016-06-21 18:56                                 ` Niklas Holsti
2016-06-21 20:13                                   ` Warren
2016-06-21 21:38                               ` Niklas Holsti
2016-06-23  2:12                                 ` Warren
2016-06-23  8:19                                   ` Niklas Holsti
2016-06-23 12:37                                     ` Warren
2016-06-23 15:36                                       ` Niklas Holsti
2016-06-24  1:55                                         ` Warren
2016-06-24 12:49                                         ` Warren
2016-06-25  5:50                                           ` Niklas Holsti
2016-06-26  1:36                                             ` Warren
2016-07-01 13:49                                             ` Warren
2016-07-01 16:28                                               ` Warren
2016-06-24 20:25                                         ` Warren
2016-06-22 13:01                               ` G.B.
2016-06-23  2:30                                 ` Warren
2016-06-20  6:08 ` Niklas Holsti
2016-06-20 12:20   ` Warren
2016-06-20 19:47 ` Shark8
2016-06-21  2:28   ` Warren
2016-06-21  7:21     ` Dmitry A. Kazakov
2016-06-21 10:32       ` Warren
2016-06-21 11:56         ` Dmitry A. Kazakov
2016-06-21 13:39           ` Warren
2016-06-21 14:04             ` Dmitry A. Kazakov
2016-06-23  0:37     ` Randy Brukardt
2016-06-23  2:25       ` Warren
2016-07-01 19:50 ` brbarkstrom
2016-07-02  1:55   ` Warren
replies disabled

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