comp.lang.ada
 help / color / mirror / Atom feed
* How Can I Create A Class Dynamically
@ 2001-09-24 16:32 Anthony E. Glover
  2001-09-24 16:48 ` Samuel Tardieu
  2001-10-01 12:35 ` Lutz Donnerhacke
  0 siblings, 2 replies; 8+ messages in thread
From: Anthony E. Glover @ 2001-09-24 16:32 UTC (permalink / raw)


I have a class hierarchy defined and I would like to be able to read a
string and then dynamically creating a pointer to an object of the type
specified by the string. Similar I think to class loading and class
factories in Java. Can this be done in Ada95. If so, how. I have looked at
the Ada.Tags packages and this looks like it might be helpfull, but I'm not
quite sure how to make use of it. I would basically like to be able to avoid
having to specify a case statement or if block when initially creating my
objects.

Thanks,
Tony







^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-09-24 16:32 How Can I Create A Class Dynamically Anthony E. Glover
@ 2001-09-24 16:48 ` Samuel Tardieu
  2001-09-25 12:40   ` Anthony E. Glover
  2001-10-01 12:35 ` Lutz Donnerhacke
  1 sibling, 1 reply; 8+ messages in thread
From: Samuel Tardieu @ 2001-09-24 16:48 UTC (permalink / raw)
  To: comp.lang.ada

On 24/09, Anthony E. Glover wrote:

| I have a class hierarchy defined and I would like to be able to read a
| string and then dynamically creating a pointer to an object of the type
| specified by the string. Similar I think to class loading and class
| factories in Java. Can this be done in Ada95. If so, how. I have looked at
| the Ada.Tags packages and this looks like it might be helpfull, but I'm not
| quite sure how to make use of it. I would basically like to be able to avoid
| having to specify a case statement or if block when initially creating my
| objects.

Let's assume that the root of your hierarchy is called Root.

One modular and portable way which would work with any language supporting
elaboration would be to:

 - have a package (probably using a protected object behind the scene) which
   exports a Register subprogram such as:

          type Root_Access is access all Root'Class;
          type Factory is access function return Root_Access;
          procedure Register (Name : String; F : Factory);

   Register would use a hash table to associate names to factories.

 - in this package, also have a Create subprogram looking like:

           function Create (Name : String) return Root_Access;

   whose body would look like:

          return Lookup (Name) .all;

   (with of course added checks) where Lookup looks up in the hash table

  - have each type register itself during the elaboration by declaring
    a factory function and registering it using Register

This way, you get a factory which knows only the types present in your
final application.

Note that you are likely to run into elaboration issues with the root type
if your "factory-like package" is not the same as the one where the root
type is declared.

If your root type is abstract (or more generally if it cannot be used with
a factory), then you can more easily solve elaboration problems. Also, it
may be worth making a generic package to implement this solution.

HopI hope you got the idea.e this helps.

  Sam




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-09-24 16:48 ` Samuel Tardieu
@ 2001-09-25 12:40   ` Anthony E. Glover
  0 siblings, 0 replies; 8+ messages in thread
From: Anthony E. Glover @ 2001-09-25 12:40 UTC (permalink / raw)


Thanks for the suggestion. I will give it a try.

Tony

"Samuel Tardieu" <sam@rfc1149.net> wrote in message
news:mailman.1001350161.22792.comp.lang.ada@ada.eu.org...
> On 24/09, Anthony E. Glover wrote:
>
> | I have a class hierarchy defined and I would like to be able to read a
> | string and then dynamically creating a pointer to an object of the type
> | specified by the string. Similar I think to class loading and class
> | factories in Java. Can this be done in Ada95. If so, how. I have looked
at
> | the Ada.Tags packages and this looks like it might be helpfull, but I'm
not
> | quite sure how to make use of it. I would basically like to be able to
avoid
> | having to specify a case statement or if block when initially creating
my
> | objects.
>
> Let's assume that the root of your hierarchy is called Root.
>
> One modular and portable way which would work with any language supporting
> elaboration would be to:
>
>  - have a package (probably using a protected object behind the scene)
which
>    exports a Register subprogram such as:
>
>           type Root_Access is access all Root'Class;
>           type Factory is access function return Root_Access;
>           procedure Register (Name : String; F : Factory);
>
>    Register would use a hash table to associate names to factories.
>
>  - in this package, also have a Create subprogram looking like:
>
>            function Create (Name : String) return Root_Access;
>
>    whose body would look like:
>
>           return Lookup (Name) .all;
>
>    (with of course added checks) where Lookup looks up in the hash table
>
>   - have each type register itself during the elaboration by declaring
>     a factory function and registering it using Register
>
> This way, you get a factory which knows only the types present in your
> final application.
>
> Note that you are likely to run into elaboration issues with the root type
> if your "factory-like package" is not the same as the one where the root
> type is declared.
>
> If your root type is abstract (or more generally if it cannot be used with
> a factory), then you can more easily solve elaboration problems. Also, it
> may be worth making a generic package to implement this solution.
>
> HopI hope you got the idea.e this helps.
>
>   Sam
>






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-09-24 16:32 How Can I Create A Class Dynamically Anthony E. Glover
  2001-09-24 16:48 ` Samuel Tardieu
@ 2001-10-01 12:35 ` Lutz Donnerhacke
  2001-10-02 23:13   ` R. Tim Coslet
  1 sibling, 1 reply; 8+ messages in thread
From: Lutz Donnerhacke @ 2001-10-01 12:35 UTC (permalink / raw)


* Anthony E. Glover wrote:
>I have a class hierarchy defined and I would like to be able to read a
>string and then dynamically creating a pointer to an object of the type
>specified by the string. Similar I think to class loading and class
>factories in Java. Can this be done in Ada95. If so, how. I have looked at
>the Ada.Tags packages and this looks like it might be helpfull, but I'm not
>quite sure how to make use of it. I would basically like to be able to avoid
>having to specify a case statement or if block when initially creating my
>objects.

IMHO this is not possible, due to missing typeness.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-10-01 12:35 ` Lutz Donnerhacke
@ 2001-10-02 23:13   ` R. Tim Coslet
  2001-10-04  8:55     ` Lutz Donnerhacke
  0 siblings, 1 reply; 8+ messages in thread
From: R. Tim Coslet @ 2001-10-02 23:13 UTC (permalink / raw)


lutz@iks-jena.de (Lutz Donnerhacke) wrote in message news:<slrn9rgoo2.k8.lutz@taranis.iks-jena.de>...
> * Anthony E. Glover wrote:
> >I have a class hierarchy defined and I would like to be able to read a
> >string and then dynamically creating a pointer to an object of the type
> >specified by the string. Similar I think to class loading and class
> >factories in Java. Can this be done in Ada95. If so, how. I have looked at
> >the Ada.Tags packages and this looks like it might be helpfull, but I'm not
> >quite sure how to make use of it. I would basically like to be able to avoid
> >having to specify a case statement or if block when initially creating my
> >objects.
> 
> IMHO this is not possible, due to missing typeness.

Have you looked at the package Ada.Streams.Stream_IO ?

This package was designed specifically to allow input/output of tagged
types (the file is NOT a text file however).

Even if Stream_IO isn't exactly what you are looking for you might
find some clues here as to how to do what you need.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-10-02 23:13   ` R. Tim Coslet
@ 2001-10-04  8:55     ` Lutz Donnerhacke
  2001-10-04 16:39       ` Pascal Obry
  2001-10-04 17:06       ` R. Tim Coslet
  0 siblings, 2 replies; 8+ messages in thread
From: Lutz Donnerhacke @ 2001-10-04  8:55 UTC (permalink / raw)


* R. Tim Coslet wrote:
>lutz@iks-jena.de (Lutz Donnerhacke) wrote in message news:<slrn9rgoo2.k8.lutz@taranis.iks-jena.de>...
>> * Anthony E. Glover wrote:
>> >I have a class hierarchy defined and I would like to be able to read a
>> >string and then dynamically creating a pointer to an object of the type
>> >specified by the string. Similar I think to class loading and class
>> >factories in Java. Can this be done in Ada95. If so, how. I have looked at
>> >the Ada.Tags packages and this looks like it might be helpfull, but I'm not
>> >quite sure how to make use of it. I would basically like to be able to avoid
>> >having to specify a case statement or if block when initially creating my
>> >objects.
>> 
>> IMHO this is not possible, due to missing typeness.
>
>Have you looked at the package Ada.Streams.Stream_IO ?
>
>This package was designed specifically to allow input/output of tagged
>types (the file is NOT a text file however).

I wonder if Stream_IO is able to read instances of derivated types unknown
at the compile time of the programm. I'd look at the Objective-* Code.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-10-04  8:55     ` Lutz Donnerhacke
@ 2001-10-04 16:39       ` Pascal Obry
  2001-10-04 17:06       ` R. Tim Coslet
  1 sibling, 0 replies; 8+ messages in thread
From: Pascal Obry @ 2001-10-04 16:39 UTC (permalink / raw)



lutz@iks-jena.de (Lutz Donnerhacke) writes:
> I wonder if Stream_IO is able to read instances of derivated types unknown
> at the compile time of the programm. I'd look at the Objective-* Code.

No, this is not possible.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|         http://perso.wanadoo.fr/pascal.obry
--|
--| "The best way to travel is by means of imagination"



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How Can I Create A Class Dynamically
  2001-10-04  8:55     ` Lutz Donnerhacke
  2001-10-04 16:39       ` Pascal Obry
@ 2001-10-04 17:06       ` R. Tim Coslet
  1 sibling, 0 replies; 8+ messages in thread
From: R. Tim Coslet @ 2001-10-04 17:06 UTC (permalink / raw)


lutz@iks-jena.de (Lutz Donnerhacke) wrote in message news:<slrn9ro8vr.4a.lutz@taranis.iks-jena.de>...
> * R. Tim Coslet wrote:
> >lutz@iks-jena.de (Lutz Donnerhacke) wrote in message news:<slrn9rgoo2.k8.lutz@taranis.iks-jena.de>...
> >
> >Have you looked at the package Ada.Streams.Stream_IO ?
> >
> >This package was designed specifically to allow input/output of tagged
> >types (the file is NOT a text file however).
> 
> I wonder if Stream_IO is able to read instances of derivated types unknown
> at the compile time of the programm. I'd look at the Objective-* Code.

No, all types must have been defined at compile time or there is no
internal representation for the Stream representation and Stream_IO
will raise Constraint_Error.

If the types were not defined at compile time and Stream_IO accepted
them, the program would not know how to work with the objects... all
operators of a 'Class are defined at compile time.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-10-04 17:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-24 16:32 How Can I Create A Class Dynamically Anthony E. Glover
2001-09-24 16:48 ` Samuel Tardieu
2001-09-25 12:40   ` Anthony E. Glover
2001-10-01 12:35 ` Lutz Donnerhacke
2001-10-02 23:13   ` R. Tim Coslet
2001-10-04  8:55     ` Lutz Donnerhacke
2001-10-04 16:39       ` Pascal Obry
2001-10-04 17:06       ` R. Tim Coslet

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