22896
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
9343
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2006 by eXept Software AG
|
9350
|
5 |
All Rights Reserved
|
9343
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
17455
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
9350
|
18 |
ExternalBytes subclass:#ExternalStructure
|
9343
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'System-Support'
|
|
23 |
!
|
|
24 |
|
|
25 |
ExternalStructure class instanceVariableNames:'cType'
|
|
26 |
|
|
27 |
"
|
|
28 |
No other class instance variables are inherited by this class.
|
|
29 |
"
|
|
30 |
!
|
|
31 |
|
|
32 |
!ExternalStructure class methodsFor:'documentation'!
|
|
33 |
|
|
34 |
copyright
|
|
35 |
"
|
|
36 |
COPYRIGHT (c) 2006 by eXept Software AG
|
9350
|
37 |
All Rights Reserved
|
9343
|
38 |
|
|
39 |
This software is furnished under a license and may be used
|
|
40 |
only in accordance with the terms of that license and with the
|
|
41 |
inclusion of the above copyright notice. This software may not
|
|
42 |
be provided or otherwise made available to, or used by, any
|
|
43 |
other person. No title to or ownership of the software is
|
|
44 |
hereby transferred.
|
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
documentation
|
|
49 |
"
|
|
50 |
Instances of this class represent external (non-Smalltalk) data,
|
|
51 |
typically C-structs or C++ instances.
|
9350
|
52 |
They are only useful to represent handles as returned by C functions as smalltalk objects.
|
9343
|
53 |
|
|
54 |
ExternallStructures are much like ExternalBytes - however, the latter
|
|
55 |
allow you to access bytes via indexed at:/at:put: messages, whilst instances of subclasses of me
|
|
56 |
allow access via the corresponding getter/setter methods of my C-type.
|
|
57 |
|
21770
|
58 |
Usage: normally (within eXept), these are to be used with the ctype framework, which parses
|
14726
|
59 |
c header files and generates structure descriptions (ctypes).
|
21770
|
60 |
However, this class can also be used without such type descriptions, by subclassing it
|
|
61 |
and redefining sizeof (to return the size in bytes of the structure) and getters
|
14726
|
62 |
(as 'self longAt:idx', unsignedLOngAt:idx, etc.).
|
|
63 |
|
9343
|
64 |
[author:]
|
14726
|
65 |
Claus Gittinger
|
9343
|
66 |
|
|
67 |
[see also:]
|
14726
|
68 |
ExternalBytes ExternalAddress
|
|
69 |
( how to write primitive code :html: programming/primitive.html )
|
9343
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
9387
|
73 |
!ExternalStructure class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
fromExternalAddress:anExternalAddress
|
|
76 |
^ self new fromExternalAddress:anExternalAddress
|
14725
|
77 |
!
|
|
78 |
|
|
79 |
new
|
|
80 |
"allocate some memory usable for data;
|
|
81 |
the memory is not controlled by the garbage collector.
|
|
82 |
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
|
|
83 |
|
|
84 |
Use this, if you have to pass a block of bytes to some
|
|
85 |
external destination (such as a C function) which does not copy the
|
|
86 |
data, but instead keeps a reference to it. For example, many functions
|
|
87 |
which expect strings simply keep a ref to the passed string - for those,
|
|
88 |
an ST/X string-pointer is not the right thing to pass, since ST/X objects
|
|
89 |
may change their address.
|
|
90 |
|
|
91 |
DANGER ALERT: the memory is NOT automatically freed until it is either
|
|
92 |
MANUALLY freed (see #free) or the returned externalBytes object
|
|
93 |
is unprotected or the classes releaseAllMemory method is called."
|
|
94 |
|
|
95 |
^ super new:(self sizeof)
|
|
96 |
|
|
97 |
"Modified: / 20-12-2010 / 16:22:51 / cg"
|
|
98 |
!
|
|
99 |
|
|
100 |
unprotectedNew
|
|
101 |
"allocate some memory usable for data;
|
|
102 |
the memory is under the control of the garbage collector.
|
|
103 |
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
|
|
104 |
|
|
105 |
DANGER ALERT: the memory block as allocated will be automatically freed
|
|
106 |
as soon as the reference to the returned externalBytes object
|
|
107 |
is gone (by the next garbage collect).
|
|
108 |
If the memory has been passed to a C-function which
|
|
109 |
remembers this pointer, bad things may happen ...."
|
|
110 |
|
|
111 |
^ super unprotectedNew:(self sizeof)
|
9387
|
112 |
! !
|
|
113 |
|
22896
|
114 |
!ExternalStructure class methodsFor:'Squeak compatibility'!
|
|
115 |
|
|
116 |
externalNew
|
|
117 |
^ self new:self byteSize
|
|
118 |
! !
|
|
119 |
|
9343
|
120 |
!ExternalStructure class methodsFor:'queries'!
|
|
121 |
|
|
122 |
cType
|
|
123 |
^ cType
|
14725
|
124 |
!
|
|
125 |
|
17455
|
126 |
fields
|
|
127 |
"for Squeak compatibility support: may return a literal array structure definition,
|
|
128 |
from which the size and layout of the fields can be constructed.
|
|
129 |
Returns nil for ST/X subclasses, which redefine the sizeof method"
|
|
130 |
|
|
131 |
^ nil
|
|
132 |
!
|
|
133 |
|
14725
|
134 |
sizeof
|
|
135 |
"the sizeof my instances in bytes"
|
|
136 |
|
17455
|
137 |
cType isNil ifTrue:[
|
|
138 |
"/ second chance for Squeak external structures
|
|
139 |
self fields notNil ifTrue:[
|
|
140 |
self generateCTypeFromFieldDescription:self fields.
|
|
141 |
].
|
|
142 |
].
|
14725
|
143 |
cType notNil ifTrue:[
|
|
144 |
^ cType sizeof
|
|
145 |
].
|
|
146 |
self subclassResponsibility
|
9343
|
147 |
! !
|
|
148 |
|
17455
|
149 |
!ExternalStructure class methodsFor:'utilities'!
|
|
150 |
|
|
151 |
generateCTypeFromFieldDescription:fieldsSpec
|
|
152 |
"to support Squeak external structures, which define a fields method,
|
|
153 |
which returns a literal array spec of fields"
|
|
154 |
|
|
155 |
self halt.
|
|
156 |
! !
|
|
157 |
|
9387
|
158 |
!ExternalStructure methodsFor:'private'!
|
|
159 |
|
19418
|
160 |
fromExternalAddress:anExternalAddressOrExternalStructure
|
|
161 |
self
|
|
162 |
setAddress:(anExternalAddressOrExternalStructure address)
|
|
163 |
size:(anExternalAddressOrExternalStructure size).
|
9387
|
164 |
! !
|
|
165 |
|
19548
|
166 |
!ExternalStructure methodsFor:'testing'!
|
|
167 |
|
|
168 |
isExternalStructure
|
|
169 |
^ true
|
|
170 |
! !
|
|
171 |
|
9343
|
172 |
!ExternalStructure class methodsFor:'documentation'!
|
|
173 |
|
|
174 |
version
|
19418
|
175 |
^ '$Header$'
|
14726
|
176 |
!
|
|
177 |
|
|
178 |
version_CVS
|
19418
|
179 |
^ '$Header$'
|
9343
|
180 |
! !
|
14725
|
181 |
|