comp.lang.ada
 help / color / mirror / Atom feed
From: Matt Borchers <mattborchers@gmail.com>
Subject: Re: Automatic Serialization of Dynamic Structures in Ada
Date: Thu, 10 Mar 2022 21:26:54 -0800 (PST)	[thread overview]
Message-ID: <03e4ee32-df3f-495d-861f-d580432c0713n@googlegroups.com> (raw)
In-Reply-To: <svu283$1o7p$1@gioia.aioe.org>

I'm not sure why Dmitry would say this is impossible unless you are actually talking about creating a generic package to serialize objects.  Objects to a generic routine would be a black box so you would have to supply the serialization routines to the generic at instantiation.  Otherwise, there are certainly cases that may prove impossible or challenging, but I think the OP is thinking of perhaps serializing simpler cases like a linked list, tree, or graph in memory.  In this case, it is definitely possible even if the objects in the structure contain other dynamic elements.

If you are serializing a structure where access type links are followed to do a simple traversal of the structure then you don't need distinct IDs for the objects that you are serializing.  Otherwise, you probably do in order to fully regenerate the dynamic structure.

My approach to this is to create a 'Read, 'Write, 'Input, and 'Output routines for both the access type and the type being accessed.  I do this mostly for completeness, but it's not necessary if the program doesn't need both.  You many not need a 'Read and 'Write, but if you need one then use 'Input and 'Output.  There is a difference that should be obeyed for proper serialization:  'Output writes any necessary bounds of an array or other container type before writing the data and 'Input expects that bounds information to be available before reading the data.  So, I generally have 'Output call 'Write and 'Input call 'Read to avoid duplication of code.

The 'Output on the access type writes a Boolean value before the access value itself - True if not null.  Then I write any boundary information necessary then invoke the 'Write on the .all of the access type.  For 'Input, it would obviously read the Boolean value and if True read the boundary data and create a new instance of the expected type and then call 'Read to load data into the newly created .all object.

For a linked list, tree, and even a complete graph a simple traversal works fine.  For an incomplete graph, you may want to write out the nodes first and then write out coded information for how the nodes are linked so a reconstruction is possible from the input stream.

This method even works for tagged types and the class-wide streaming routines that will rebuild the proper objects because the class-wide streaming routines stream the tag information about the object allowing dispatching on 'Class'Input (if I remember correctly).

Regards,
Matt

      reply	other threads:[~2022-03-11  5:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 20:26 Automatic Serialization of Dynamic Structures in Ada Manuel Gomez
2022-03-04 22:01 ` Dmitry A. Kazakov
2022-03-11  5:26   ` Matt Borchers [this message]
replies disabled

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