How to pass around access types to interfaces? - ADA

This is a discussion on How to pass around access types to interfaces? - ADA ; I'm learning Ada and I've run into a big problem with passing around access types to interfaces. I guess I just don't know all the subtleties of access types, I'm used to C/C++ pointers. I didn't find anything helpful on ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

How to pass around access types to interfaces?

  1. Default How to pass around access types to interfaces?

    I'm learning Ada and I've run into a big problem with passing around
    access types to interfaces. I guess I just don't know all the
    subtleties of access types, I'm used to C/C++ pointers. I didn't find
    anything helpful on the net or in the Ada Reference Manual.

    I'm trying to get this code to run:

    in some package:
    type My_Interface is interface;
    type My_Interface_Access is access all My_Interface'Class;

    procedure Do_Something( Item : in out Some_Object; With :
    My_Interface_Access );

    in main program:
    type Concrete_Type is new My_Interface with ...;


    I need to pass an object of type Concrete_Type to the With parameter
    of the Do_Something procedure. But the Compiler won't let me, no
    matter what I do.


    My_Object : access Concrete_Type := new Concrete_Type;
    Do_Something( Item, My_Interface_Access( My_Object ) );

    gives the compiler error "cannot convert local pointer to non-local
    access type".

    My_Object : My_Interface_Access := new Concrete_Type;

    gives the compiler error "type in allocator has deeper level than
    designated class-wide type".

    I found a way to pass my paramter by using this function:

    function Convert( Item : not null access Concrete_Type ) return
    My_Interface_Access is
    begin
    return My_Interface_Access( Item );
    end;

    But I don't this is such a good idea.

    What is the right way to do this? I can't imagine that what I'm trying
    to do is not supported in Ada.

  2. Default Re: How to pass around access types to interfaces?

    Sven wrote:
    > I'm learning Ada and I've run into a big problem with passing around
    > access types to interfaces. I guess I just don't know all the
    > subtleties of access types, I'm used to C/C++ pointers. I didn't find
    > anything helpful on the net or in the Ada Reference Manual.
    >
    > I'm trying to get this code to run:
    >
    > in some package:
    > type My_Interface is interface;
    > type My_Interface_Access is access all My_Interface'Class;
    >
    > procedure Do_Something( Item : in out Some_Object; With :
    > My_Interface_Access );
    >
    > in main program:
    > type Concrete_Type is new My_Interface with ...;


    I assume this type declaration is in the main *procedure*. This
    means that the type has a local scope and life, which prevents
    converting its values to a more long-lived access type. Try
    declaring Concrete_Type in a package; that makes it live as long as
    the whole program, and should allow such conversions.

    HTH,

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .

  3. Default Re: How to pass around access types to interfaces?

    > I assume this type declaration is in the main *procedure*. This
    > means that the type has a local scope and life, which prevents
    > converting its values to a more long-lived access type. Try
    > declaring Concrete_Type in a package; that makes it live as long as
    > the whole program, and should allow such conversions.


    Yeah, the declaration was in the main procedure. When moving it to a
    package everything works like it's supposed to. Thanks for your help.

    I guess I have still a lot to learn about Ada. I was just thinking
    about the lifetime of the object. It never occured to me that a type
    also has a lifetime.

  4. Default Re: How to pass around access types to interfaces?

    > > I assume this type declaration is in the main *procedure*. This
    > > means that the type has a local scope and life, which prevents
    > > converting its values to a more long-lived access type. Try
    > > declaring Concrete_Type in a package; that makes it live as long as
    > > the whole program, and should allow such conversions.

    >
    > Yeah, the declaration was in the main procedure. When moving it to a
    > package everything works like it's supposed to. Thanks for your help.
    >
    > I guess I have still a lot to learn about Ada. I was just thinking
    > about the lifetime of the object. It never occured to me that a type
    > also has a lifetime.


    This is one of the most complex areas of Ada; it defines the lifetime
    of both objects and types in terms of "accessibility levels" (RM
    3.10.2). This explains the error message "type in allocator has deeper
    level than designated class-wide type".

    Types can have a limited life time since they can be declared inside a
    subprogram (or indeed in any block).

    --
    Ludovic Brenta.

  5. Default Re: How to pass around access types to interfaces?

    On Oct 24, 12:45 pm, Sven <sven.weida...@gmail.com> wrote:
    > I'm trying to get this code to run:
    >
    > in some package:
    >   type My_Interface is interface;
    >   type My_Interface_Access is access all My_Interface'Class;
    >
    >   procedure Do_Something( Item : in out Some_Object; With :
    > My_Interface_Access );


    Does your compiler let you use the reserved word 'with' as the name of
    a parameter? That could confuse things ..

  6. Default Re: How to pass around access types to interfaces?

    On 24 Okt., 23:32, sjw <simon.j.wri...@mac.com> wrote:
    > On Oct 24, 12:45 pm, Sven <sven.weida...@gmail.com> wrote:
    >
    > > I'm trying to get this code to run:

    >
    > > in some package:
    > >   type My_Interface is interface;
    > >   type My_Interface_Access is access all My_Interface'Class;

    >
    > >   procedure Do_Something( Item : in out Some_Object; With :
    > > My_Interface_Access );

    >
    > Does your compiler let you use the reserved word 'with' as the name of
    > a parameter? That could confuse things ..


    I guess not. My real code looks different, I just made those names up
    to show what my problem is. That was easier than to strip down my
    actual code to just the problem.

  7. Default Re: How to pass around access types to interfaces?

    Am 24.10.2008, 13:45 Uhr, schrieb Sven <sven.weidauer@gmail.com>:

    > type My_Interface_Access is access all My_Interface'Class;


    Do you realy need an access type? Read:

    http://en.wikibooks.org/wiki/Ada_Pro...lass-wide_type

    If so, does it need to be an access all? Read:

    http://en.wikibooks.org/wiki/Ada_Pro...vs._access_all

    Six month after I switched from C++ to Ada I removed about 80% access
    types from the code I created until then. And from those access types
    which remained more did not need the "all" and I consequently removed that
    as well.

    Martin
    --
    Martin Krischik

  8. Default Re: How to pass around access types to interfaces?

    "Martin Krischik" <krischik@users.sourceforge.net> writes:

    > Am 24.10.2008, 13:45 Uhr, schrieb Sven <sven.weidauer@gmail.com>:
    >
    >> type My_Interface_Access is access all My_Interface'Class;

    >
    > Do you realy need an access type? Read:
    >
    > http://en.wikibooks.org/wiki/Ada_Pro...lass-wide_type
    >
    > If so, does it need to be an access all? Read:


    In my experience, access-to-class-wide types almost always need to have
    "all", because you often want to convert from access-to-T2 up to
    access-to-T1'Class (where T2 is a descendant of T1).

    - Bob

  9. Default Re: How to pass around access types to interfaces?

    On Sat, 25 Oct 2008 18:15:09 -0400, Robert A Duff wrote:

    > "Martin Krischik" <krischik@users.sourceforge.net> writes:
    >
    >> Am 24.10.2008, 13:45 Uhr, schrieb Sven <sven.weidauer@gmail.com>:
    >>
    >>> type My_Interface_Access is access all My_Interface'Class;

    >>
    >> Do you realy need an access type? Read:
    >>
    >> http://en.wikibooks.org/wiki/Ada_Pro...lass-wide_type
    >>
    >> If so, does it need to be an access all? Read:

    >
    > In my experience, access-to-class-wide types almost always need to have
    > "all", because you often want to convert from access-to-T2 up to
    > access-to-T1'Class (where T2 is a descendant of T1).


    It would be nice if that were finally fixed![*]

    But, in general it is better to use only access to class-wide. So the cases
    where access to T2 is converted to access to T1 are suspicious to me. When
    that happens I readily use Unchecked_Conversion, since it is already
    broken.

    --------
    * Similar issue: there is no good way to obtain a pool-specific access
    type from Finalize.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

  10. Default Re: How to pass around access types to interfaces?

    Am 26.10.2008, 00:15 Uhr, schrieb Robert A Duff
    <bobduff@shell01.theworld.com>:

    > "Martin Krischik" <krischik@users.sourceforge.net> writes:
    >
    >> Am 24.10.2008, 13:45 Uhr, schrieb Sven <sven.weidauer@gmail.com>:
    >>
    >>> type My_Interface_Access is access all My_Interface'Class;

    >>
    >> Do you realy need an access type? Read:
    >>
    >> http://en.wikibooks.org/wiki/Ada_Pro...lass-wide_type
    >>
    >> If so, does it need to be an access all? Read:

    >
    > In my experience, access-to-class-wide types almost always need to have
    > "all", because you often want to convert from access-to-T2 up to
    > access-to-T1'Class (where T2 is a descendant of T1).


    I guess you are right here. But then: access to class wide is the kind of
    access which a C++ convert will uses without realy needing them. I should
    know, I am a C++ convert who made precisely that mistake.

    Martin

    --
    Martin Krischik

+ Reply to Thread
Page 1 of 2 1 2 LastLast