C Pointer Access question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

C Pointer Access question

mario bancos
Hi,

I'm wondering if there's any Class to map an ExternalAddress to a
"PointerClass" of any ExternalStructure, like the CPointerType of
VisualWorks.

ie.

ba := ByteArray new: 4.
dwordpointer := PointerClass fromAddress: ba yourAddress type: DWORD.
dwordPointer at: 1. "Read 4 bytes"
wordpointer := PointerClass fromAddress: ba yourAddress type: WORD.
wordPointer at: 1. "Read 2 bytes"
wordPointer at: 2. "Read 2 bytes"


Thanks


Reply | Threaded
Open this post in threaded view
|

Re: C Pointer Access question

Bill Schwab-2
Mario,

I think the answer will turn out to be that Dolphin handles this in part by
the base class (ExternalStructure, ExternalAddress), in part by the class
method used to instantiate the object, and finally by passing external
objects by reference.  The latter point works to advantage in most
situations, and makes a few things quite confusing.  For the rest, take a
look at

  http://www.object-arts.co.uk/wiki/html/Dolphin/ExternalInterfacing.htm

Does that help?

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: C Pointer Access question

Blair McGlashan
In reply to this post by mario bancos
"mario bancos" <[hidden email]> wrote in message
news:[hidden email]...
> Hi,
>
> I'm wondering if there's any Class to map an ExternalAddress to a
> "PointerClass" of any ExternalStructure, like the CPointerType of
> VisualWorks.
>

In Dolphin ExternalStructures can be either 'value' (internal ByteArray) or
'reference' (pointer) instances.

> ie.
>
> ba := ByteArray new: 4.
> dwordpointer := PointerClass fromAddress: ba yourAddress type: DWORD.
> dwordPointer at: 1. "Read 4 bytes"
> wordpointer := PointerClass fromAddress: ba yourAddress type: WORD.
> wordPointer at: 1. "Read 2 bytes"
> wordPointer at: 2. "Read 2 bytes"

This is a slightly different thing - C confuses pointers and arrays, but you
don't really want pointers here, since then you lose important information
like the length. In Dolphin you would need to use a subclass of
ExternalArray. There is a generic one of these, StructureArray, which should
work for any structure, and some specialized ones that offer higher
performance for a limited range of types. There is also a slight difference
in behaviour which you will notice if you evaluate the examples below. For
example:

 ba := ByteArray new: 4.
 dwords := StructureArray fromAddress: ba yourAddress length: 1
elementClass: DWORD.
 dwords at: 1. "Read 4 bytes - returned as an instance of DWORD that
references the original data"
 words := StructureArray fromAddress: ba yourAddress length: 2 elementClass:
WORD.
 words at: 1. "Read 2 bytes - returned as an instance of WORD that
references the original data"
 words at: 2. "Read 2 bytes - ditto"

You must specify the length when you create the array. This allows for
bounds checking on indexed access, and also allows ExternalArray to
implement useful parts of the collection protocol such as do:, select: and
collect:.

Or:

The most direct way to rewrite your code would be:

dwords := DWORDArray new: 1.
dwords at: 1. "Read 4 bytes - returned as an integer"
words := WORDArray new: 2.
 words at: 1. "Read 2 bytes - returned as an integer"
 words at: 2. "Read 2 bytes - returned as an integer"


Regards

Blair