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

发布于 2024-12-13 23:36:23 字数 1635 浏览 1 评论 0原文

我在 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 => 中放入什么? [...] 代码?我是否应该更改 Test_Class 定义中的 Primary : Primary_Task (Test_Class'Access); 的定义?

I have a class Test_Class in Ada 2005 which has a parent-linked task property called Primary, from type Primary_Task, defined as:

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

I need build a one-step constructor for my class in the form

   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;

Currently my code is:

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

So, how can I code it? What should I put in Primary => [...] code? Should I change the definition of Primary : Primary_Task (Test_Class'Access); in Test_Class definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-12-20 23:36:23

我在 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 [email protected].               |
| 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 用户可能必须等待下一个版本才能使用此解决方案。

I got an answer from Randy Brukardt (thank you) on comp.lang.ada:

In Ada 2005 or later, use "<>" to default initialize a component in an
aggregate (which is the only thing you can do with a task).

(...)

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;

However, I tried to compile it using GNAT GPL 2011 and got the GNATBUG below

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 [email protected].               |
| 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

So GNAT GPL users may have to wait for the next release to use this solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文