Added method for returning immutable objects.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@14776 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-10-14 08:52:39 +00:00
parent 53e98d912a
commit d54488d74d
8 changed files with 76 additions and 12 deletions

View file

@ -1,3 +1,14 @@
2002-10-14 Richard Frith-Macdonald <rfm@gnu.org>
Source/NSObject.m: Add ([-makeImmutableCopyOnFail:]) for use by methods
wishing to return constant string/array/dictionary etc results after
building them using mutable objects they created.
Source/GSArray.m: Implement ([makeImmutableCopyOnFail:])
Source/GSString.m: ditto
Source/GSDictionary.m: ditto
Source/GSSet.m: ditto
Source/NSFileManager.m: Use it
2002-10-13 Mirko Viviani <mirko.viviani@rccr.cremona.it>
* Source/NSBundle.m ([NSBundle +_addFrameworkFromClass:]): remove

View file

@ -285,6 +285,7 @@ GS_EXPORT NSRecursiveLock *gnustep_global_lock;
+ (void) descriptionWithLocale: (NSDictionary*)aLocale
indent: (unsigned)level
to: (id<GNUDescriptionDestination>)output;
- (id) makeImmutableCopyOnFail: (BOOL)force;
- (Class) transmuteClassTo: (Class)aClassObject;
- (id) subclassResponsibility: (SEL)aSel;
- (id) shouldNotImplement: (SEL)aSel;

View file

@ -479,6 +479,12 @@ static SEL eqSel;
_contents_array[index] = RETAIN(anObject);
}
- (id) makeImmutableCopyOnFail: (BOOL)force
{
isa = [GSArray class];
return self;
}
- (void) removeLastObject
{
if (_count == 0)

View file

@ -284,6 +284,12 @@ static SEL objSel;
return self;
}
- (id) makeImmutableCopyOnFail: (BOOL)force
{
isa = [GSDictionary class];
return self;
}
- (void) setObject: (id)anObject forKey: (id)aKey
{
GSIMapNode node;

View file

@ -562,6 +562,12 @@ static Class mutableSetClass;
}
}
- (id) makeImmutableCopyOnFail: (BOOL)force
{
isa = [GSSet class];
return self;
}
- (void) minusSet: (NSSet*) other
{
if (other == self)

View file

@ -2920,6 +2920,12 @@ transmute(ivars self, NSString *aString)
return lossyCString_c((ivars)self);
}
- (id) makeImmutableCopyOnFail: (BOOL)force
{
isa = [GSString class];
return self;
}
- (id) mutableCopy
{
GSMutableString *obj;

View file

@ -45,9 +45,9 @@
#include <Foundation/NSLock.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSSet.h>
#include "GSPrivate.h"
#include <stdio.h>
@ -1428,12 +1428,12 @@ static NSFileManager* defaultManager = nil;
id values[5];
id keys[5] = {
NSFileSystemSize,
NSFileSystemFreeSize,
NSFileSystemNodes,
NSFileSystemFreeNodes,
NSFileSystemNumber
};
NSFileSystemSize,
NSFileSystemFreeSize,
NSFileSystemNodes,
NSFileSystemFreeNodes,
NSFileSystemNumber
};
if (stat(cpath, &statbuf) != 0)
return nil;
@ -1468,7 +1468,8 @@ static NSFileManager* defaultManager = nil;
* Returns an array of the contents of the specified directory.<br />
* The listing does <strong>not</strong> recursively list subdirectories.<br />
* The special files '.' and '..' are not listed.<br />
* Returns nil if path is not a directory (or it can't be read for some reason).
* Indicates an error by returning nil (eg. if path is not a directory or
* it can't be read for some reason).
*/
- (NSArray*) directoryContentsAtPath: (NSString*)path
{
@ -1504,7 +1505,7 @@ static NSFileManager* defaultManager = nil;
}
RELEASE(direnum);
return content;
return [content makeImmutableCopyOnFail: NO];
}
/**
@ -1569,7 +1570,7 @@ static NSFileManager* defaultManager = nil;
RELEASE(direnum);
return content;
return [content makeImmutableCopyOnFail: NO];
}
/**

View file

@ -2088,18 +2088,45 @@ static BOOL double_release_check_enabled = NO;
:class_get_class_method(GSObjCClass(self), aSel)));
}
/**
* Transmutes the receiver into an immutable version of the same object
* and returns the result.<br />
* If the receiver is not a mutable object or cannot be simply transmuted,
* then this method either returns the receiver unchanged or,
* if the force flag is set to YES, returns an autoreleased copy of the
* receiver.<br />
* Mutable classes should override this default implementation.<br />
* This method is used in methods which are declared to return immutable
* objects (eg. an NSArray), but which create and build mutable ones
* internally.
*/
- (id) makeImmutableCopyOnFail: (BOOL)force
{
if (force == YES)
{
return AUTORELEASE([self copy]);
}
return self;
}
/**
* Changes the class of the receiver (the 'isa' pointer) to be aClassObject,
* but only if the receiver is an instance of a subclass of aClassObject
* which has not added extra instance variables.<br />
* Returns zero on failure, or the old class on success.
*/
- (Class) transmuteClassTo: (Class)aClassObject
{
if (GSObjCIsInstance(self) == YES)
if (class_is_class(aClassObject))
if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
if ([self isKindOfClass:aClassObject])
if ([self isKindOfClass: aClassObject])
{
Class old_isa = isa;
isa = aClassObject;
return old_isa;
}
return nil;
return 0;
}
- (id) subclassResponsibility: (SEL)aSel