在Ada 2005中构建具有任务属性的类

时间:2011-11-08 02:21:04

标签: class ada

我在Ada 2005中有一个类Test_Class,它有一个名为Primary的父链接任务属性,类型为Primary_Task,定义为:

   type Test_Class is tagged limited
      record
         Info    : Integer;
         Value   : Float;
         Primary : Primary_Task (Test_Class'Access);
      end record;

我需要以

的形式为我的班级构建一步构造函数
   function Construct (T : access Test_Class) return Test_Class_Ptr is
   begin
      return new Test_Class'(Info => T.Info + 1,
                             Value => 0.0,
                             Primary => [WHAT I WANNA KNOW]);
   end Construct;

目前我的代码是:

-- test_pkg.ads
package Test_Pkg is
   type Test_Class;
   type Test_Class_Ptr is access all Test_Class;

   task type Primary_Task (This_Test : access Test_Class) is
      pragma Storage_Size (1000);
   end Primary_Task;

   type Test_Class is tagged limited
      record
         Info    : Integer;
         Value   : Float;
         Primary : Primary_Task (Test_Class'Access);
      end record;

   function Construct (T : access Test_Class) return Test_Class_Ptr;
end Test_Pkg;

-- test_pkg.adb
with Text_IO; use Text_IO;
package body Test_Pkg is
   [...]

   function Construct (T : access Test_Class) return Test_Class_Ptr is
      T_Ptr : constant Test_Class_Ptr := new Test_Class;
   begin
      T_Ptr.Info := T.Info + 1;
      T_Ptr.Value := 0.0;
      return T_Ptr;
   end Construct;
end Test_Pkg;

那么,我该如何编码呢?我应该在Primary => [...]代码中添加什么内容?我应该更改Primary : Primary_Task (Test_Class'Access);定义中Test_Class的定义吗?

1 个答案:

答案 0 :(得分:1)

我在comp.lang.ada上得到了Randy Brukardt(谢谢)的回答:

  

在Ada 2005或更高版本中,使用“<>”默认情况下初始化一个组件   聚合(这是你可以用任务做的唯一事情)。

     

(...)

function Construct (T : access Test_Class) return Test_Class_Ptr is
begin
 return new Test_Class'(Info => T.Info + 1,
                       Value => 0.0,
                       Primary => <>);
end Construct;

但是,我尝试使用GNAT GPL 2011编译它并获得GNATBUG

c:\tst>gnatmake -gnat12 test_pkg.adb
gcc -c -gnat12 test_pkg.adb

+===========================GNAT BUG DETECTED==============================+
| GPL 2011 (20110428) (i686-pc-mingw32) GCC error:                         |
| in create_tmp_var, at gimplify.c:505                                     |
| Error detected around test_pkg.adb:20:29                          |
| Please submit a bug report by email to report@adacore.com.               |
| GAP members can alternatively use GNAT Tracker:                          |
| http://www.adacore.com/ section 'send a report'.                         |
| See gnatinfo.txt for full info on procedure for submitting bugs.         |
| Use a subject line meaningful to you and us to track the bug.            |
| Include the entire contents of this bug box in the report.               |
| Include the exact gcc or gnatmake command that you entered.              |
| Also include sources listed below in gnatchop format                     |
| (concatenated together with no headers between files).                   |
| Use plain ASCII or MIME attachment.                                      |
+==========================================================================+
Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).

test_pkg.adb
test_pkg.ads

raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:423
gnatmake: "test_pkg.adb" compilation error

因此,GNAT GPL用户可能必须等待下一个版本才能使用此解决方案。