2013-10-22 26 views
1

我想在Ada.sequential_IO上放置包装或外观。这有点难看,但我正在尝试使用一些自动翻译的代码。所以,我有:Ada:在(通用)包中隐藏实现名称

with Ada.sequential_IO; 

generic 

    type element_type is private; 

package queue_file is 

    package implementation is new Ada.sequential_IO (element_type); 

    subtype instance is implementation.file_type; 

    function eofQ (channel : instance) return Boolean renames implementation.end_of_file; 
    procedure readQ (channel : in instance; item : out element_type) renames implementation.read; 
    -- etc. 

end queue_file; 

这是所有非常好,但名称queue_file.implementation是可见的。我试图把它移入私人部分,并写包实现是私人的,但它没有它。那么有什么方法可以隐藏名字?

+1

我不认为我会调用子程序'eofQ','readQ'等。为什么不只是'eof','read'? (或甚至'End_Of_File','Read'?:)) –

+0

+1对于Simon:为了明确它们是队列操作,使用限定名,queue_file.Read等 –

+0

@Simon:这只是我' m试图做,这是自动将Pascal源代码转换成Ada。 Pascal名称eof,read等被超载,因为它们可以应用于文本类型的对象和X类型的文件类型的对象。如果您感兴趣,我可以给您提供完整和肮脏的细节,但它非常适用于特定应用程序。 – Michael

回答

2

你不能做你想要做什么,至少不是W/O打破了由subtype instance is implementation.file_type;

例鉴于implementation可见的依赖:

private with Ada.Sequential_IO; 

generic 
    type element_type is private; 
package queue_file is 

    type instance is limited private; 

    function eofQ (channel : instance) return Boolean; 
    procedure readQ (channel : in instance; item : out element_type); 
    -- SUBPROGRAMS FOR SEQUENTIAL_IO INTERFACE -- 
    procedure Open 
     (File : in out instance; 
     Name : String; 
     Write: Boolean; 
     Form : String := ""); 

    procedure Read (File : instance; Item : out Element_Type); 
    procedure Write (File : instance; Item : Element_Type); 
    -- etc. 
private 
    package implementation is new Ada.sequential_IO (element_type); 

    type instance is new implementation.file_type; 
end queue_file; 

Pragma Ada_2012; 
package body queue_file is 

    function eofQ (channel : instance) return Boolean is 
     (implementation.end_of_file(implementation.File_Type(channel))); 

    procedure readQ (channel : in instance; item : out element_type) is 
    use implementation; 
    begin 
    implementation.read(File_Type(Channel), item); 
    end readQ; 

    procedure Open 
     (File : in out instance; 
     Name : String; 
     Write: Boolean; 
     Form : String := "") is 
    use implementation; 
    begin 
    Open(
     File => File_Type(File), 
     Mode => (if Write then Out_File else In_File), 
     Name => Name, 
     Form => Form 
     ); 
    end Open; 

    -- left as an exercise  
    procedure Read (File : instance; Item : out Element_Type) is null; 
    procedure Write (File : instance; Item : Element_Type) is null; 
    -- etc. 
end queue_file; 
+0

@Simon,谢谢你的修正。 – Shark8