Separate package type / alias declarations in Ada

I would like to declare some “user-defined compiler constants” so that my specification files are as “constant” as possible. This is something common in C ++, for example.

// misc/config.hh
namespace misc
{
  typedef std::shared_ptr<A> A_ptr; 
  namespace arch = ibmpc;
}

// misc/code.hh
#include "misc/config.hh"
namespace misc
{
  void function p(A_ptr a);
}

What will happen in Ada:

-- misc.ads
package Misc is
  use Types; ----> forbidden !

  procedure P(A : A_Access);
end Misc;

-- misc-types.ads
package Misc.Types is
  type A_Access is A'Access;
end Misc.Types;

Of course, this does not work, as it useis the keyword of the context ... therefore, my question is: how can something be done with the same results in Ada?

+2
source share
3 answers

I think this is a reasonable mapping from your original C ++ to Ada:

For starters, the corresponding more or less, I think yours namespace misc, in the file misc.ads,

package Misc is
end Misc;

Then, the corresponding config.hh, in the file misc-config.ads,

package Misc.Config is
   type A is (For_Example, An_Enumeration);
   type A_Access is access A;
end Misc.Config;

(, , Misc). , code.hh, misc-code.ads,

with Misc.Config;
package Misc.Code is
   use Config;
   procedure P (A : A_Access);
end Misc.Code;

wouldnt use Config;, - , - . , use Config; use Misc.Config;, , youre Misc; , , use Misc.Config;.

+2

, , , . , sniplets, : .

Ada , ( ) . , spec/body, , .

, , ; .

With Cartridge;
Package Pen is

  Subtype Model_Number is Positive Range 1000..3304;
  Type Fountain_pen is private;
  -- Pen procedures
private
  Type Fountain_pen is Record
 Model  : Model_Number;
     Ink    : Cartridge.Ink_Cartridge;
  end record;      
end Pen;

With Pen;
Package Cartridge is
  Type Ink_Cartridge is private;
  -- Ink procedures
private
  Type Ink_Cartridge is record
     Color      : Integer; -- I'm being lazy.
     This_Pen   : Pen.Fountain_pen;
  end record;
end Cartridge;

Package Body Pen is
  --- Pen Stuff
end Pen;

Package Body Cartridge is
  -- Ink stuff
end Cartridge;

( .) , , ... , . , , ; , , .

. - Misc Misc.Constants; Misc Misc.Constants. , Misc.Types , .

+1

You can put the specification of the child package in the specification of the parent package, for example:

package Misc is
   package Types is
      type A is private;
      type A_Access is access A;
      -- other stuff
   end Types;

   procedure P (A : Types.A_Access);
end Misc;

You can even use Types;after the announcement Misc.Types.

+1
source

All Articles