comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: converting pointer to value
Date: Thu, 4 Mar 2021 08:55:56 -0800 (PST)	[thread overview]
Message-ID: <02709d96-50fe-4e87-bdb5-4f430fa2717an@googlegroups.com> (raw)
In-Reply-To: <s1r05i$d1$1@dont-email.me>

On Thursday, March 4, 2021 at 8:59:48 AM UTC-7, björn lundin wrote:

> this code looks like 
> 
> type SQLSMALLINT is range -(2 ** 15) .. +(2 ** 15) - 1; 
> for SQLSMALLINT'Size use 16; 
> 
> type SQLRETURN is new SQLSMALLINT; 
> 
> 
> how to get the Pvalue as an SQLRETURN? 

Ok, in Ada the creation of a new type, even if only deriving it via NEW, makes a type which is incompatible with the old type.
Thus you can say TYPE TEMPERATURE IS NEW INTEGER; and TYPE SPEED IS NEW INTEGER; and values of these types are NOT interchangeable even though both have the same internal representation and properties that INTEGER does. The way that you can use these different types together is by casting; given X : SPEED := 3; you can say Y :  TEMPERATURE := SPEED(X); -- which works because they have the same internal representation if they did not have the same representation you would have to use UNCHECKED_CONVERSION.

An example where you might need differing representations is modeling the x86 -- you could have something like:
Type Integer_Register is new Integer; -- IIRC, AX is 16-bits, divided into bytes AH and AL.
Type HL_Byte_Register is record
  High, Low : Interfaces.Unsigned_8;
end record;

AX : Integer_Register:= 16#EF10#;
...
Declare
  Function Convert is new Ada.Unchecked_Conversion(Integer_Register, HL_Byte_Register);
  Function Convert is new Ada.Unchecked_Conversion(HL_Byte_Register, Integer_Register);
  Temp : HL_Byte_Register := convert( AX );
Begin
  -- Stuff w/ bytes.
  AX:= Convert( Temp );
End;

In the code you present, SQLRETURN derives from SQLSMALLINT, so you can say SQLSMALLINT( Return_Value )... yes, I know this isn't pvalue as sqlreturn, we'll get there.

> 
> function SQLParamData (StatementHandle : SQLHSTMT; 
> pValue : access SQLPOINTER) 
> return SQLRETURN; 
> 

So this returns a number, of the same representation that the 16-bit SQLSMALLINT has.

> so I get back the column in pValue. 
> pValue is declared as 
> Pvalue : aliased Sqlpointer; 
...
> type SQLPOINTER is private;
....
> private
> type SQLPOINTER is new System.Storage_Elements.Integer_Address;

And here we have SQLPOINTER, which is private, but which is something of the same representation that Integer_Address has; let's assume that the ADDRESS type is implementation-defined, and a private type.
What does this mean?
It means that we don't know what the size of ADDRESS (and thus SQLPOINTER) actually is, but assuming you're on a 32- or 64-bit machine it's likely 2 to 4 times as large as the 16-bit SQLSMALLINT-- which is a good indication that a simple UNCHECKED_CONVERSION is the wrong answer.

Try looking at the operations which have SQLPOINTER as a parameter/result.

  parent reply	other threads:[~2021-03-04 16:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-04 15:59 converting pointer to value Björn Lundin
2021-03-04 16:50 ` Dmitry A. Kazakov
2021-03-05  7:06   ` Björn Lundin
2021-03-05  7:44     ` Dmitry A. Kazakov
2021-03-05  9:10       ` Björn Lundin
2021-03-04 16:55 ` Shark8 [this message]
2021-03-04 17:35   ` Dmitry A. Kazakov
2021-03-04 19:38     ` Shark8
2021-03-04 21:27       ` Dmitry A. Kazakov
2021-03-05  8:58         ` Björn Lundin
2021-03-05  8:54     ` Björn Lundin
2021-03-05 11:02     ` Björn Lundin
2021-03-05 11:57       ` Björn Lundin
2021-03-05 14:00         ` Dmitry A. Kazakov
2021-03-09 12:07     ` [SOLVED] " Björn Lundin
2021-03-04 20:09   ` Simon Wright
2021-03-04 21:00     ` Shark8
2021-03-05  8:59       ` Björn Lundin
2021-03-05  7:10   ` Björn Lundin
replies disabled

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