From 507bceeb416b7c6274d1dfac50b8f8147c622306 Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 15 Jan 1999 11:24:03 +0000 Subject: [PATCH] Added functions to access instance variables. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3566 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++ Headers/gnustep/base/NSObjCRuntime.h | 5 ++ Source/NSObjCRuntime.m | 85 ++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/ChangeLog b/ChangeLog index fbbd474a0..b98991da9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Jan 15 10:45:00 1999 Richard Frith-Macdonald + + src/NSObjCRuntime.m: Added GSGetInstanceVariable() and + GSSetInstanceVariable() methods - extensions to gnustep. + src/NSObjCRuntime.h: ditto + Tue Jan 12 4:10:00 1999 Richard Frith-Macdonald * src/NSProcessInfo.m: Fix for GNU/Linux-powerpc (__PPC__) to get diff --git a/Headers/gnustep/base/NSObjCRuntime.h b/Headers/gnustep/base/NSObjCRuntime.h index 2455dfd8c..886edc2ff 100644 --- a/Headers/gnustep/base/NSObjCRuntime.h +++ b/Headers/gnustep/base/NSObjCRuntime.h @@ -53,4 +53,9 @@ extern void NSLogv (NSString* format, va_list args); #define nil 0 #endif nil +#ifndef NO_GNUSTEP +extern BOOL GSGetInstanceVariable(id obj, NSString *name, void* data); +extern BOOL GSSetInstanceVariable(id obj, NSString *name, void* data); +#endif + #endif /* __NSObjCRuntime_h_GNUSTEP_BASE_INCLUDE */ diff --git a/Source/NSObjCRuntime.m b/Source/NSObjCRuntime.m index 42c082bb1..abd0c96d6 100644 --- a/Source/NSObjCRuntime.m +++ b/Source/NSObjCRuntime.m @@ -72,3 +72,88 @@ NSGetSizeAndAlignment(const char *typePtr, unsigned *sizep, unsigned *alignp) *alignp = info.align; } +BOOL +GSGetIinstanceVariable(id obj, NSString *iVarName, void *data) +{ + const char *name = [iVarName cString]; + Class class; + struct objc_ivar_list *ivars; + struct objc_ivar *ivar = 0; + int offset; + const char *type; + unsigned int size; + + class = [obj class]; + while (class != nil && ivar == 0) + { + ivars = class->ivars; + class = class->super_class; + if (ivars) + { + int i; + + for (i = 0; i < ivars->ivar_count; i++) + { + if (strcmp(ivars->ivar_list[i].ivar_name, name) == 0) + { + ivar = &ivars->ivar_list[i]; + break; + } + } + } + } + if (ivar == 0) + { + NSLog(@"Attempt to get non-existent ivar"); + return NO; + } + + offset = ivar->ivar_offset; + type = ivar->ivar_type; + size = objc_sizeof_type(type); + memcpy(data, ((void*)obj) + offset, size); + return YES; +} + +BOOL +GSSetInstanceVariable(id obj, NSString *iVarName, void *data) +{ + const char *name = [iVarName cString]; + Class class; + struct objc_ivar_list *ivars; + struct objc_ivar *ivar = 0; + int offset; + const char *type; + unsigned int size; + + class = [obj class]; + while (class != nil && ivar == 0) + { + ivars = class->ivars; + class = class->super_class; + if (ivars) + { + int i; + + for (i = 0; i < ivars->ivar_count; i++) + { + if (strcmp(ivars->ivar_list[i].ivar_name, name) == 0) + { + ivar = &ivars->ivar_list[i]; + break; + } + } + } + } + if (ivar == 0) + { + NSLog(@"Attempt to set non-existent ivar"); + return NO; + } + + offset = ivar->ivar_offset; + type = ivar->ivar_type; + size = objc_sizeof_type(type); + memcpy(((void*)obj) + offset, data, size); + return YES; +}