diff --git a/ChangeLog b/ChangeLog index 9721d352f..c9b623553 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-12-25 Dr. H. Nikolaus Schaller + + * Headers/Foundation/NSValueTransformer.h: + * Headers/Foundation/Foundation.h: + * Source/NSValueTransformer.h: + * Source/GNUmakefile: + Initial implementation of NSValueTransformer. + 2006-12-25 Richard Frith-Macdonald * Headers/Additions/GNUstep/GSVersionMacros.h: diff --git a/Headers/Foundation/Foundation.h b/Headers/Foundation/Foundation.h index a201823ee..8705c8679 100644 --- a/Headers/Foundation/Foundation.h +++ b/Headers/Foundation/Foundation.h @@ -117,6 +117,7 @@ #import #import #import +#import #import #import diff --git a/Headers/Foundation/NSValueTransformer.h b/Headers/Foundation/NSValueTransformer.h new file mode 100644 index 000000000..3db4c8d78 --- /dev/null +++ b/Headers/Foundation/NSValueTransformer.h @@ -0,0 +1,78 @@ +/* Interface for NSValueTransformer for GNUStep + Copyright (C) 2006 Free Software Foundation, Inc. + + Written Dr. H. Nikolaus Schaller + Created on Mon Mar 21 2005. + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSValueTransformer_h_GNUSTEP_BASE_INCLUDE +#define __NSValueTransformer_h_GNUSTEP_BASE_INCLUDE +#import + +#if OS_API_VERSION(100300,GS_API_LATEST) && GS_API_VERSION(010200,GS_API_LATEST) + +#import + +#if defined(__cplusplus) +extern "C" { +#endif + +GS_EXPORT NSString* const NSNegateBooleanTransformerName; +GS_EXPORT NSString* const NSIsNilTransformerName; +GS_EXPORT NSString* const NSIsNotNilTransformerName; +GS_EXPORT NSString* const NSUnarchiveFromDataTransformerName; + +@class NSString; + +@interface NSValueTransformer : NSObject + ++ (BOOL) allowsReverseTransformation; ++ (void) setValueTransformer: (NSValueTransformer *)transformer + forName: (NSString *)name; ++ (Class) transformedValueClass; ++ (NSValueTransformer *) valueTransformerForName: (NSString *)name; ++ (NSArray *) valueTransformerNames; + +- (id) reverseTransformedValue: (id)value; +- (id) transformedValue: (id)value; + +@end + +// builtin transformers + +@interface NSNegateBooleanTransformer : NSValueTransformer +@end + +@interface NSIsNilTransformer : NSValueTransformer +@end + +@interface NSIsNotNilTransformer : NSValueTransformer +@end + +@interface NSUnarchiveFromDataTransformer : NSValueTransformer +@end + +#if defined(__cplusplus) +} +#endif + +#endif /* OS_API_VERSION */ + +#endif /* __NSValueTransformer_h_GNUSTEP_BASE_INCLUDE */ diff --git a/Source/GNUmakefile b/Source/GNUmakefile index 7b11d7600..966bc86bf 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -243,6 +243,7 @@ NSURLResponse.m \ NSURLHandle.m \ NSUserDefaults.m \ NSValue.m \ +NSValueTransformer.m \ NSXMLParser.m \ NSZone.m \ externs.m \ @@ -369,6 +370,7 @@ NSURLResponse.h \ NSUserDefaults.h \ NSUtilities.h \ NSValue.h \ +NSValueTransformer.h \ NSXMLParser.h \ NSZone.h diff --git a/Source/NSValueTransformer.m b/Source/NSValueTransformer.m new file mode 100644 index 000000000..45978bdda --- /dev/null +++ b/Source/NSValueTransformer.m @@ -0,0 +1,175 @@ +/* Implementation for NSValueTransformer for GNUStep + Copyright (C) 2006 Free Software Foundation, Inc. + + Written Dr. H. Nikolaus Schaller + Created on Mon Mar 21 2005. + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#import "Foundation/Foundation.h" + +@implementation NSValueTransformer + +NSString * const NSNegateBooleanTransformerName + = @"NSNegateBooleanTransformerName"; +NSString * const NSIsNilTransformerName + = @"NSIsNilTransformerName"; +NSString * const NSIsNotNilTransformerName + = @"NSIsNotNilTransformerName"; +NSString * const NSUnarchiveFromDataTransformerName + = @"NSUnarchiveFromDataTransformerName"; + +// non-abstract methods + +static NSMutableDictionary *names; + ++ (void) setValueTransformer: (NSValueTransformer *)transformer + forName: (NSString *)name +{ + if (names == nil) + { + [self valueTransformerNames]; // allocate if needed + } + [names setObject: transformer forKey: name]; +} + ++ (NSValueTransformer *) valueTransformerForName: (NSString *)name +{ + return [names objectForKey: name]; +} + ++ (NSArray *) valueTransformerNames; +{ + if (names == nil) + { + names = [[NSMutableDictionary alloc] init]; + } + return [names allKeys]; +} + +// abstract methods (must be implemented in subclasses) + ++ (BOOL) allowsReverseTransformation +{ + [self subclassResponsibility: _cmd]; + return NO; +} + ++ (Class) transformedValueClass +{ + return [self subclassResponsibility: _cmd]; +} + +- (id) reverseTransformedValue: (id)value +{ + return [self subclassResponsibility: _cmd]; +} + +- (id) transformedValue: (id)value +{ + return [self subclassResponsibility: _cmd]; +} + +@end + +// builtin transformers + +@implementation NSNegateBooleanTransformer + ++ (BOOL) allowsReverseTransformation +{ + return YES; +} ++ (Class) transformedValueClass +{ + return [NSNumber class]; +} +- (id) reverseTransformedValue: (id) value +{ + return [NSNumber numberWithBool: ![value boolValue]]; +} +- (id) transformedValue: (id)value +{ + return [NSNumber numberWithBool: ![value boolValue]]; +} + +@end + +@implementation NSIsNilTransformer + ++ (BOOL) allowsReverseTransformation +{ + return NO; +} ++ (Class) transformedValueClass +{ + return [NSNumber class]; +} +- (id) reverseTransformedValue: (id)value +{ + return [self notImplemented: _cmd]; +} +- (id) transformedValue: (id)value +{ + return [NSNumber numberWithBool: (value == nil)]; +} + +@end + +@implementation NSIsNotNilTransformer + ++ (BOOL) allowsReverseTransformation +{ + return NO; +} ++ (Class) transformedValueClass +{ + return [NSNumber class]; +} +- (id) reverseTransformedValue: (id)value +{ + return [self notImplemented: _cmd]; +} +- (id) transformedValue: (id)value +{ + return [NSNumber numberWithBool: (value != nil)]; +} + +@end + +@implementation NSUnarchiveFromDataTransformer + ++ (BOOL) allowsReverseTransformation +{ + return YES; +} ++ (Class) transformedValueClass +{ + return [NSData class]; +} +- (id) reverseTransformedValue: (id)value +{ + return [self notImplemented: _cmd]; +} +- (id) transformedValue: (id)value +{ + return [self notImplemented: _cmd]; +} + +@end