75
111
*alignp = info.align;
80
* This function is used to locate information about the instance
81
* variable of obj called name. It returns YES if the variable
82
* was found, NO otherwise. If it returns YES, then the values
83
* pointed to by type, size, and offset will be set (except where
84
* they are nul pointers).
87
GSFindInstanceVariable(id obj, const char *name,
88
const char **type, unsigned int *size, int *offset)
91
struct objc_ivar_list *ivars;
92
struct objc_ivar *ivar = 0;
95
while (class != nil && ivar == 0)
98
class = class->super_class;
103
for (i = 0; i < ivars->ivar_count; i++)
105
if (strcmp(ivars->ivar_list[i].ivar_name, name) == 0)
107
ivar = &ivars->ivar_list[i];
119
*type = ivar->ivar_type;
121
*size = objc_sizeof_type(ivar->ivar_type);
123
*offset = ivar->ivar_offset;
128
* This function performs no checking ... you should use it only where
129
* you are providing information from a call to GSFindInstanceVariable()
130
* and you know that the data area provided is the correct size.
133
GSGetVariable(id obj, int offset, unsigned int size, void *data)
135
memcpy(data, ((void*)obj) + offset, size);
139
* This function performs no checking ... you should use it only where
140
* you are providing information from a call to GSFindInstanceVariable()
141
* and you know that the data area provided is the correct size.
144
GSSetVariable(id obj, int offset, unsigned int size, const void *data)
146
memcpy(((void*)obj) + offset, data, size);
152
GSInstanceVariableInfo(id obj, NSString *iVarName,
153
const char **type, unsigned *size, unsigned *offset)
155
const char *name = [iVarName cString];
157
return GSFindInstanceVariable(obj, name, type, size, offset);
163
GSGetInstanceVariable(id obj, NSString *iVarName, void *data)
165
const char *name = [iVarName cString];
169
if (GSFindInstanceVariable(obj, name, 0, &size, &offset) == YES)
171
GSGetVariable(obj, offset, size, data);
180
GSSetInstanceVariable(id obj, NSString *iVarName, const void *data)
182
const char *name = [iVarName cString];
186
if (GSFindInstanceVariable(obj, name, 0, &size, &offset) == YES)
188
GSSetVariable(obj, offset, size, data);
194
#include <Foundation/NSValue.h>
195
#include <Foundation/NSKeyValueCoding.h>
197
* This is used internally by the key-value coding methods, to get a
198
* value from an object either via an accessor method (if sel is
199
* supplied), or via direct access (if type, size, and offset are
201
* Automatic conversion between NSNumber and C scalar types is performed.<br />
202
* If type is nul and can't be determined from the selector, the
203
* [NSObject-handleQueryWithUnboundKey:] method is called to try
207
GSGetValue(NSObject *self, NSString *key, SEL sel,
208
const char *type, unsigned size, int offset)
212
NSMethodSignature *sig = [self methodSignatureForSelector: sel];
214
if ([sig numberOfArguments] != 2)
216
[NSException raise: NSInvalidArgumentException
217
format: @"key-value get method has wrong number of args"];
219
type = [sig methodReturnType];
223
return [self handleQueryWithUnboundKey: key];
238
v = *(id *)((char *)self + offset);
243
(id (*)(id, SEL))[self methodForSelector: sel];
245
v = (*imp)(self, sel);
257
v = *(char *)((char *)self + offset);
261
signed char (*imp)(id, SEL) =
262
(signed char (*)(id, SEL))[self methodForSelector: sel];
264
v = (*imp)(self, sel);
266
val = [NSNumber numberWithChar: v];
276
v = *(unsigned char *)((char *)self + offset);
280
unsigned char (*imp)(id, SEL) =
281
(unsigned char (*)(id, SEL))[self methodForSelector:
284
v = (*imp)(self, sel);
286
val = [NSNumber numberWithUnsignedChar: v];
296
v = *(short *)((char *)self + offset);
300
short (*imp)(id, SEL) =
301
(short (*)(id, SEL))[self methodForSelector: sel];
303
v = (*imp)(self, sel);
305
val = [NSNumber numberWithShort: v];
315
v = *(unsigned short *)((char *)self + offset);
319
unsigned short (*imp)(id, SEL) =
320
(unsigned short (*)(id, SEL))[self methodForSelector:
323
v = (*imp)(self, sel);
325
val = [NSNumber numberWithUnsignedShort: v];
335
v = *(int *)((char *)self + offset);
339
int (*imp)(id, SEL) =
340
(int (*)(id, SEL))[self methodForSelector: sel];
342
v = (*imp)(self, sel);
344
val = [NSNumber numberWithInt: v];
354
v = *(unsigned int *)((char *)self + offset);
358
unsigned int (*imp)(id, SEL) =
359
(unsigned int (*)(id, SEL))[self methodForSelector:
362
v = (*imp)(self, sel);
364
val = [NSNumber numberWithUnsignedInt: v];
374
v = *(long *)((char *)self + offset);
378
long (*imp)(id, SEL) =
379
(long (*)(id, SEL))[self methodForSelector: sel];
381
v = (*imp)(self, sel);
383
val = [NSNumber numberWithLong: v];
393
v = *(unsigned long *)((char *)self + offset);
397
unsigned long (*imp)(id, SEL) =
398
(unsigned long (*)(id, SEL))[self methodForSelector:
401
v = (*imp)(self, sel);
403
val = [NSNumber numberWithUnsignedLong: v];
414
v = *(long long *)((char *)self + offset);
418
long long (*imp)(id, SEL) =
419
(long long (*)(id, SEL))[self methodForSelector: sel];
421
v = (*imp)(self, sel);
423
val = [NSNumber numberWithLongLong: v];
431
unsigned long long v;
435
v = *(unsigned long long *)((char *)self + offset);
439
unsigned long long (*imp)(id, SEL) =
440
(unsigned long long (*)(id, SEL))[self
441
methodForSelector: sel];
443
v = (*imp)(self, sel);
445
val = [NSNumber numberWithUnsignedLongLong: v];
456
v = *(float *)((char *)self + offset);
460
float (*imp)(id, SEL) =
461
(float (*)(id, SEL))[self methodForSelector: sel];
463
v = (*imp)(self, sel);
465
val = [NSNumber numberWithFloat: v];
475
v = *(double *)((char *)self + offset);
479
double (*imp)(id, SEL) =
480
(double (*)(id, SEL))[self methodForSelector: sel];
482
v = (*imp)(self, sel);
484
val = [NSNumber numberWithDouble: v];
490
void (*imp)(id, SEL) =
491
(void (*)(id, SEL))[self methodForSelector: sel];
499
[NSException raise: NSInvalidArgumentException
500
format: @"key-value get method has unsupported type"];
507
* This is used internally by the key-value coding methods, to set a
508
* value in an object either via an accessor method (if sel is
509
* supplied), or via direct access (if type, size, and offset are
511
* Automatic conversion between NSNumber and C scalar types is performed.<br />
512
* If type is nul and can't be determined from the selector, the
513
* [NSObject-handleTakevalue:forUnboundKey:] method is called to try
517
GSSetValue(NSObject *self, NSString *key, id val, SEL sel,
518
const char *type, unsigned size, int offset)
522
NSMethodSignature *sig = [self methodSignatureForSelector: sel];
524
if ([sig numberOfArguments] != 3)
526
[NSException raise: NSInvalidArgumentException
527
format: @"key-value set method has wrong number of args"];
529
type = [sig getArgumentTypeAtIndex: 2];
533
[self handleTakeValue: val forUnboundKey: key];
546
id *ptr = (id *)((char *)self + offset);
553
void (*imp)(id, SEL, id) =
554
(void (*)(id, SEL, id))[self methodForSelector: sel];
556
(*imp)(self, sel, val);
563
char v = [val charValue];
567
char *ptr = (char *)((char *)self + offset);
573
void (*imp)(id, SEL, char) =
574
(void (*)(id, SEL, char))[self methodForSelector: sel];
576
(*imp)(self, sel, v);
583
unsigned char v = [val unsignedCharValue];
587
unsigned char *ptr = (unsigned char*)((char *)self + offset);
593
void (*imp)(id, SEL, unsigned char) =
594
(void (*)(id, SEL, unsigned char))[self methodForSelector:
597
(*imp)(self, sel, v);
604
short v = [val shortValue];
608
short *ptr = (short*)((char *)self + offset);
614
void (*imp)(id, SEL, short) =
615
(void (*)(id, SEL, short))[self methodForSelector: sel];
617
(*imp)(self, sel, v);
624
unsigned short v = [val unsignedShortValue];
630
ptr = (unsigned short*)((char *)self + offset);
635
void (*imp)(id, SEL, unsigned short) =
636
(void (*)(id, SEL, unsigned short))[self methodForSelector:
639
(*imp)(self, sel, v);
646
int v = [val intValue];
650
int *ptr = (int*)((char *)self + offset);
656
void (*imp)(id, SEL, int) =
657
(void (*)(id, SEL, int))[self methodForSelector: sel];
659
(*imp)(self, sel, v);
666
unsigned int v = [val unsignedIntValue];
670
unsigned int *ptr = (unsigned int*)((char *)self + offset);
676
void (*imp)(id, SEL, unsigned int) =
677
(void (*)(id, SEL, unsigned int))[self methodForSelector:
680
(*imp)(self, sel, v);
687
long v = [val longValue];
691
long *ptr = (long*)((char *)self + offset);
697
void (*imp)(id, SEL, long) =
698
(void (*)(id, SEL, long))[self methodForSelector: sel];
700
(*imp)(self, sel, v);
707
unsigned long v = [val unsignedLongValue];
711
unsigned long *ptr = (unsigned long*)((char *)self + offset);
717
void (*imp)(id, SEL, unsigned long) =
718
(void (*)(id, SEL, unsigned long))[self methodForSelector:
721
(*imp)(self, sel, v);
729
long long v = [val longLongValue];
733
long long *ptr = (long long*)((char *)self + offset);
739
void (*imp)(id, SEL, long long) =
740
(void (*)(id, SEL, long long))[self methodForSelector: sel];
742
(*imp)(self, sel, v);
751
unsigned long long v = [val unsignedLongLongValue];
755
unsigned long long *ptr = (unsigned long long*)((char*)self +
762
void (*imp)(id, SEL, unsigned long long) =
763
(void (*)(id, SEL, unsigned long long))[self
764
methodForSelector: sel];
766
(*imp)(self, sel, v);
774
float v = [val floatValue];
778
float *ptr = (float*)((char *)self + offset);
784
void (*imp)(id, SEL, float) =
785
(void (*)(id, SEL, float))[self methodForSelector: sel];
787
(*imp)(self, sel, v);
794
double v = [val doubleValue];
798
double *ptr = (double*)((char *)self + offset);
804
void (*imp)(id, SEL, double) =
805
(void (*)(id, SEL, double))[self methodForSelector: sel];
807
(*imp)(self, sel, v);
813
[NSException raise: NSInvalidArgumentException
814
format: @"key-value set method has unsupported type"];
820
/* Getting a system error message on a variety of systems */
822
LPTSTR GetErrorMsg(DWORD msgId)
827
FORMAT_MESSAGE_ALLOCATE_BUFFER |
828
FORMAT_MESSAGE_FROM_SYSTEM |
829
FORMAT_MESSAGE_IGNORE_INSERTS,
831
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
832
(LPTSTR)&lpMsgBuf, 0, NULL);
834
return (LPTSTR)lpMsgBuf;
837
#ifndef HAVE_STRERROR
841
extern char* sys_errlist[];
844
if (eno < 0 || eno >= sys_nerr)
846
return("unknown error number");
848
return(sys_errlist[eno]);
851
#endif /* __MINGW__ */
853
const char *GSLastErrorStr(long error_id)
856
return GetErrorMsg(GetLastError());
858
return strerror(error_id);