From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=0.8 required=3.0 tests=BAYES_50,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.6 X-Received: by 2002:ad4:5fc5:0:b0:435:4420:d056 with SMTP id jq5-20020ad45fc5000000b004354420d056mr6473404qvb.130.1646976415454; Thu, 10 Mar 2022 21:26:55 -0800 (PST) X-Received: by 2002:a25:4945:0:b0:61d:546d:aedb with SMTP id w66-20020a254945000000b0061d546daedbmr6815547yba.147.1646976415121; Thu, 10 Mar 2022 21:26:55 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 10 Mar 2022 21:26:54 -0800 (PST) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=2601:193:4101:a3a0:3185:a3b1:bbd1:d53e; posting-account=1tLBmgoAAAAfy5sC3GUezzrpVNronPA- NNTP-Posting-Host: 2601:193:4101:a3a0:3185:a3b1:bbd1:d53e References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <03e4ee32-df3f-495d-861f-d580432c0713n@googlegroups.com> Subject: Re: Automatic Serialization of Dynamic Structures in Ada From: Matt Borchers Injection-Date: Fri, 11 Mar 2022 05:26:55 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:63602 List-Id: I'm not sure why Dmitry would say this is impossible unless you are actuall= y talking about creating a generic package to serialize objects. Objects t= o a generic routine would be a black box so you would have to supply the se= rialization 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, tre= e, 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 routi= nes 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 seria= lization: 'Output writes any necessary bounds of an array or other contain= er 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 val= ue itself - True if not null. Then I write any boundary information necess= ary 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 a= nd create a new instance of the expected type and then call 'Read to load d= ata 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 reco= nstruction is possible from the input stream. This method even works for tagged types and the class-wide streaming routin= es that will rebuild the proper objects because the class-wide streaming ro= utines stream the tag information about the object allowing dispatching on = 'Class'Input (if I remember correctly). Regards, Matt