ExternalStructure.st
author Stefan Vogel <sv@exept.de>
Fri, 11 Apr 2014 10:29:03 +0200
changeset 16321 6e24f3c5ba96
parent 14726 af723d15ccf8
child 17455 374a772dfd00
child 18022 f23232c2eaef
permissions -rw-r--r--
class: Win32OperatingSystem changed: #selectOnAnyReadable:writable:exception:readableInto:writableInto:exceptionInto:withTimeOut: comment/format
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2006 by eXept Software AG
9350
7cdbea98454a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9343
diff changeset
     3
	      All Rights Reserved
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic' }"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
9350
7cdbea98454a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9343
diff changeset
    14
ExternalBytes subclass:#ExternalStructure
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:''
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'System-Support'
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
ExternalStructure class instanceVariableNames:'cType'
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
 No other class instance variables are inherited by this class.
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
!ExternalStructure class methodsFor:'documentation'!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
copyright
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 COPYRIGHT (c) 2006 by eXept Software AG
9350
7cdbea98454a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9343
diff changeset
    33
	      All Rights Reserved
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 This software is furnished under a license and may be used
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 only in accordance with the terms of that license and with the
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
 inclusion of the above copyright notice.   This software may not
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
 be provided or otherwise made available to, or used by, any
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
 other person.  No title to or ownership of the software is
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
 hereby transferred.
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
documentation
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    Instances of this class represent external (non-Smalltalk) data,
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    typically C-structs or C++ instances.
9350
7cdbea98454a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9343
diff changeset
    48
    They are only useful to represent handles as returned by C functions as smalltalk objects.
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    ExternallStructures are much like ExternalBytes - however, the latter
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    allow you to access bytes via indexed at:/at:put: messages, whilst instances of subclasses of me
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
    allow access via the corresponding getter/setter methods of my C-type.
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
14726
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    54
    Usage: normally (within exept), these are to be used with the ctype framework, which parses
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    55
    c header files and generates structure descriptions (ctypes).
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    56
    However, this class can also be used wthout such type descriptions, by subclassing it
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    57
    and redefining sizeof (to return the size in bytes of the structure) and getters 
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    58
    (as 'self longAt:idx', unsignedLOngAt:idx, etc.).
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    59
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    [author:]
14726
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    61
        Claus Gittinger
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    [see also:]
14726
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    64
        ExternalBytes ExternalAddress
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
    65
        ( how to write primitive code :html: programming/primitive.html )
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
"
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
! !
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
9387
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
    69
!ExternalStructure class methodsFor:'instance creation'!
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
    70
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
    71
fromExternalAddress:anExternalAddress
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
    72
    ^ self new fromExternalAddress:anExternalAddress
14725
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    73
!
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    74
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    75
new
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    76
    "allocate some memory usable for data;
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    77
     the memory is not controlled by the garbage collector.
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    78
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    79
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    80
     Use this, if you have to pass a block of bytes to some
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    81
     external destination (such as a C function) which does not copy the
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    82
     data, but instead keeps a reference to it. For example, many functions
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    83
     which expect strings simply keep a ref to the passed string - for those,
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    84
     an ST/X string-pointer is not the right thing to pass, since ST/X objects
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    85
     may change their address.
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    86
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    87
     DANGER ALERT: the memory is NOT automatically freed until it is either
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    88
                   MANUALLY freed (see #free) or the returned externalBytes object
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    89
                   is unprotected or the classes releaseAllMemory method is called."
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    90
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    91
    ^ super new:(self sizeof)
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    92
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    93
    "Modified: / 20-12-2010 / 16:22:51 / cg"
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    94
!
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    95
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    96
unprotectedNew
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    97
    "allocate some memory usable for data;
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    98
     the memory is under the control of the garbage collector.
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
    99
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   100
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   101
     DANGER ALERT: the memory block as allocated will be automatically freed
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   102
                   as soon as the reference to the returned externalBytes object
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   103
                   is gone (by the next garbage collect).
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   104
                   If the memory has been passed to a C-function which
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   105
                   remembers this pointer, bad things may happen ...."
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   106
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   107
    ^ super unprotectedNew:(self sizeof)
9387
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   108
! !
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   109
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
!ExternalStructure class methodsFor:'queries'!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
cType
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    ^ cType
14725
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   114
!
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   115
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   116
sizeof
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   117
    "the sizeof my instances in bytes"
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   118
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   119
    cType notNil ifTrue:[
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   120
        ^ cType sizeof
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   121
    ].
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   122
    self subclassResponsibility
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
! !
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
9387
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   125
!ExternalStructure methodsFor:'private'!
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   126
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   127
fromExternalAddress:anExternalAddress
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   128
    self setAddress:(anExternalAddress address) size:(anExternalAddress size).
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   129
! !
8784e3906d25 use fromExternalAddress to create structure.
ca
parents: 9350
diff changeset
   130
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
!ExternalStructure class methodsFor:'documentation'!
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
version
14726
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
   134
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStructure.st,v 1.5 2013-01-28 18:04:13 cg Exp $'
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
   135
!
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
   136
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
   137
version_CVS
af723d15ccf8 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 14725
diff changeset
   138
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStructure.st,v 1.5 2013-01-28 18:04:13 cg Exp $'
9343
0c2dcef80d03 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
! !
14725
76d8dc9a4dc6 class: ExternalStructure
Claus Gittinger <cg@exept.de>
parents: 9387
diff changeset
   140