How can I serialize and deserialize a private type?

Viewed 74

I'm working with the GNAT.OS_Lib module to implement a process manager for Linux. I'm using the subprograms in that module to spawn processes and get back PIDs, and now I want to save those PIDs to a file so they can be retrieved later to kill the process, get its status, etc. The problem is, the Process_Id type is private:

type Process_Id is private; -- s-os_lib.ads, line 743

The module provides a Pid_To_Integer function, but not the inverse. How can I serialize and deserialize Process_Ids?

1 Answers

Study section K.2 "Language Defined Attributes" in the Ada Language Reference Manual http://www.ada-auth.org/standards/12rm/html/RM-K-2.html

The attributes to study are S'Read, S'Write.

Your file must be created as a stream file. The Process_Id'Write attribute will serialize and write to the stream file. The Process_Id'Read attribute will read and de-serialize the data in the file.

If S'Read and S'Write do not work for you because of the nature of a compound type you should use the S'Input and S'Output attributes, which will read and write any bounds or discriminants.

S'Input and S'Output will work correctly with all types.

Related