From: andrewd@cs.adelaide.edu.au (Andrew Dunstan)
Newsgroups: comp.lang.ada
Subject: converting (limited) private types?
Date: 26 Sep 92 02:40:46 GMT
Organization: The University of Adelaide
NNTP-Posting-Host: achilles.cs.adelaide.edu.au

HACK ALERT! What follows is not for the squeamish.

Last night I got bored by the old Jimmy Stewart movie in the box,
so I turned my mind to a problem that has been bugging me a bit.

I am in the process of writing up the user manual for my LL1 parser
generator, written in 100% pure Ada of course, which interfaces with
an aflex-generated scanner. (and quite nice it is too - handles Pascal
quite well, and if some kind soul has an LL1ish Ada grammar I would love
to put the damned thing through its paces properly :-) )

Getting on to the advanced topics, I want to tell the user how to
handle "include" files, if the language they are dealing with is so
silly as to use them :-). The way I would normally do this in the case 
where file inclusion can be nested would be to have a stack of files.

Trouble is, file types in Ada are limited private. So you can't assign
to objects of the type. So you can't make a stack of them.

While pondering this, an evil thought came into my head. Before succumbing
to my better feelings, I quickly dialled up the university and tried the
following:

  with text_io; use text_io;
  with unchecked_conversion;
  procedure file_conv is


     type conv is array(1..file_type'size) of boolean;
     pragma pack(conv);
     function to_conv is new unchecked_conversion(file_type,conv);
     function to_file is new unchecked_conversion(conv,file_type);
   
   
    a_file : file_type;
    a_conv : conv;
  
  begin
    open(a_file,out_file,"foo_bar");
    set_output(a_file);
    put_line("this should not be seen");
    a_conv := to_conv(a_file);
    set_output(standard_output);
    put_line("This should be seen");
    set_output(to_file(a_conv));
    put_line("This should not be seen either");
    set_output(standard_output);
    put_line("This should also be seen");
  --  close(to_file(a_conv));
  end;
   
Of course, the commented out call to close won't work, since it takes
an in out file parameter. The rest of it, perhaps surprisingly, does.

What I want to know is:

(1) APART from any righteous feelings about breaking the language's
privacy mechanism, are there any good reasons for not doing something
like this?

(2) Is there a better way to do it, one which does not rely on this
sort of hack?

(3) How can I close the damned file? (I've thought of a really disgusting
way, which oddly enough should be reasonably portable, but I certainly won't
publish it on the net.)

I would be grateful for advice. I won't be grateful for flames, but I can stand
them. :-)


-- 
#######################################################################
#  Andrew Dunstan                   #   There's nothing good or bad   #
#  Department of Computer Science   #   but thinking makes it so.     #
#  University of Adelaide           #                                 #
#  South Australia                  #          - Shakespeare          #
#  net: andrewd@cs.adelaide.edu.au  #                                 #
#######################################################################
Newsgroups: comp.lang.ada