initial implementation of NSValueTransformer

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@24238 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2006-12-25 18:50:15 +00:00
parent 5b17cc6858
commit 77e89978e6
5 changed files with 264 additions and 0 deletions

View file

@ -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 <rfm@gnu.org>
* Headers/Additions/GNUstep/GSVersionMacros.h:

View file

@ -117,6 +117,7 @@
#import <Foundation/NSURLResponse.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSValueTransformer.h>
#import <Foundation/NSXMLParser.h>
#import <Foundation/NSZone.h>

View file

@ -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 <GNUstepBase/GSVersionMacros.h>
#if OS_API_VERSION(100300,GS_API_LATEST) && GS_API_VERSION(010200,GS_API_LATEST)
#import <Foundation/NSObject.h>
#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 */

View file

@ -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

175
Source/NSValueTransformer.m Normal file
View file

@ -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