Re: How to hide type internals



Gerd wrote:

Hi,

I would like to write a package that exports functions using a special
type, without showing the details in the spec (even not in the private
part).
In Modula-2 one can declare opaque types (which mostly are pointers but
also can be any type of word size).

I've done this in occasions where I want to have a multi-platform package,
so switching bodies is enough, keeping the same spec file. Like this:

package Blah is

type External is private;

-- Operations on external

private

type Internal; -- forward declaration

type Internal_Acess is access all Internal;

type External is record
Ptr : Internal_Access;
end record;

end;

And then you define Internal in the body.

With Ada05 you can even get rid of Internal_Access and simply have

type External is record
Ptr : access Internal;
end record;

Of course you can have reference counting, deep copying or whatever making
External a Controlled type, so you can have a proper abstraction (This will
be probably a requisite for initialization anyways...)

Hope this helps.
.



Relevant Pages

  • Re: Pure, Storage_Size and Unchecked_Conversion
    ... function G(X: Ptr) return Ptr; ... package body PP is ... generic type T is private; type A is access T; ... pragma Pure; --OK ...
    (comp.lang.ada)
  • Re: SPARK examiner visibility problem
    ... #hide Std_IO annotation). ... #hide annotations must be in the spec, not in the body, to take ... a package body ... the --# hide follows the 'is' keyword. ...
    (comp.lang.ada)
  • Re: Ada to Ada Pragma Export/Import
    ... access the public routines in their Ada code. ... package bodies (but can deliver the object files, the specs, and the ... No. just deliver a normal Ada spec; ...
    (comp.lang.ada)
  • Re: parent package referring to child
    ... handle this in all Ada's spec. ... That is split the FOO package into smaller files and use the "seperate" ... package body Ch10 is separate; ...
    (comp.lang.ada)
  • circular unit dependency
    ... I've got a problem with circular unit dependency. ... In main package I have: ... in derv1 package spec and body I have: ...
    (comp.lang.ada)