ExternalInt.st
changeset 4576 abbfe9f91ef1
equal deleted inserted replaced
4575:d34168bc0691 4576:abbfe9f91ef1
       
     1 "
       
     2  COPYRIGHT (c) 2018 by eXept Software AG
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 "{ Package: 'stx:libbasic2' }"
       
    13 
       
    14 "{ NameSpace: Smalltalk }"
       
    15 
       
    16 ExternalBytes subclass:#ExternalInt
       
    17 	instanceVariableNames:''
       
    18 	classVariableNames:''
       
    19 	poolDictionaries:''
       
    20 	category:'System-Support'
       
    21 !
       
    22 
       
    23 !ExternalInt class methodsFor:'documentation'!
       
    24 
       
    25 copyright
       
    26 "
       
    27  COPYRIGHT (c) 2018 by eXept Software AG
       
    28               All Rights Reserved
       
    29 
       
    30  This software is furnished under a license and may be used
       
    31  only in accordance with the terms of that license and with the
       
    32  inclusion of the above copyright notice.   This software may not
       
    33  be provided or otherwise made available to, or used by, any
       
    34  other person.  No title to or ownership of the software is
       
    35  hereby transferred.
       
    36 "
       
    37 !
       
    38 
       
    39 documentation
       
    40 "
       
    41     Instances of me can be used as container in which int values are returned from C-calls. 
       
    42     Notice that the size of these ints depends on the machine's (and compiler's) int-size
       
    43     (which is ExternalBytes sizeofInt).
       
    44 "
       
    45 ! !
       
    46 
       
    47 !ExternalInt class methodsFor:'instance creation'!
       
    48 
       
    49 new
       
    50     "allocate some memory usable to hold a C-int;
       
    51      the memory is not controlled by the garbage collector.
       
    52      Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
       
    53 
       
    54      Use this, if you have to pass a pointer to an integer
       
    55      external destination (such as a C function) which does not copy the
       
    56      data, but instead keeps a reference to it.
       
    57 
       
    58      DANGER ALERT: the memory is NOT automatically freed until it is either
       
    59                    MANUALLY freed (see #free) or the returned externalBytes object
       
    60                    is unprotected or the classes releaseAllMemory method is called."
       
    61 
       
    62     ^ super new:(ExternalBytes sizeofInt)
       
    63 
       
    64     "
       
    65      ExternalInt new
       
    66     "
       
    67 !
       
    68 
       
    69 unprotectedNew
       
    70     "allocate some memory usable to hold a naitve C-int;
       
    71      the memory is under the control of the garbage collector.
       
    72      Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
       
    73 
       
    74      DANGER ALERT: the memory block as allocated will be automatically freed
       
    75                    as soon as the reference to the returned externalBytes object
       
    76                    is gone (by the next garbage collect).
       
    77                    If the memory has been passed to a C-function which
       
    78                    remembers this pointer, bad things may happen ...."
       
    79 
       
    80     ^ super unprotectedNew:(ExternalBytes sizeofInt)
       
    81 
       
    82     "
       
    83      ExternalInt new
       
    84     "
       
    85 ! !
       
    86 
       
    87 !ExternalInt methodsFor:'accessing'!
       
    88 
       
    89 asExternalBytes
       
    90     ^ ExternalBytes address:(self value)
       
    91 
       
    92     "
       
    93      (ExternalLong new value:10) asExternalBytes
       
    94      (ExternalLong new value:0) asExternalBytes
       
    95     "
       
    96 !
       
    97 
       
    98 asInteger
       
    99     "warning: retrieves a signed integer with the size of the native machine's
       
   100      pointer (i.e. either 32 or 64bit)"
       
   101 
       
   102     ExternalBytes sizeofInt == 8 ifTrue:[
       
   103         ^ self signedInt64At:1 MSB:IsBigEndian
       
   104     ] ifFalse:[
       
   105         ^ self signedInt32At:1 MSB:IsBigEndian
       
   106     ]
       
   107 
       
   108     "
       
   109      ExternalInt new value:10
       
   110      (ExternalInt new value:16rFFFFFF) asInteger
       
   111     "
       
   112 !
       
   113 
       
   114 asUnsignedInteger
       
   115     "warning: retrieves an integer with the size of the native machine's
       
   116      pointer (i.e. either 32 or 64bit)"
       
   117 
       
   118     ExternalBytes sizeofInt == 8 ifTrue:[
       
   119         ^ self unsignedInt64At:1 MSB:IsBigEndian
       
   120     ] ifFalse:[
       
   121         ^ self unsignedInt32At:1 MSB:IsBigEndian
       
   122     ]
       
   123 
       
   124     "
       
   125      (ExternalInt new value:10) asUnsignedInteger
       
   126     "
       
   127 
       
   128     "Created: / 22-12-2010 / 18:31:03 / cg"
       
   129 !
       
   130 
       
   131 value
       
   132     ExternalBytes sizeofInt == 8 ifTrue:[
       
   133         ^ self signedInt64At:1 MSB:IsBigEndian
       
   134     ] ifFalse:[
       
   135         ^ self signedInt32At:1 MSB:IsBigEndian
       
   136     ]
       
   137 
       
   138     "
       
   139      (ExternalInt new value:10) value
       
   140     "
       
   141 
       
   142     "Modified: / 30.3.1998 / 17:07:57 / cg"
       
   143 !
       
   144 
       
   145 value:anInteger
       
   146     ExternalBytes sizeofInt == 8 ifTrue:[
       
   147         self signedInt64At:1 put:anInteger MSB:IsBigEndian
       
   148     ] ifFalse:[
       
   149         self signedInt32At:1 put:anInteger MSB:IsBigEndian
       
   150     ]
       
   151 
       
   152     "
       
   153      (ExternalInt new value:10) value
       
   154     "
       
   155 ! !
       
   156 
       
   157 !ExternalInt methodsFor:'printing & storing'!
       
   158 
       
   159 printOn:aStream
       
   160     aStream nextPutAll:self className; nextPut:$(.
       
   161     self asInteger printOn:aStream.
       
   162     aStream nextPut:$)
       
   163 ! !
       
   164 
       
   165 !ExternalInt class methodsFor:'documentation'!
       
   166 
       
   167 version
       
   168     ^ '$Header$'
       
   169 !
       
   170 
       
   171 version_CVS
       
   172     ^ '$Header$'
       
   173 ! !
       
   174