comp.lang.ada
 help / color / mirror / Atom feed
* new/free of abstract types
@ 2003-03-31  7:41 Frank
  2003-03-31 15:59 ` Matthew Heaney
  0 siblings, 1 reply; 4+ messages in thread
From: Frank @ 2003-03-31  7:41 UTC (permalink / raw)


Hi!

Regarding: GNAT3-15p/Windows XP/Ada95

I have an abstract type, and wish to perform a new/free of objects extended
from this type, inside one of the methods/subprograms
that has the abstract type as a classwide parameter (in one of the type
methods, to say it in Java/C++ language).see eksample below.

It seems that I cant to it this way:

See example below to see how "The_New_Object" is declared:

The_New_Object := new MyType;

because the real property of the type is not fully known in the scope.

What I wish is to create a new object of the same type as the object
"P_Parameter" (see example below).
Can it be done - if so, how is the syntax?

I havent yet tried to do this with the "Freeing" method, so a tip about that
would also be appreciated.


Frank

eksample:
---------------
type Pointer_To_MyType is access all MyType;

type A_Type is record
   The_New_Object : Pointer_To_MyType;
end record;

Something_Global : A_Type;


---------------
  procedure Subprogram ( .......
                                      P_Parameter : out MyType'Class) is

....
...
  begin
.....
....
      Someething_Global.The_New_Object := new MyType; <---------------??????










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

* Re: new/free of abstract types
  2003-03-31  7:41 new/free of abstract types Frank
@ 2003-03-31 15:59 ` Matthew Heaney
  2003-04-01  8:30   ` Frank
  0 siblings, 1 reply; 4+ messages in thread
From: Matthew Heaney @ 2003-03-31 15:59 UTC (permalink / raw)


"Frank" <franjoe@frisurf.no> wrote in message news:<3e87f119$1@news.wineasy.se>...
> 
> I have an abstract type, and wish to perform a new/free of objects extended
> from this type, inside one of the methods/subprograms
> that has the abstract type as a classwide parameter (in one of the type
> methods, to say it in Java/C++ language).  See example below.
> 
> It seems that I can't to it this way:
> 
> See example below to see how "The_New_Object" is declared:
> 
> The_New_Object := new MyType;
> 
> because the real property of the type is not fully known in the scope.
> 
> What I wish is to create a new object of the same type as the object
> "P_Parameter" (see example below).
> Can it be done - if so, how is the syntax?
> 
> I haven't yet tried to do this with the "Freeing" method, so a tip about that
> would also be appreciated.
> 
> 
> Frank
> 
> example:
> ---------------
> type Pointer_To_MyType is access all MyType;
> 
> type A_Type is record
>    The_New_Object : Pointer_To_MyType;
> end record;
> 
> Something_Global : A_Type;
> 
> ---------------
>   procedure Subprogram (P_Parameter : out MyType'Class) is
>   begin
>       Something_Global.The_New_Object := new MyType; <---?

You have to allocate objects of T'Class, like this:

type Pointer_To_MyType_Class is access all MyType'Class;

procedure Op (Arg : in MyType'Class) is  --in param, not out
begin
   Something_Global.The_New_Object := new MyType'Class'(Arg);

The_New_Object will inherit the type tag of Arg.  This is sort of like
the prototype pattern, except that it's built into the language.

Freeing the object isn't a problem, because Ada.Unchecked_Deallocation
accepts an indefinte type (as T'Class), e.g.

procedure Free is
   new Ada.Unchecked_Deallocation 
     (MyType'Class, 
      Pointer_To_MyType_Class);

Now you can say:

Free (Something_Global.The_New_Object);

Let me know if any of this isn't clear.  You asked an important
question, and it touches upon aspects of the language with which many
programmers aren't familiar.



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

* Re: new/free of abstract types
  2003-03-31 15:59 ` Matthew Heaney
@ 2003-04-01  8:30   ` Frank
  2003-04-01 15:13     ` Matthew Heaney
  0 siblings, 1 reply; 4+ messages in thread
From: Frank @ 2003-04-01  8:30 UTC (permalink / raw)



>
> Let me know if any of this isn't clear.  You asked an important
> question, and it touches upon aspects of the language with which many
> programmers aren't familiar.

Hi!
Thank you for help. This is what I "deduced" from Norman Cohens book also
:-)
It is just that it looks as if I want to initialise the new object with the
values from the parameter one.

As you may notice I have the parameter as "out" mode. This prototyping gives
me the impression that someting is sent in to
the subprogram still (tag perhaps?). Is it a problem for me to use only
"out" in this parameter or should I use "in out"?


Frank





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

* Re: new/free of abstract types
  2003-04-01  8:30   ` Frank
@ 2003-04-01 15:13     ` Matthew Heaney
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 2003-04-01 15:13 UTC (permalink / raw)


"Frank" <franjoe@frisurf.no> wrote in message news:<3e894e2b$1@news.wineasy.se>...
> >
> It is just that it looks as if I want to initialise the new object with the
> values from the parameter one.

It wasn't clear from your original example whether you wanted to
initialize the newly allocated object from the subprogram parameter.

If may be the case that you only need the tag of the object, and not
the object itself.  You could introduce a level of indirection using a
factory method:

  type T is abstract tagged null record;

  type T_Class_Access is access all T'Class;

  function New_T (Dummy : T) return T_Class_Access is abstract;

Each type that derives from T can then override New_T, to return a
newly allocated object of that same type, e.g.

   type NT is new T with null record;

   function New_T (Dummy : NT) return T_Class_Access is
   begin
      return new NT;  --ignore Dummy object
   end;

 
> As you may notice I have the parameter as "out" mode. This prototyping gives
> me the impression that someting is sent in to
> the subprogram still (tag perhaps?). Is it a problem for me to use only
> "out" in this parameter or should I use "in out"?

Use out if you're definately going to assign to the parameter.  
Use inout if you're only going to conditionally assign to the
parameter.



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

end of thread, other threads:[~2003-04-01 15:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-31  7:41 new/free of abstract types Frank
2003-03-31 15:59 ` Matthew Heaney
2003-04-01  8:30   ` Frank
2003-04-01 15:13     ` Matthew Heaney

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