comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada/Motif problem...
  1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
@ 1996-08-27  0:00 ` Dale Stanbrough
  1996-08-27  0:00   ` Michael K Rohan
  1996-08-27  0:00 ` Larry Kilgallen
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Dale Stanbrough @ 1996-08-27  0:00 UTC (permalink / raw)



Larry Kilgallen writes:

"Understanding that you have asked us how to lie to an Ada compiler...

> Any inspiration on how to pass Ada strings as callback data in Motif?

Depending on compiler specifics, you may be able to get away with
an access to a single character (the first in the string).

Larry Kilgallen"

But that doesn't really help at the other end, that is the routine that
gets called and gets passed a pointer to a character. It may know that
it points to the first character of a string, but how should it know how
long the string is? We lose the bounds of the string if we do this.


Dale




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Ada/Motif problem...
@ 1996-08-27  0:00 Dale Stanbrough
  1996-08-27  0:00 ` Dale Stanbrough
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Dale Stanbrough @ 1996-08-27  0:00 UTC (permalink / raw)



Hi,

One of our students has run into problems writing a Motif routine.
The callback data is to be a string as follows..


Xt.Intrinsic.XtAddCallback(
                                 pb,
                                 Motif.xmstrdefs.XmNactivateCallback,
                                 hello_dialog_pkg.popup'access,
                                 "Hello World");
                                 

I've tried using unchecked_conversion from an access to all string
to XtPointer, but Gnat complains (quite rightly) about these pointer
types being of different sizes.

Any inspiration on how to pass Ada strings as callback data in Motif?

Here's the subprogram declaration...
                                 
procedure XtAddCallback(
                widget       : Xt.Intrinsic.Widget;
                callback_name: X.Strings.const_charp;
                callback     : XtCallbackProc;
                closure      : XtPointer);

Dale




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
  1996-08-27  0:00 ` Dale Stanbrough
@ 1996-08-27  0:00 ` Larry Kilgallen
  1996-08-28  0:00 ` Jon S Anthony
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Larry Kilgallen @ 1996-08-27  0:00 UTC (permalink / raw)



In article <4vupg6$ljf@goanna.cs.rmit.edu.au>, Dale Stanbrough <dale@goanna.cs.rmit.EDU.AU> writes:

Understanding that you have asked us how to lie to an Ada compiler...

> Any inspiration on how to pass Ada strings as callback data in Motif?

Depending on compiler specifics, you may be able to get away with
an access to a single character (the first in the string).

Larry Kilgallen




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00 ` Dale Stanbrough
@ 1996-08-27  0:00   ` Michael K Rohan
  1996-08-28  0:00     ` Larry Kilgallen
  0 siblings, 1 reply; 13+ messages in thread
From: Michael K Rohan @ 1996-08-27  0:00 UTC (permalink / raw)



Dale Stanbrough wrote:
> 
> Larry Kilgallen writes:
> 
> "Understanding that you have asked us how to lie to an Ada compiler...
> 
> > Any inspiration on how to pass Ada strings as callback data in Motif?
> 
> Depending on compiler specifics, you may be able to get away with
> an access to a single character (the first in the string).
> 
> Larry Kilgallen"
> 
> But that doesn't really help at the other end, that is the routine that
> gets called and gets passed a pointer to a character. It may know that
> it points to the first character of a string, but how should it know how
> long the string is? We lose the bounds of the string if we do this.
> 
> Dale

Hi,

I am currently learning Motif programming and Ada95 together by going
thru
Power Programming...Motif and implementing the examples in Ada95.  The
pullmenu example of Chapter 4, passes strings as client data.

This response is rather long, but the solution I came up with was to use
a another type (Label) with the unchecked conversion routines.  The
callback spec is

    --
    -- Callbacks used for the PullMenu example program
    --

    with Ada.Unchecked_Conversion;
    with Xt.Intrinsic;

    package Callbacks is

        --
        -- Exception to ``break-out'' of the X event loop
        Finished : exception;

        type Label (L : Positive) is
            record
                Text : String(1..L);
            end record;
        --
        -- Type used for client argument to callbacks (strings)
        type Label_Pointer is access all Label;

        --
        -- Conversion functions to/from Label_Pointer/XtPointer
        function Label_Pointer_To_XtPointer is
            new Ada.Unchecked_Conversion(Label_Pointer,
Xt.Intrinsic.XtPointer);
        function XtPointer_To_Label_Pointer is
            new Ada.Unchecked_Conversion(Xt.Intrinsic.XtPointer,
Label_Pointer);

        --
        -- The exit callback
        procedure Exit_Callback (Widget      : Xt.Intrinsic.Widget;
                                 Client_Data : Xt.Intrinsic.XtPointer;
                                 Call_Data   : Xt.Intrinsic.XtPointer);

        --
        -- The generic callback used for all other callbacks in the
program
        procedure Generic_Callback (Widget : Xt.Intrinsic.Widget;
                                    Client_Data :
Xt.Intrinsic.XtPointer;
                                    Call_Data   :
Xt.Intrinsic.XtPointer);

    private

        pragma Convention(C, Exit_Callback);
        pragma Convention(C, Generic_Callback);

    end Callbacks;

with body

    --
    -- Callbacks used for the Pull_Menu example program
    --

    with Ada.Text_IO;

    package body Callbacks is

        --
        -- The exit callback
        procedure Exit_Callback (Widget      : Xt.Intrinsic.Widget;
                                 Client_Data : Xt.Intrinsic.XtPointer;
                                 Call_Data   : Xt.Intrinsic.XtPointer)
is
        begin
            Generic_Callback(Widget, Client_Data, Call_Data);
            raise Finished;
        end Exit_Callback;

        --
        -- The generic callback used for all other callbacks in the
program
        procedure Generic_Callback (Widget      : Xt.Intrinsic.Widget;
                                    Client_Data :
Xt.Intrinsic.XtPointer;
                                    Call_Data   :
Xt.Intrinsic.XtPointer) is
            L : Label_Pointer :=
XtPointer_To_Label_Pointer(Client_Data);
        begin
            Ada.Text_IO.Put_Line("Menu callback for [" & L.Text & "]");
            Ada.Text_IO.Flush;
        end Generic_Callback;

    end Callbacks;

The main program (Utilities.Create_Push_Button calls XtAddCallback),
abbreviated is,

    --
    -- Main program for the ``Power Programming...Motif'' pullmenu
example
    -- program of Chapter 4.
    --

    procedure PullMenu is

        use Motif.XmStrDefs;
        use Motif.Ada_Support.Stdarg;

        --
        -- Utility routine to generate a Label_Pointer from a string.
        -- Label_Pointer's are passed to the callbacks as the client
data
        function "+" (S: String) return Xt.Intrinsic.XtPointer is
            Result : Callbacks.Label_Pointer := new
Callbacks.Label(S'Length);
        begin
            Result.Text := S;
            return Callbacks.Label_Pointer_To_XtPointer(Result);
        end "+";

        .
        .

    begin
        .
        .

        -- Set up menu choices for File menu.
        W := Utilities.Create_Push_Button(File_Menu, "new",
                                         
Callbacks.Generic_Callback'access,
                                          +"New");
        .
        .
    exception
        when Callbacks.Finished => null;
    end PullMenu;

Have fun,
Michael.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
  1996-08-27  0:00 ` Dale Stanbrough
  1996-08-27  0:00 ` Larry Kilgallen
@ 1996-08-28  0:00 ` Jon S Anthony
  1996-08-28  0:00 ` David C. Hoos, Sr.
  1996-08-29  0:00 ` Jon S Anthony
  4 siblings, 0 replies; 13+ messages in thread
From: Jon S Anthony @ 1996-08-28  0:00 UTC (permalink / raw)



In article <4vupg6$ljf@goanna.cs.rmit.edu.au> Dale Stanbrough <dale@goanna.cs.rmit.EDU.AU> writes:

> Hi,
> 
> One of our students has run into problems writing a Motif routine.
> The callback data is to be a string as follows..
> 
> 
> Xt.Intrinsic.XtAddCallback(
>                                  pb,
>                                  Motif.xmstrdefs.XmNactivateCallback,
>                                  hello_dialog_pkg.popup'access,
>                                  "Hello World");

You appear to be using the Ada95 bindings.  Good idea.  The client
data for XtAddCallback is not particularly well defined - in C land it
is something that fits in [pointer_size bits] which _typically_ means
it can be either a pointer or something like an integer.  The bindings
Xt.Intrinsic.XtPointer kinda sorta captures this by being a pointer to
a "null record" (a kinda sorta general undefined thingy).  In your
case it is clearly a pointer that is expected, but not one to
XtPointerRec (the designated type of XtPointer).  As you point out,
you can't really use access all String since String is unconstrained.
You could use an access all to some _constrained_ string.  But IMO that
sucks for various reasons, not the least of which is it is not flexible.

So, what you really want is just a "refinement" of XtPointerRec:

    subtype Call_Data_Type is Xt.Intrinsic.XtPointer;

    -- The information impl...
    --
    type Stg_Ref is access all String;
 
    type Info_Type is record
        Msg : Stg_Ref;
    end record;
 
    type Info_Ref is access all Info_Type;
 
    function To_CDT is new Unchecked_Conversion
        (Source => Info_Ref, Target => Call_Data_Type );
 
    msg  : aliased String := "The message";
    Info : aliased Info_Type := (Msg => Msg'Access);

    ...
    hello_dialog_pkg.popup'access,
    To_CDT(Info'Access));

This is much nicer since Info_Rec can contain anything and can be changed
to contain anything without having to rehack the conversions and whatnot
for the new information...

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
                   ` (2 preceding siblings ...)
  1996-08-28  0:00 ` Jon S Anthony
@ 1996-08-28  0:00 ` David C. Hoos, Sr.
  1996-08-29  0:00 ` Jon S Anthony
  4 siblings, 0 replies; 13+ messages in thread
From: David C. Hoos, Sr. @ 1996-08-28  0:00 UTC (permalink / raw)



Hi Dale,
It appears that your problem is passing an Ada string where an XtPointer is
required.  This confusion may result from the fact that in C a literal
string is stored as a null-terminated static string in a memory location
determined by the compiler/linker, and that the value of that literal
string expression is its address.  Further, if you are compiling the C
without strict ANSI constraints, that pointer to char will be accepted as a
valid XtPointer!
I haven't worked with these X11Ada bindings yet, but I would suggest the
string "Hello World" & Ascii.NUL be assigned to an object declared as
aliased -- e.g.,
Hello_World : aliased constant STRING := "Hello World" & Ascii.NUL;

Then I would pass the value Hello_World'ACCESS as the parameter.

The reason I am suggesting the explicit NUL termination for the string, is
that that's what the X11 library, written in C expects.  There may be some
fancier way with some of the packages in the Ada95 annexes, but I haven't
learned enough about them yet.

Hope this helps

-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

Dale Stanbrough <dale@goanna.cs.rmit.EDU.AU> wrote in article
<4vupg6$ljf@goanna.cs.rmit.edu.au>...
> Hi,
> 
> One of our students has run into problems writing a Motif routine.
> The callback data is to be a string as follows..
> 
> 
> Xt.Intrinsic.XtAddCallback(
>                                  pb,
>                                  Motif.xmstrdefs.XmNactivateCallback,
>                                  hello_dialog_pkg.popup'access,
>                                  "Hello World");
>                                  
> 
> I've tried using unchecked_conversion from an access to all string
> to XtPointer, but Gnat complains (quite rightly) about these pointer
> types being of different sizes.
> 
> Any inspiration on how to pass Ada strings as callback data in Motif?
> 
> Here's the subprogram declaration...
>                                  
> procedure XtAddCallback(
>                 widget       : Xt.Intrinsic.Widget;
>                 callback_name: X.Strings.const_charp;
>                 callback     : XtCallbackProc;
>                 closure      : XtPointer);
> 
> Dale
> 




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00   ` Michael K Rohan
@ 1996-08-28  0:00     ` Larry Kilgallen
  1996-08-29  0:00       ` Paul Hussein
  0 siblings, 1 reply; 13+ messages in thread
From: Larry Kilgallen @ 1996-08-28  0:00 UTC (permalink / raw)




 Dale Stanbrough wrote:
> 
> Larry Kilgallen writes:
> 
> "Understanding that you have asked us how to lie to an Ada compiler...
> 
> > Any inspiration on how to pass Ada strings as callback data in Motif?
> 
> Depending on compiler specifics, you may be able to get away with
> an access to a single character (the first in the string).
> 
> Larry Kilgallen"
> 
> But that doesn't really help at the other end, that is the routine that
> gets called and gets passed a pointer to a character. It may know that
> it points to the first character of a string, but how should it know how
> long the string is? We lose the bounds of the string if we do this.

In my experience calling DECwindows from Ada, it wanted a lot of
null-terminated strings.  Thus the called subsystem, being written
in C, cannot tell the difference between a pointer to null-terminated
string and a pointer to the first character thereof.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
@ 1996-08-28  0:00 G. Vincent Castellano
  0 siblings, 0 replies; 13+ messages in thread
From: G. Vincent Castellano @ 1996-08-28  0:00 UTC (permalink / raw)



> One of our students has run into problems writing a Motif routine.
> The callback data is to be a string as follows..
>
> Xt.Intrinsic.XtAddCallback(
>                                  pb,
>                                  Motif.xmstrdefs.XmNactivateCallback,
>                                  hello_dialog_pkg.popup'access,
>                                  "Hello World");

The specification of this routine is:

    procedure XtAddCallback(
                widget       : Xt.Intrinsic.Widget;
                callback_name: X.Strings.const_charp;
                callback     : XtCallbackProc;
                closure      : XtPointer);

The type XtPointer is not really a useful one, being a
private (access) type with no operations for setting it.
You are obliged to use Unchecked_Conversion to convert to
and from it.

A good choice in this case would be String_Access from the
package Ada.Strings.Unbounded.  (Any access type will
probably work, since access types are generally all the
same size.)

  function To_XtPointer is new Unchecked_Conversion(
              Source => Ada.Strings.Unbounded.String_Access,
              Target => Xt.Intrinsic.XtPointer );

  Hi_String:  constant Ada.Strings.Unbounded.String_Access
              := new String'("Hello World");

-- ...

  Xt.Intrinsic.XtAddCallback(    pb,
                                 Motif.xmstrdefs.XmNactivateCallback,
                                 hello_dialog_pkg.popup'access,
                                 To_XtPointer( Hi_String ) );

BUT:  be careful that the object you're specifying as callback
data is not local to a subprogram, else it will be garbage by
the time the callback is invoked.  Declare it in a package spec
or body to be safe.

Also consider whether callback data is really useful.  If it's
never going to change, you probably don't need it.  I prefer to
play it safe and design callbacks so that only integers are
passed around, which I can then use to index into a table.
 -----------------------------------------------------------------------
 -    G. Vincent Castellano, Sr. Software Engineer, OC Systems Inc     -
 -   gvc@ocsystems.com :: X/Ada WWW => http://www.ocsystems.com/xada   -
 -----------------------------------------------------------------------
 - "If virtual memory did not exist, it would                          -
 -   have become necessary for us to invent it."                       -
 -----------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem
@ 1996-08-28  0:00 W. Wesley Groleau (Wes)
  0 siblings, 0 replies; 13+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-08-28  0:00 UTC (permalink / raw)



If the Motif libs were compiled with gcc then the following should work
with GNAT:

   HW : constant String := "Hello World" & ASCII.NUL;

   begin

      Xt.Intrinsic.XtAddCallback ( pb,
                                   Motif.et_cetera,
                                   hello.et_cetera'access,
                                   HW'Address             );

as long as you ensure that HW is ALWAYS in scope.

I've obviously never tried it with Ada-83, but that method for
sending a string to C works with several C compilers and Ada-83 compilers.

OOPS, I just realized you're trying to send it BACK to Ada.  Yes, you lose
bounds info, but you can sort of get them back as follows:

in callback:

   Local_HW : contant String := String_From_C ( the_thing_that_came_from_C );

where String_From_C is provided (with a different name) by most vendors,
but is basically

   Temp : String ( 1 .. some_arbitrary_limit );
   for Temp'Address use the_thing_that_came_from_C;

...

   for I in Temp'Range loop
      if Temp(I) = ASCII.NULL then
         return Temp ( Temp'First .. I-1 );
      end if;
   end loop;

---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Hughes Defense Communications (MS 10-40)                 Home: 219-471-7206
Fort Wayne,  IN   46808                  (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-28  0:00     ` Larry Kilgallen
@ 1996-08-29  0:00       ` Paul Hussein
  0 siblings, 0 replies; 13+ messages in thread
From: Paul Hussein @ 1996-08-29  0:00 UTC (permalink / raw)



kilgallen@eisner.decus.org (Larry Kilgallen) wrote:


> Dale Stanbrough wrote:
>> 
>> Larry Kilgallen writes:
>> 
>> "Understanding that you have asked us how to lie to an Ada compiler...
>> 
>> > Any inspiration on how to pass Ada strings as callback data in Motif?
>> 
>> Depending on compiler specifics, you may be able to get away with
>> an access to a single character (the first in the string).
>> 
>> Larry Kilgallen"
>> 
>> But that doesn't really help at the other end, that is the routine that
>> gets called and gets passed a pointer to a character. It may know that
>> it points to the first character of a string, but how should it know how
>> long the string is? We lose the bounds of the string if we do this.

>In my experience calling DECwindows from Ada, it wanted a lot of
>null-terminated strings.  Thus the called subsystem, being written
>in C, cannot tell the difference between a pointer to null-terminated
>string and a pointer to the first character thereof.


Spot on! When passing strings from Ada to C one needs to
null-terminate them. The best Idea is to write yourself a little
package the does C conversions and declaraarions much like the ones
that come with DEC Ada or VADS Ada.


package C_Types is

   type Long is ...
   type Int is ...
   type 

   etc
end C_Types; 


package C_Strings is

   type Null_Terminated_String is new STRING;

   type Char_Pointer is new SYSTEM.ADDRESS;
   Null_Char_Pointer : constant Char_Pointer := SYSTEM.NULL_ADDRESS;

   function To_String ( String : in STRING ) return
Null_Terminated_String;
  function To_String ( String : in Null_Terminated_String ) return
Null_Terminated_String;

   etc.
end C_Strings.



   Where what you pass to C would go something like this.


   Null_String              : constant
C_Strings.Null_Terminated_String := C_Strings.To_String ("Hello
Baby");
   String_Pointer : C_Strings.Char_Pointer :=
Null_String'FIRST'ADDRESS;



   Calback ( ......

                  String => String_Pointer
                   .... )


I think this is corrrect.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
                   ` (3 preceding siblings ...)
  1996-08-28  0:00 ` David C. Hoos, Sr.
@ 1996-08-29  0:00 ` Jon S Anthony
  1996-08-29  0:00   ` Dale Stanbrough
  4 siblings, 1 reply; 13+ messages in thread
From: Jon S Anthony @ 1996-08-29  0:00 UTC (permalink / raw)



In article <503hsp$be2@romeo.logica.co.uk> husseinp@logica.com (Paul Hussein) writes:

> Spot on! When passing strings from Ada to C one needs to
> null-terminate them. The best Idea is to write yourself a little
> package the does C conversions and declaraarions much like the ones
> that come with DEC Ada or VADS Ada.

Nah.  The best idea is to start using Ada95 and use the _standard_ out
of the box interoperability of Interfaces.C.Strings which does all this
(and more).

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-29  0:00 ` Jon S Anthony
@ 1996-08-29  0:00   ` Dale Stanbrough
  1996-09-02  0:00     ` Sandy McPherson
  0 siblings, 1 reply; 13+ messages in thread
From: Dale Stanbrough @ 1996-08-29  0:00 UTC (permalink / raw)



"Nah.  The best idea is to start using Ada95 and use the _standard_ out
 of the box interoperability of Interfaces.C.Strings which does all this
 (and more)."
 

Interfaces.C.Strings lets me create a copy of the Ada string, and pass
a pointer to that copy. The callback can then make an Ada string from this
copy. This all seems rather expensive, as well as only being by value.

Is there any way to pass the string _by reference_ in a manner that is
reasonably portable (even with the use of unchecked conversion). I'm 
really looking for a way of getting a pointer to the string somehow
that is compatable with size of C pointers.


Dale




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada/Motif problem...
  1996-08-29  0:00   ` Dale Stanbrough
@ 1996-09-02  0:00     ` Sandy McPherson
  0 siblings, 0 replies; 13+ messages in thread
From: Sandy McPherson @ 1996-09-02  0:00 UTC (permalink / raw)
  Cc: Dale Stanbrough


Dale Stanbrough wrote:
> 
> "Nah.  The best idea is to start using Ada95 and use the _standard_ out
>  of the box interoperability of Interfaces.C.Strings which does all this
>  (and more)."
> 
> 
> Interfaces.C.Strings lets me create a copy of the Ada string, and pass
> a pointer to that copy. The callback can then make an Ada string from this
> copy. This all seems rather expensive, as well as only being by value.
> 
> Is there any way to pass the string _by reference_ in a manner that is
> reasonably portable (even with the use of unchecked conversion). I'm
> really looking for a way of getting a pointer to the string somehow
> that is compatable with size of C pointers.
> 
> Dale

This however misses an important point. You don't need to covert to/from
C compatible strings to pass data to a callback, the user data passed to
the AddCallback function is not used by Motif it is simply passed
through, to the callback. Motif does not use this data. Most of the
previous posters immediatley assumed that you were in a
SetValues/GetValues context, where the string needs to/will be an ASCIZ
(null terminated ascii string). 

The type XtPointer is equivalent to an address. It is easy to convert a
string to its address, convert that to XtPointer using
UNCHECKED_CONVERSION and then pass it to the AddCallback function. The
problem however is how you convert back from the address in the callback
and it is different for constrained and unconstrained types.

The simplest method is to use/create a type which is constrained and
known to both the caller of XtAddCallback and to the callback itself.
This is because you have to get back to where you began, which is easy
if you have a constrained type, but a bit more complicated if it is
unconstrained: normally constrained array types are passed by reference
and the implementation of the access type is purely an address, whereas
unconstrained types are usually passed by a more complex method and the
access type is analogous. In Ada'95 converting  string access ->
XtPointer -> string access, using UNCHECKED_CONVERSION should be trivial
I tried to do this in Ada a few years ago and failed miserably because
it was not possible to use an access type on a statically declared
object, so I had to resort to buggering around with addresses to get the
desired effect. (However, at this point we decided to write our GUI
control software in C and the rest of the application in Ada)

I have a feeling however if one does (something along the lines of) the
following, it might be possible to handle the unconstrained strings by
converting the address (or the access type) of the unconstrained string
access to an XtPointer (I assume this is an access type which is simply
an address, otherwise Motif would choke on it).

type USTRING_ACCESS is access STRING;
function TO_XT_POINTER( P: SYSTEM'ADDRESS ) return XtPointer is new
UNCHECKED_CONVERSION;
function FROM_XT_POINTER( P: XtPointer ) return SYSTEM'ADDRESS is new
UNCHECKED_CONVERSION;

in the function calling AddCallback you can do 

hellod : USTRING_ACCESS := "Hello World"'ACESSS;
ptr : XtPointer := TO_XT_POINTER( hellod'ADDRESS );

and in the callback fix the address of a USTRING_ACCESS variable at the
address given by the XtPointer

hellod : USTRING_ACCESS;
for hellod at FROM_XT_POINTER( ptr );

This is really horrific I know, but it is a fact of life that C and Ada
do not mix well.

If multiple indirection is possible using

type USTRING_ACCESS is access STRING;
type USTRING_ACCESS_ACCESS is access USTRING_ACCESS;
function TO_XT_POINTER( P: USTRING_ACCESS_ACCESS ) return XtPointer is
new UNCHECKED_CONVERSION;
function FROM_XT_POINTER( P: XtPointer ) return USTRING_ACCESS_ACCESS is
new UNCHECKED_CONVERSION;

and USTRING_ACCESS_ACCESS reduces to an address then you might be able
to construct something more elegant! e.g.

hellod : USTRING_ACCESS := "Hello World"'ACCESS;
ptr : XtPointer := TO_XT_POINTER( hellod'ACCESS );

and

hellod : USTRING_ACCESS = FROM_XT_POINTER( ptr );

I do not have access to an Ada compiler, or a reference manual, so I
take no responsibility for any (obvious) flaws, and I have never used
Ada'95 so I'm not sure of the syntax for dereferencing access type.
Anyone brave enough to try this will just have to "suck it and see".

"Loud clearing of throat follows"

Of course having shown it is not necessary to null terminate the string
to have it passed to the callback, you have to null terminate it within
the callback if you wish to do anything useful with it: i.e. pass it to
a Motif function.
-- 
Sandy McPherson	MBCS CEng.	tel: 	+31 71 565 4288 (w)
ESTEC/WAS
P.O. Box 299
NL-2200AG Noordwijk




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~1996-09-02  0:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
1996-08-27  0:00 ` Dale Stanbrough
1996-08-27  0:00   ` Michael K Rohan
1996-08-28  0:00     ` Larry Kilgallen
1996-08-29  0:00       ` Paul Hussein
1996-08-27  0:00 ` Larry Kilgallen
1996-08-28  0:00 ` Jon S Anthony
1996-08-28  0:00 ` David C. Hoos, Sr.
1996-08-29  0:00 ` Jon S Anthony
1996-08-29  0:00   ` Dale Stanbrough
1996-09-02  0:00     ` Sandy McPherson
  -- strict thread matches above, loose matches on Subject: below --
1996-08-28  0:00 G. Vincent Castellano
1996-08-28  0:00 W. Wesley Groleau (Wes)

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