我正在阅读 Barnes 的书“Programming in Ada 2012”。这是实现第 12.5 节中的堆栈的代码示例。
src/stacks.adb:(主要相关文件)
package body Stacks is
procedure Push(S: in out Stack; X: in Integer) is
begin
S := new Cell'(S,X);
end Push;
procedure Pop(S: in out Stack; X: in out Integer) is
begin
X := S.Value;
S := Stack(S.Next);
end Pop;
function "="(S, T: Stack) return Boolean is
SS: access Cell := S;
TT: access Cell := T;
begin
while SS /= null and TT /= null loop
if SS.Value /= TT.Value then …Run Code Online (Sandbox Code Playgroud) 不使用 nonstandard\xe2\x80\xa1 Scalar_Storage_Order 子句,如何通过记录表示子句结合任何其他语言功能的任意组合来可移植地表示 IPv4 标头,以便 \ xe2\x80\x9c相同的\xe2\x80\x9d 代码适用于小端和大端处理器,但以 IETF 称为网络字节的方式在线上发出(例如,通过以太网帧的有效负载) order(这是 IETF 对 big-endian 的奇特名称)。在 C 中,\xe2\x80\x9c相同的\xe2\x80\x9d 代码可以利用预处理器宏在小端处理器上执行字节交换,但在大端处理器上不执行任何操作,但标准 Ada 没有预处理器。在 C++ 中,\xe2\x80\x9c相同的\xe2\x80\x9d 代码可以利用元模板编程 (MTP) 在小端处理器上执行字节交换,但在大端处理器上不执行任何操作,但是标准 Ada 缺乏 MTP。
\n\n(顺便说一句,当大端处理器与小端外设 IC 的内存映射寄存器接口时,设备驱动程序中也会出现同样的问题,反之亦然:小端处理器与大端 IC 接口时的内存映射寄存器。)
\n\n BytesPerWord : constant := 4;\n BitsPerByte : constant := 8;\n PowerOf2Highest : constant := BytesPerWord*BitsPerByte - 1; -- part #1 of byte-swap\n type Header_IPv4 is record\n Version : integer range 0 .. F#16;\n IHL : integer range 0 .. F#16;\n TOS : integer …Run Code Online (Sandbox Code Playgroud) 我有以下用于网络协议实现的代码。由于协议是大端,我想使用Bit_Order属性和High_Order_First值,但似乎我犯了一个错误。
With Ada.Unchecked_Conversion;
with Ada.Text_IO; use Ada.Text_IO;
with System; use System;
procedure Bit_Extraction is
type Byte is range 0 .. (2**8)-1 with Size => 8;
type Command is (Read_Coils,
Read_Discrete_Inputs
) with Size => 7;
for Command use (Read_Coils => 1,
Read_Discrete_Inputs => 4);
type has_exception is new Boolean with Size => 1;
type Frame is record
Function_Code : Command;
Is_Exception : has_exception := False;
end record
with Pack => True,
Size => 8;
for Frame …Run Code Online (Sandbox Code Playgroud) 我很困惑如何'Size以及'Component_Size从文件中读取输入的工作时,并尝试使用Unchecked_Conversion。我知道要成功使用Unchecked_ConversionSource和Target都必须相同size。我正在从类似文件的输入中读取内容,000100000101001并希望使用未经检查的转换将其放入Bits数组中。但是,由于它们大小不一样或太小,因此转换似乎总是失败。
with Ada.Unchecked_Conversion;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure main is
type Bit is mod 2 with size => 1;
type Bit_Array is array(Positive range <>) of Bit with pack;
subtype Bit15 is Bit_Array(1 .. 15); //subtypes because can't use conversion on unconstrainted type
subtype Bit11 is Bit_Array(1 .. 11);
function StringA_to_Bit15 is
new Ada.Unchecked_Conversion(source => String, target => Bit15);
begin
while not end_of_file loop …Run Code Online (Sandbox Code Playgroud) 我的程序包中有一些方法可以对访问常量进行操作,以访问标记的记录。为了调用这些函数,我必须指定程序包名称。我宁愿只放置变量名[dot]函数名,但这会产生错误:no selector "foo" for type "Color"。这是为什么?
这是最小的复制器:
procedure Main is
type Color is tagged
record
Hue : Integer;
Saturation : Integer;
Value : Integer;
end record;
type AccessColor is access constant Color;
procedure foo (C : in AccessColor) is
begin
null;
end foo;
AccessS : AccessColor;
begin
foo (AccessS);
--AccessS.foo; -- does not work. Why?
end Main;
Run Code Online (Sandbox Code Playgroud)
请注意,在我的实际代码中,完全指定函数是不方便的,因为与上面的示例不同,foo是在单独的程序包中的某个位置定义的:
Some.Package.Name.Depp.foo(AccessS);
Run Code Online (Sandbox Code Playgroud)
即使AccessS已经指定在哪里可以找到函数,所以我应该能够做到:
AccessS.foo;
Run Code Online (Sandbox Code Playgroud) 使用GtkAda,我尝试使用资源API在代码中直接包含Glade文件。
为此,我们可以使用glib-compile-resources从一组资源中生成C代码,然后可以将其链接到Ada代码。
问题在于,此C代码需要Gtk包含,我们通常从Linux下的pkg-config命令获得该包含,例如
gcc -c -x c `pkg-config --cflags gio-2.0` myglade.gresource.c
Run Code Online (Sandbox Code Playgroud)
我想知道如何在GPRBuild项目文件中提供相同类型的信息。
仅供参考,我已经尝试在C语言的编译器软件包中使用pkg-config命令,但没有成功。当然,我设法手工建造,但这有点长:)