From 9aecbe56d2ee21918844147971f2215c36b129f5 Mon Sep 17 00:00:00 2001 From: rfm Date: Wed, 19 Mar 2008 06:59:43 +0000 Subject: [PATCH] Some tidying up of macros. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@26357 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 13 + Headers/Additions/GNUstepBase/GNUstep.h | 337 +++++++++++++++++--- Headers/Additions/GNUstepBase/GSFunctions.h | 24 +- Headers/Foundation/NSBundle.h | 151 --------- Headers/Foundation/NSObject.h | 191 +---------- Source/DocMakefile | 1 + 6 files changed, 336 insertions(+), 381 deletions(-) diff --git a/ChangeLog b/ChangeLog index db77340bb..d00588c86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2008-03-19 Richard Frith-Macdonald + + * Source/DocMakefile: include GNUstep.h + * Headers/Foundation/NSObject.h: include GNUstep.h for macros + * Headers/Foundation/NSBundle.h: move gnustep specific macros out + * Headers/Additions/GNUstepBase/GSFunctions.h: undeprecate and add + GSLocalizedStringFromTableInFramework from NSBundle.h + * Headers/Additions/GNUstepBase/GNUstep.h: Add comments etc. + Code changes to avoid duplication of macro definitions, make + GNUstep.h usable anywhere, and rename a couple of GNUstep specific + extensions to use the GS prefix rather than the (reserved by Apple) + NS prefix. + 2008-03-18 Riccardo Mottola * Source/GNUMakefile: Added FoundationErrors.h diff --git a/Headers/Additions/GNUstepBase/GNUstep.h b/Headers/Additions/GNUstepBase/GNUstep.h index d56385dfc..f0f3562c1 100644 --- a/Headers/Additions/GNUstepBase/GNUstep.h +++ b/Headers/Additions/GNUstepBase/GNUstep.h @@ -25,59 +25,316 @@ #ifndef __GNUSTEP_GNUSTEP_H_INCLUDED_ #define __GNUSTEP_GNUSTEP_H_INCLUDED_ -#ifndef GNUSTEP +/* The contents of this file are designed to be usable with either + * GNUstep-base or MacOS-X Foundation. + */ -#define AUTORELEASE(object) [object autorelease] -#define TEST_AUTORELEASE(object) ({ if (object) [object autorelease]; }) +#if GS_WITH_GC -#define RELEASE(object) [object release] -#define TEST_RELEASE(object) ({ if (object) [object release]; }) +#ifndef RETAIN +#define RETAIN(object) ((id)object) +#endif +#ifndef RELEASE +#define RELEASE(object) +#endif +#ifndef AUTORELEASE +#define AUTORELEASE(object) ((id)object) +#endif -#define RETAIN(object) [object retain] -#define TEST_RETAIN(object) ({ if (object) [object retain]; }) +#ifndef TEST_RETAIN +#define TEST_RETAIN(object) ((id)object) +#endif +#ifndef TEST_RELEASE +#define TEST_RELEASE(object) +#endif +#ifndef TEST_AUTORELEASE +#define TEST_AUTORELEASE(object) ((id)object) +#endif -#define ASSIGN(object,value) ({\ - id __value = (id)(value); \ - id __object = (id)(object); \ - if (__value != __object) \ - { \ - if (__value != nil) \ - { \ - [__value retain]; \ - } \ - object = __value; \ - if (__object != nil) \ - { \ - [__object release]; \ - } \ - } \ - }) +#ifndef ASSIGN +#define ASSIGN(object,value) (object = value) +#endif +#ifndef ASSIGNCOPY +#define ASSIGNCOPY(object,value) (object = [value copy]) +#endif +#ifndef DESTROY +#define DESTROY(object) (object = nil) +#endif -#define ASSIGNCOPY(object,value) ASSIGN(object, [[value copy] autorelease]); +#ifndef CREATE_AUTORELEASE_POOL +#define CREATE_AUTORELEASE_POOL(X) +#endif -#define DESTROY(object) ({ \ - if (object) \ - { \ - id __o = object; \ - object = nil; \ - [__o release]; \ - } \ - }) +#ifndef RECREATE_AUTORELEASE_POOL +#define RECREATE_AUTORELEASE_POOL(X) +#endif -#define CREATE_AUTORELEASE_POOL(X) \ -NSAutoreleasePool *(X) = [NSAutoreleasePool new] +#define IF_NO_GC(X) -#define NSLocalizedString(key, comment) \ - [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] +#else -#define _(X) NSLocalizedString (X, nil) +#ifndef RETAIN +/** + * Basic retain operation ... calls [NSObject-retain] + */ +#define RETAIN(object) [object retain] +#endif + +#ifndef RELEASE +/** + * Basic release operation ... calls [NSObject-release] + */ +#define RELEASE(object) [object release] +#endif + +#ifndef AUTORELEASE +/** + * Basic autorelease operation ... calls [NSObject-autorelease] + */ +#define AUTORELEASE(object) [object autorelease] +#endif + +#ifndef TEST_RETAIN +/** + * Tested retain - only invoke the + * objective-c method if the receiver is not nil. + */ +#define TEST_RETAIN(object) ({\ +id __object = (id)(object); (__object != nil) ? [__object retain] : nil; }) +#endif +#ifndef TEST_RELEASE +/** + * Tested release - only invoke the + * objective-c method if the receiver is not nil. + */ +#define TEST_RELEASE(object) ({\ +id __object = (id)(object); if (__object != nil) [__object release]; }) +#endif +#ifndef TEST_AUTORELEASE +/** + * Tested autorelease - only invoke the + * objective-c method if the receiver is not nil. + */ +#define TEST_AUTORELEASE(object) ({\ +id __object = (id)(object); (__object != nil) ? [__object autorelease] : nil; }) +#endif + +#ifndef ASSIGN +/** + * ASSIGN(object,value) assigns the value to the object with + * appropriate retain and release operations. + */ +#define ASSIGN(object,value) ({\ +id __value = (id)(value); \ +id __object = (id)(object); \ +if (__value != __object) \ + { \ + if (__value != nil) \ + { \ + [__value retain]; \ + } \ + object = __value; \ + if (__object != nil) \ + { \ + [__object release]; \ + } \ + } \ +}) +#endif + +#ifndef ASSIGNCOPY +/** + * ASSIGNCOPY(object,value) assigns a copy of the value to the object + * with release of the original. + */ +#define ASSIGNCOPY(object,value) ({\ +id __value = (id)(value); \ +id __object = (id)(object); \ +if (__value != __object) \ + { \ + if (__value != nil) \ + { \ + __value = [__value copy]; \ + } \ + object = __value; \ + if (__object != nil) \ + { \ + [__object release]; \ + } \ + } \ +}) +#endif + +#ifndef DESTROY +/** + * DESTROY() is a release operation which also sets the variable to be + * a nil pointer for tidiness - we can't accidentally use a DESTROYED + * object later. It also makes sure to set the variable to nil before + * releasing the object - to avoid side-effects of the release trying + * to reference the object being released through the variable. + */ +#define DESTROY(object) ({ \ + if (object) \ + { \ + id __o = object; \ + object = nil; \ + [__o release]; \ + } \ +}) +#endif + +#ifndef CREATE_AUTORELEASE_POOL +/** + * Declares an autorelease pool variable and creates and initialises + * an autorelease pool object. + */ +#define CREATE_AUTORELEASE_POOL(X) \ + NSAutoreleasePool *(X) = [NSAutoreleasePool new] +#endif + +#ifndef RECREATE_AUTORELEASE_POOL +/** + * Similar, but allows reuse of variables. Be sure to use DESTROY() + * so the object variable stays nil. + */ +#define RECREATE_AUTORELEASE_POOL(X) \ + if (X == nil) \ + (X) = [NSAutoreleasePool new] +#endif + +#define IF_NO_GC(X) X + +#endif + + +/** + *

+ * This function (macro) is a GNUstep extension. + *

+ *

+ * _(@"My string to translate") + *

+ *

+ * is exactly the same as + *

+ *

+ * NSLocalizedString (@"My string to translate", @"") + *

+ *

+ * It is useful when you need to translate an application + * very quickly, as you just need to enclose all strings + * inside _(). But please note that when you + * use this macro, you are not taking advantage of comments + * for the translator, so consider using + * NSLocalizedString instead when you need a + * comment. + *

+ */ +#define _(X) NSLocalizedString (X, @"") + + /* The quickest possible way to localize a static string: + + static NSString *string = __(@"New Game"); + + NSLog (_(string)); */ + +/** + *

+ * This function (macro) is a GNUstep extension. + *

+ *

+ * __(@"My string to translate") + *

+ *

+ * is exactly the same as + *

+ *

+ * GSLocalizedStaticString (@"My string to translate", @"") + *

+ *

+ * It is useful when you need to translate an application very + * quickly. You would use it as follows for static strings: + *

+ *

+ * + * NSString *message = __(@"Hello there"); + * ... more code ... + * NSLog (_(messages)); + * + *

+ *

+ * But please note that when you use this macro, you are not + * taking advantage of comments for the translator, so + * consider using GSLocalizedStaticString + * instead when you need a comment. + *

+ */ #define __(X) X -#define NSLocalizedStaticString(X, Y) X + /* The better way for a static string, with a comment - use as follows - + static NSString *string = GSLocalizedStaticString (@"New Game", + @"Menu Option"); -#include + NSLog (_(string)); + + If you need anything more complicated than this, please initialize + the static strings manually. +*/ + +/** + *

+ * This function (macro) is a GNUstep extensions, and it is used + * to localize static strings. Here is an example of a static + * string: + *

+ *

+ * + * NSString *message = @"Hi there"; + * ... some code ... + * NSLog (message); + * + *

+ *

+ * This string can not be localized using the standard + * openstep functions/macros. By using this gnustep extension, + * you can localize it as follows: + *

+ *

+ * + * NSString *message = GSLocalizedStaticString (@"Hi there", + * @"Greeting"); + * + * ... some code ... + * + * NSLog (NSLocalizedString (message, @"")); + * + *

+ *

+ * When the tools generate the + * Localizable.strings file from the source + * code, they will ignore the NSLocalizedString + * call while they will extract the string (and the comment) + * to localize from the GSLocalizedStaticString + * call. + *

+ *

+ * When the code is compiled, instead, the + * GSLocalizedStaticString call is ignored (discarded, + * it is a macro which simply expands to key), while + * the NSLocalizedString will actually look up the + * string for translation in the Localizable.strings + * file. + *

+ *

+ * Please note that there is currently no macro/function to + * localize static strings using different tables. If you + * need that functionality, you have either to prepare the + * localization tables by hand, or to rewrite your code in + * such a way as not to use static strings. + *

+ */ +#define GSLocalizedStaticString(key, comment) key -#endif /* GNUSTEP */ #endif /* __GNUSTEP_GNUSTEP_H_INCLUDED_ */ diff --git a/Headers/Additions/GNUstepBase/GSFunctions.h b/Headers/Additions/GNUstepBase/GSFunctions.h index e84242a89..dbbf9cfc5 100644 --- a/Headers/Additions/GNUstepBase/GSFunctions.h +++ b/Headers/Additions/GNUstepBase/GSFunctions.h @@ -1,4 +1,4 @@ -/** Additional functions for GNUStep +/** Additional functions and macros for GNUStep Copyright (C) 2005 Free Software Foundation, Inc. Written by: Richard Frith-Macdonald @@ -26,24 +26,36 @@ #ifndef __GSFunctions_h_GNUSTEP_BASE_INCLUDE #define __GSFunctions_h_GNUSTEP_BASE_INCLUDE -#include "GNUstepBase/GSVersionMacros.h" +#include "GNUstepBase/GNUstep.h" #include "GNUstepBase/preface.h" #include "GNUstepBase/GSObjCRuntime.h" -#include "GNUstepBase/GNUstep.h" - -#warning "deprecated header ... will be removed in a later release" #if defined(__cplusplus) extern "C" { #endif +#define GSLocalizedStringFromTableInFramework(key, tbl, fpth, comment) \ + [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" \ + table: [bundle pathForGNUstepResource:(tbl) ofType: nil inDirectory: (fpth)] + + /* Now Support for Quick Localization */ + + /* The quickest possible way to localize a string: + + NSLog (_(@"New Game")); + + Please make use of the longer functions taking a comment when you + get the time to localize seriously your code. + */ + #if GS_API_VERSION(GS_API_NONE,011500) @class NSArray; @class NSString; /** * Try to locate file/directory (aName).(anExtension) in paths. - * Will return the first found or nil if nothing is found. + * Will return the first found or nil if nothing is found.
+ * Deprecated ... may be removed in later release. */ GS_EXPORT NSString *GSFindNamedFile(NSArray *paths, NSString *aName, NSString *anExtension); diff --git a/Headers/Foundation/NSBundle.h b/Headers/Foundation/NSBundle.h index 9c5ec687a..bcd6865f6 100644 --- a/Headers/Foundation/NSBundle.h +++ b/Headers/Foundation/NSBundle.h @@ -529,157 +529,6 @@ GS_EXPORT NSString* const NSLoadedClasses; #define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \ [bundle localizedStringForKey:(key) value:@"" table:(tbl)] -#if OS_API_VERSION(GS_API_NONE, GS_API_NONE) -#define NSLocalizedStringFromTableInFramework(key, tbl, fpth, comment) \ - [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" \ - table: [bundle pathForGNUstepResource:(tbl) ofType: nil inDirectory: (fpth)] -#endif /* GNUSTEP */ - - /* Now Support for Quick Localization */ -#if OS_API_VERSION(GS_API_NONE, GS_API_NONE) - - /* The quickest possible way to localize a string: - - NSLog (_(@"New Game")); - - Please make use of the longer functions taking a comment when you - get the time to localize seriously your code. - */ - -/** - *

- * This function (macro) is a GNUstep extension. - *

- *

- * _(@"My string to translate") - *

- *

- * is exactly the same as - *

- *

- * NSLocalizedString (@"My string to translate", @"") - *

- *

- * It is useful when you need to translate an application - * very quickly, as you just need to enclose all strings - * inside _(). But please note that when you - * use this macro, you are not taking advantage of comments - * for the translator, so consider using - * NSLocalizedString instead when you need a - * comment. - *

- */ -#define _(X) NSLocalizedString (X, @"") - - /* The quickest possible way to localize a static string: - - static NSString *string = __(@"New Game"); - - NSLog (_(string)); */ - -/** - *

- * This function (macro) is a GNUstep extension. - *

- *

- * __(@"My string to translate") - *

- *

- * is exactly the same as - *

- *

- * NSLocalizedStaticString (@"My string to translate", @"") - *

- *

- * It is useful when you need to translate an application very - * quickly. You would use it as follows for static strings: - *

- *

- * - * NSString *message = __(@"Hello there"); - - * ... more code ... - - * NSLog (_(messages)); - * - *

- *

- * But please note that when you use this macro, you are not - * taking advantage of comments for the translator, so - * consider using NSLocalizedStaticString - * instead when you need a comment. - *

- */ -#define __(X) X - - /* The better way for a static string, with a comment - use as follows - - - static NSString *string = NSLocalizedStaticString (@"New Game", - @"Menu Option"); - - NSLog (_(string)); - - If you need anything more complicated than this, please initialize - the static strings manually. -*/ - -/** - *

- * This function (macro) is a GNUstep extensions, and it is used - * to localize static strings. Here is an example of a static - * string: - *

- *

- * - * NSString *message = @"Hi there"; - - * ... some code ... - - * NSLog (message); - * - *

- *

- * This string can not be localized using the standard - * openstep functions/macros. By using this gnustep extension, - * you can localize it as follows: - *

- *

- * - * NSString *message = NSLocalizedStaticString (@"Hi there", - * @"Greeting"); - * - * ... some code ... - * - * NSLog (NSLocalizedString (message, @"")); - * - *

- *

- * When the tools generate the - * Localizable.strings file from the source - * code, they will ignore the NSLocalizedString - * call while they will extract the string (and the comment) - * to localize from the NSLocalizedStaticString - * call. - *

- *

- * When the code is compiled, instead, the - * NSLocalizedStaticString call is ignored (discarded, - * it is a macro which simply expands to key), while - * the NSLocalizedString will actually look up the - * string for translation in the Localizable.strings - * file. - *

- *

- * Please note that there is currently no macro/function to - * localize static strings using different tables. If you - * need that functionality, you have either to prepare the - * localization tables by hand, or to rewrite your code in - * such a way as not to use static strings. - *

- */ -#define NSLocalizedStaticString(key, comment) key - -#endif /* GS_API_NONE */ #if defined(__cplusplus) } diff --git a/Headers/Foundation/NSObject.h b/Headers/Foundation/NSObject.h index bcbf68cf6..bd8426056 100644 --- a/Headers/Foundation/NSObject.h +++ b/Headers/Foundation/NSObject.h @@ -27,11 +27,18 @@ #ifndef __NSObject_h_GNUSTEP_BASE_INCLUDE #define __NSObject_h_GNUSTEP_BASE_INCLUDE + #import #import #import #import +#ifndef GS_WITH_GC +#define GS_WITH_GC 0 +#endif + +#import + #if defined(__cplusplus) extern "C" { #endif @@ -408,190 +415,6 @@ GS_EXPORT NSRecursiveLock *gnustep_global_lock; inModes: (NSArray*)modes; @end -/* - * RETAIN(), RELEASE(), and AUTORELEASE() are placeholders for the - * future day when we have garbage collecting. - */ -#ifndef GS_WITH_GC -#define GS_WITH_GC 0 -#endif -#if GS_WITH_GC - -#ifndef RETAIN -#define RETAIN(object) ((id)object) -#endif -#ifndef RELEASE -#define RELEASE(object) -#endif -#ifndef AUTORELEASE -#define AUTORELEASE(object) ((id)object) -#endif - -#ifndef TEST_RETAIN -#define TEST_RETAIN(object) ((id)object) -#endif -#ifndef TEST_RELEASE -#define TEST_RELEASE(object) -#endif -#ifndef TEST_AUTORELEASE -#define TEST_AUTORELEASE(object) ((id)object) -#endif - -#ifndef ASSIGN -#define ASSIGN(object,value) (object = value) -#endif -#ifndef ASSIGNCOPY -#define ASSIGNCOPY(object,value) (object = [value copy]) -#endif -#ifndef DESTROY -#define DESTROY(object) (object = nil) -#endif - -#ifndef CREATE_AUTORELEASE_POOL -#define CREATE_AUTORELEASE_POOL(X) -#endif - -#ifndef RECREATE_AUTORELEASE_POOL -#define RECREATE_AUTORELEASE_POOL(X) -#endif - -#define IF_NO_GC(X) - -#else - -#ifndef RETAIN -/** - * Basic retain operation ... calls [NSObject-retain] - */ -#define RETAIN(object) [object retain] -#endif - -#ifndef RELEASE -/** - * Basic release operation ... calls [NSObject-release] - */ -#define RELEASE(object) [object release] -#endif - -#ifndef AUTORELEASE -/** - * Basic autorelease operation ... calls [NSObject-autorelease] - */ -#define AUTORELEASE(object) [object autorelease] -#endif - -#ifndef TEST_RETAIN -/** - * Tested retain - only invoke the - * objective-c method if the receiver is not nil. - */ -#define TEST_RETAIN(object) ({\ -id __object = (id)(object); (__object != nil) ? [__object retain] : nil; }) -#endif -#ifndef TEST_RELEASE -/** - * Tested release - only invoke the - * objective-c method if the receiver is not nil. - */ -#define TEST_RELEASE(object) ({\ -id __object = (id)(object); if (__object != nil) [__object release]; }) -#endif -#ifndef TEST_AUTORELEASE -/** - * Tested autorelease - only invoke the - * objective-c method if the receiver is not nil. - */ -#define TEST_AUTORELEASE(object) ({\ -id __object = (id)(object); (__object != nil) ? [__object autorelease] : nil; }) -#endif - -#ifndef ASSIGN -/** - * ASSIGN(object,value) assigns the value to the object with - * appropriate retain and release operations. - */ -#define ASSIGN(object,value) ({\ -id __value = (id)(value); \ -id __object = (id)(object); \ -if (__value != __object) \ - { \ - if (__value != nil) \ - { \ - [__value retain]; \ - } \ - object = __value; \ - if (__object != nil) \ - { \ - [__object release]; \ - } \ - } \ -}) -#endif - -#ifndef ASSIGNCOPY -/** - * ASSIGNCOPY(object,value) assigns a copy of the value to the object - * with release of the original. - */ -#define ASSIGNCOPY(object,value) ({\ -id __value = (id)(value); \ -id __object = (id)(object); \ -if (__value != __object) \ - { \ - if (__value != nil) \ - { \ - __value = [__value copy]; \ - } \ - object = __value; \ - if (__object != nil) \ - { \ - [__object release]; \ - } \ - } \ -}) -#endif - -#ifndef DESTROY -/** - * DESTROY() is a release operation which also sets the variable to be - * a nil pointer for tidiness - we can't accidentally use a DESTROYED - * object later. It also makes sure to set the variable to nil before - * releasing the object - to avoid side-effects of the release trying - * to reference the object being released through the variable. - */ -#define DESTROY(object) ({ \ - if (object) \ - { \ - id __o = object; \ - object = nil; \ - [__o release]; \ - } \ -}) -#endif - -#ifndef CREATE_AUTORELEASE_POOL -/** - * Declares an autorelease pool variable and creates and initialises - * an autorelease pool object. - */ -#define CREATE_AUTORELEASE_POOL(X) \ - NSAutoreleasePool *(X) = [NSAutoreleasePool new] -#endif - -#ifndef RECREATE_AUTORELEASE_POOL -/** - * Similar, but allows reuse of variables. Be sure to use DESTROY() - * so the object variable stays nil. - */ -#define RECREATE_AUTORELEASE_POOL(X) \ - if (X == nil) \ - (X) = [NSAutoreleasePool new] -#endif - -#define IF_NO_GC(X) X - -#endif - #if defined(__cplusplus) } #endif diff --git a/Source/DocMakefile b/Source/DocMakefile index 5256a4cfa..dd74f3f30 100644 --- a/Source/DocMakefile +++ b/Source/DocMakefile @@ -126,6 +126,7 @@ NSZone.h BaseAdditions_AGSDOC_FILES = \ ../Documentation/BaseAdditions.gsdoc \ +GNUstep.h \ GSCategories.h \ GSIArray.h \ GSIMap.h \