在阿达类型/包别名独立宣言(Separate declarations of types/packa

2019-09-17 00:07发布

我想声明一些“用户自定义编译器常量”,以保持我的规范文件,“恒”成为可能。 这是在C ++中,如共同的东西:

// 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);
}

这将是阿达:

-- 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;

当然,这也不行,因为use是一个上下文关键字...所以我的问题:怎么可能跟Ada中相同的结果的东西吗?

Answer 1:

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

To start with, corresponding more-or-less, I think, to your namespace misc, in file misc.ads,

package Misc is
end Misc;

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

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

(which could, of course, also reference types in Misc). Then, corresponding to code.hh, in file misc-code.ads,

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

Personally I wouldn’t use Config;, at any rate in specs - it can make it difficult to work out where something is defined. Note, you can say use Config; or use Misc.Config; where shown, because you’re in a child of Misc; in the context clause, which is also OK, you would have to use Misc.Config;.



Answer 2:

好吧,当我看到你正在试图做的,你要去了解它错了什么。 你必须与给定sniplets问题是这样的:循环依赖。

现在,阿达已通过使非圆形(在某种意义上)通过它的使用规范和机构的管理循环依赖的一个好方法。 由于两者是分离的,我们可以有两套规格/体文件,其中身体正是引用了其他的规范,因为规范是公开可见。

作为一个例子,诚然荒谬; 让我们用喷泉,钢笔和墨水盒。

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;

(不编译。)我忘了怎么虽然创建了一个循环嵌套类型的组件...无论如何,你应该得到的总体思路有。 但是,你想比相互依存的类型完全不同的; 你想要的是某种你常数容器。

我会建议使用子包。 喜欢的东西MiscMisc.Constants ; 在那里你把你的基本类型为杂项和常数为Misc.Constants。 如果你的类型取决于您的常量,你可能会做一个独立的子包为他们Misc.Types和使用的相互withing如上图所示。



Answer 3:

你可以把孩子包规格父包规格里面,就像这样:

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;

你甚至可以use Types; 之后的声明Misc.Types



文章来源: Separate declarations of types/packages aliases in Ada
标签: packaging ada