From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6eac62e4f2badf3a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-08 06:58:57 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: Persistence of limited tagged types Date: 8 Apr 2003 06:58:56 -0700 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0304080558.2df9c8c8@posting.google.com> References: <1049742443.855336@master.nyc.kbcfp.com> NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1049810337 24141 127.0.0.1 (8 Apr 2003 13:58:57 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 8 Apr 2003 13:58:57 GMT Xref: archiver1.google.com comp.lang.ada:35992 Date: 2003-04-08T13:58:57+00:00 List-Id: Jano wrote in message news:... > Hyman Rosen dice... > > > > This is known as the Factory pattern. Instead of changing the > > function for each new tag, you can have the package that defines > > the type enroll a read method into a table of methods. Then the > > factory reads the external tag representation, looks it up in > > the table, and calls the method if it's present. > > I like this solution. With very little overwork. This will be the subject of the Dear Ada column in next issue of Ada Letters. Basically, each type will register a factory function like this: function New_T return T'Class; Implemented something like this (say, for NT, which derives from T): function New_T return T'Class is begin return NT'(T with I, J, K); end; Actually, your type is limited, so it would be something like: function New_T return T_Class_Access is O : constant NT_Access := new NT; begin --init O return T_Class_Access (O); end; You have a map whose keys are type string, and whose element is a pointer to the factory function: type Factory_Type is access function return T_Class_Access; package Table_Types is new Charles.Maps.Sorted.Strings.Unbounded (Factory_Type); Table : Table_Types.Container_Type; Now a type registers its factory function: procedure Register (Key : Ada.Tags.Tag; Factory : Factory_Type) is begin Insert (Table, External_Tag (Key), Factory); end; Now, given a tag, you can look up the factory function, and create a new object, e.g. function New_T (Key : Ada.Tags.Tag) return T_Class_Access is I : constant Iterator_Type := Find (Table, External_Tag (Key)); begin if I = Back (Table) then raise Tag_Error; end if; declare Factory : constant Factory_Type := Element (I); begin return Factory.all; --invoke factory function end; end New_T; If you're streaming off of disk, then the tag is probably stored in the stream in its external form, so one possibility is: function New_T (Stream : access Root_Stream_Type'Class) return T_Class_Access is Key : constant String := Read (Stream); --or whatever I : constant Iterator_Type := Find (Table, Key); begin ... declare Factory : constant Factory_Type := Element (I); begin return Factory (Stream); --invoke factory function end; end New_T; Here, we pass the stream to the factory function, and let it finish reading the object out of the stream. The map container in the example is from the Charles library. http://home.earthlink.net/~matthewjheaney/charles/ You can use either the sorted or hashed version of the map, that has type String as the key.