Re: Access to function returning class-wide type
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Thu, 21 Aug 2008 15:34:39 +0200
On Thu, 21 Aug 2008 13:56:35 +0200, Paweł 'Nivertius' Płazieński wrote:
Dmitry A. Kazakov wrote:
What I need is a access type to 'constructor' of an derivate of abstractSee Ada.Tags.Generic_Dispatching_Constructor. It gives you a function
object.
As I presented, there is a workaround, but that's not the 'right' way to
do it. I really want to do it without some tricky wrapping.
How do I define The_Access_Type to do what I want?
constructing an object from the type tag, the parameters (Natural in your
case) using the function you want (Proc). It goes as follows:
with Ada.Tags.Generic_Dispatching_Constructor;
package A is
type Abstracted is abstract tagged null record;
function Proc (N : not null access Natural)
return Abstracted is abstract;
function Constructor is
new Ada.Tags.Generic_Dispatching_Constructor
(Abstracted, Natural, Proc);
end A;
I belive that would work too, but at the cost of defining function Proc (N :
not null access Natural) return ... for each descendant type.
Yes, it is the body of the "constructor".
That would be
another wrapper, so it's the same resolution to the problem as Proc_Wrapper
in my original code, assuming that I need Proc (N : Natural) as well.
Not really, because it would replace old Proc completely. The idea is:
with Ada.Tags; use Ada.Tags;
package A is
type Abstracted is abstract tagged null record;
-- To be used
function Factory (T : Tag; N : Natural) return Abstracted'Class;
-- Implementation detail, to be provided by each derived type
function Proc (N : not null access Natural)
return Abstracted is abstract;
end A;
with Ada.Tags.Generic_Dispatching_Constructor;
package body A is
function Make_It is
new Generic_Dispatching_Constructor (Abstracted, Natural, Proc);
function Factory (T : Tag; N : Natural) return Abstracted'Class is
Parameters : aliased Natural := N;
begin
return Make_It (T, Parameters'Access);
end Factory;
end A;
Now, when you derive from Abstracted you override Proc and that's all you
need to keep Factory working:
type Concrete is new Abstracted with private;
overriding Proc (N : not null access Natural return Concrete;
In short, the solution replaces pointer->constructor with tag->constructor.
Both enforced to be overridden.
P.S. I don't know reasons why it was decided to use an access type in
Ada.Tags.Generic_Dispatching_Constructor profile. One could always pass a
pointer there if a side effect on the parameters were desired. But that is
another issue, and in any case Proc is thought as an implementation detail,
so its exact profile should not really matter.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- Follow-Ups:
- Re: Access to function returning class-wide type
- From: Randy Brukardt
- Re: Access to function returning class-wide type
- References:
- Access to function returning class-wide type
- From: Paweł 'Nivertius' Płazieński
- Re: Access to function returning class-wide type
- From: Paweł 'Nivertius' Płazieński
- Access to function returning class-wide type
- Prev by Date: Re: Access to function returning class-wide type
- Next by Date: Re: Array slices and types
- Previous by thread: Re: Access to function returning class-wide type
- Next by thread: Re: Access to function returning class-wide type
- Index(es):
Relevant Pages
|