comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: 'private' and Privacy
Date: Wed, 8 Jul 2009 05:47:39 -0700 (PDT)
Date: 2009-07-08T05:47:39-07:00	[thread overview]
Message-ID: <2e6bb81a-6e74-4198-abb4-33b584931e9f@g31g2000yqc.googlegroups.com> (raw)
In-Reply-To: 843a36b0-041d-4826-98b4-0fbcb1a4d287@d9g2000prh.googlegroups.com

Rick Duley wrote on comp.lang.ada:
> I have never fathomed out why we declare entities in packages to be
> 'private' and then have to go and tell the world what is in them.  I
> would assume that 'private' has something to do with 'information
> hiding' yet we expose what is 'private'.

Because the compiler needs to know the sizes of all types and objects
so it can emit the code for passing parameters to the subprograms
declared in the package.  Ada is designed in such a way that all the
information is in the spec, so the compiler needs not look at the
body.  This in turn makes it possible to provide only the specs and
not the source files for the body.

> I have:
>
>    KEYPAD_ROWS_COUNT : constant Positive := 2;
>    -- The number of rows on a keypad.
>
>    KEYPAD_COLUMNS_COUNT : constant Positive := 2;
>    -- The number of columns on a keypad.
>
>    type Keys_Type is array
>      (1 .. KEYPAD_ROWS_COUNT, 1 .. KEYPAD_COLUMNS_COUNT)
>    of Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
>    --Intermediate, addressable storage of keys for the keypad.
>
> I am trying to find a way to ensure that the user only addresses items
> in the array in the manner I provide rather than making use of the
> information clearly visible about the range of the array.  I can use
> functions instead of constants to define array range values but they
> have to be fully declared before I define the array - and this exposes
> that which I wish to remain private (the actual range).
>
> Isn't this a contradiction in terms, or _is_ there a way to retain
> 'Privacy'?

It seems to me that you have a design problem.

On the one hand, you expose to the client the fact that you model your
widget with an array of buttons; on the other hand you don't want your
client to know the bounds of the array.  I think there is a
contradiction inherent in this design.

You can resolve the contradiction in two ways:

1) expose the array type and allow the client to index into the array
directly. Prevent out-of-bounds indexes with strong typing:

package Keypad is
   ROWS_COUNT : constant := 2;
   COLUMNS_COUNT : constant := 2;

   type Row_Index is range 1 .. ROWS_COUNT;
   type Column_Index is range 1 .. COLUMNS_COUNT;
   type Keys_Type is array (Row_Index, Column_Index) of
     Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
end Keypad;

2) provide only an abstract data type where the only operations are in
terms of the semantics of each button; hide the fact that there is an
array behind the scenes.  If there is only one keypad, this is a
singleton, so you don't even need any type declaration in the spec:

with Gtk.Key_Button_Pkg;
package Keypad is
   function First_Button return
Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
   function Second_Button return
Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
   function Third_Button return
Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
   function Fourth_Button return
Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
end Keypad;

package body Keypad is
   ROWS_COUNT : constant := 2;
   COLUMNS_COUNT : constant := 2;

   type Row_Index is range 1 .. ROWS_COUNT;
   type Column_Index is range 1 .. COLUMNS_COUNT;
   Button : array (Row_Index, Column_Index) of
     Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;

   function First_Button return
Gtk.Key_Button_Pkg.Gtk_Key_Button_Access is
   begin
      return Button (1, 1);
   end First_Button;
   -- etc.
end Keypad;

(if there are several keypads, you declare an abstract data type as
usual:

type Keypad_Type (<>) is limited private;
function Construct return Keypad_Type;
function First_Button (KP : Keypad_Type)
  return Gtk.Key_Button_Pkg.Gtk_Key_Button_Access;
-- etc.
)

HTH

--
Ludovic Brenta.



  parent reply	other threads:[~2009-07-08 12:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-08  2:48 'private' and Privacy Rick
2009-07-08  2:48 ` stefan-lucks
2009-07-08  6:51 ` Gautier write-only
2009-07-08 12:47 ` Ludovic Brenta [this message]
2009-07-08 15:25 ` (see below)
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox