我正在和Ada打交道,我想了GNAT.Ctrl_C
包裹。我想尝试尝试一下,因为它看起来很整洁。我编写了以下程序来测试它
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Ctrl_C; use GNAT.Ctrl_C;
procedure Spam is
procedure MyHandler is
begin
Put_Line("YOU WILL NEVER END MY SPAM");
end MyHandler;
begin
Install_Handler(Handler => MyHandler);
while True loop
Put_Line("SPAM SPAM SPAM SPAM SPAM SPAM");
end loop;
end Spam;
当我尝试编译时,我得到错误"expected type Handler_Type defined at g-ctrl_c.ads:45"
和found procedure name, probably missing Access attribute
我听说过Ada有一个超级严格的类型系统,但这阻碍了我长时间学习语言。如果它有助于GNAT.Ctrl_C
的全文如下:
------------------------------------------------------------------------------
-- --
-- GNAT RUN-TIME COMPONENTS --
-- --
-- G N A T . C T R L _ C --
-- --
-- S p e c --
-- --
-- Copyright (C) 2002-2010, AdaCore --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
-- This package may be used to intercept the interruption of a running
-- program by the operator typing Control-C, without having to use an Ada
-- interrupt handler protected object.
-- This package is currently implemented under Windows and Unix platforms
-- Note concerning Unix systems:
-- The behavior of this package when using tasking depends on the interaction
-- between sigaction() and the thread library.
package GNAT.Ctrl_C is
type Handler_Type is access procedure;
-- Any parameterless library level procedure can be used as a handler.
-- Handler_Type should not propagate exceptions.
procedure Install_Handler (Handler : Handler_Type);
-- Set up Handler to be called if the operator hits Ctrl-C, instead of the
-- standard Control-C handler.
procedure Uninstall_Handler;
-- Reinstall the standard Control-C handler.
-- If Install_Handler has never been called, this procedure has no effect.
private
pragma Import (C, Uninstall_Handler, "__gnat_uninstall_int_handler");
end GNAT.Ctrl_C;
答案 0 :(得分:4)
Install_Handler(Handler => MyHandler'Unrestricted_Access);
与某些语言不同,要获得对过程的访问或“引用”或“指针”,您必须使用'Access
属性进行显式。这有点像在C中使用&func
来获取指向函数的指针,但C允许您省略&
。 Ada不允许这种速记,部分原因是因为Ada在调用没有参数的过程/函数时不需要括号,这意味着仅使用名称在语法上会有歧义。
在许多情况下,使用'Access
就足够了。这里还不够的原因是因为Ada的可访问性规则。假设你写了像
procedure Spam is
Number_Of_Vikings : Integer := 0;
procedure MyHandler is
begin
Put_Line("YOU WILL NEVER END MY SPAM");
Number_Of_Vikings := Number_Of_Vikings + 1;
end MyHandler;
begin
Install_Handler(Handler => MyHandler);
--while True loop
for I in 1 .. 10 loop
Put_Line("SPAM SPAM SPAM SPAM SPAM SPAM");
end loop;
end Spam;
理论上,Install_Handler
可以将Handler
参数存储在全局中,这意味着(理论上)其他东西可以使用访问过程全局来调用MyHandler
之后Spam
过程已返回。然后,MyHandler
会尝试使用Number_Of_Vikings
执行某些操作。但是该变量将不再存在,因为它属于不再运行的Spam
过程。结果可能是灾难性的。 (JavaScript有闭包所以即使在Spam
退出之后它也会保持变量,而Java和C ++ 11也只有有限的形式,但是Ada没有任何等价物。)因此,Ada有规则防止这种事情发生。要绕过这些规则,你可以使用'Unrestricted_Access
(这是GNAT属性,不是由语言定义的),它告诉编译器“我知道我在做什么,不要担心它。” / p>
答案 1 :(得分:4)
如果处理程序被移动到&#34;库级&#34; (即未嵌入您的测试程序中),将'Access
添加到对它的引用就足够了:
my_handler.ads:
procedure My_Handler;
my_handler.adb:
with Ada.Text_IO;
procedure My_Handler is
begin
Ada.Text_IO.Put_Line ("YOU WILL NEVER END MY SPAM");
end My_Handler;
spam.adb:
with Ada.Text_IO;
with GNAT.Ctrl_C;
with My_Handler;
procedure Spam is
begin
GNAT.Ctrl_C.Install_Handler (Handler => My_Handler'Access);
loop
Ada.Text_IO.Put_Line("SPAM SPAM SPAM SPAM SPAM SPAM");
end loop;
end Spam;