Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSProgress fixes branch #65

Merged
merged 5 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Headers/Foundation/Foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
#import <Foundation/NSPortNameServer.h>
#import <Foundation/NSPredicate.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSProgress.h>
#import <Foundation/NSProtocolChecker.h>
#import <Foundation/NSProxy.h>
#import <Foundation/NSRange.h>
Expand Down
8 changes: 0 additions & 8 deletions Headers/Foundation/MISSING
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,9 @@ NSAttributedString:
- enumerateAttribute:inRange:options:usingBlock:
-------------------------------------------------------------
NSBundle:
- bundleURL
- resourceURL
- executableURL
- URLForAuxiliaryExecutable:
- privateFrameworksURL
- sharedFrameworksURL
- sharedSupportURL
- builtInPlugInsURL
- appStoreReceiptURL
- pathForAuxiliaryExecutable:
- privateFrameworksPath
- sharedFrameworksPath
- sharedSupportPath
+ URLsForResourcesWithExtension:subdirectory:inBundleWithURL:
Expand Down
4 changes: 2 additions & 2 deletions Headers/Foundation/NSProgress.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
extern "C" {
#endif

@class NSString, NSDictionary, NSArray, NSNumber, NSProgress;
@class NSString, NSDictionary, NSArray, NSNumber, NSURL, NSProgress;

#if OS_API_VERSION(MAC_OS_X_VERSION_10_9, GS_API_LATEST)

Expand Down Expand Up @@ -91,7 +91,7 @@ GS_NSProgress_IVARS;
- (void) setCompletedUnitCount: (int64_t)unitCount;

- (NSString *) localizedDescription;
- (NSString *) localizedAddtionalDescription;
- (NSString *) localizedAdditionalDescription;

// Observing progress
- (double) fractionCompleted;
Expand Down
67 changes: 60 additions & 7 deletions Source/NSProgress.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
NSProgressUnpublishingHandler _unpublishingHandler; \
GSProgressPendingUnitCountBlock _pendingUnitCountHandler; \
GSProgressResumingHandler _resumingHandler; \
NSString *_localizedDescription; \
NSString *_localizedAdditionalDescription; \
NSProgress *_parent;

#define EXPOSE_NSProgress_IVARS
Expand All @@ -58,6 +60,7 @@
#import <Foundation/NSURL.h>
#import <Foundation/NSString.h>
#import <Foundation/NSProgress.h>
#import <Foundation/NSKeyValueObserving.h>

#define GSInternal NSProgressInternal
#include "GSInternal.h"
Expand Down Expand Up @@ -96,15 +99,16 @@ - (instancetype) initWithParent: (NSProgress *)parent
internal->_throughput = nil;
internal->_totalUnitCount = 0;
internal->_completedUnitCount = 0;
internal->_userInfo = [userInfo mutableCopy];
internal->_userInfo = RETAIN([userInfo mutableCopy]);
internal->_cancelled = NO;
internal->_cancellable = NO;
internal->_paused = NO;
internal->_pausable = NO;
internal->_indeterminate = NO;
internal->_finished = NO;
internal->_localizedDescription = nil;
internal->_localizedAdditionalDescription = nil;
internal->_parent = parent; // this is a weak reference and not retained.
internal->_userInfo = [[NSMutableDictionary alloc] initWithCapacity: 10];
}
return self;
}
Expand All @@ -119,6 +123,8 @@ - (void) dealloc
RELEASE(internal->_fileTotalCount);
RELEASE(internal->_throughput);
RELEASE(internal->_userInfo);
RELEASE(internal->_localizedDescription);
RELEASE(internal->_localizedAdditionalDescription);

[super dealloc];
}
Expand Down Expand Up @@ -209,14 +215,22 @@ - (void) setCompletedUnitCount: (int64_t)count

- (NSString *) localizedDescription
{
return [NSString stringWithFormat: @"%f percent complete",
[self fractionCompleted]];
return internal->_localizedDescription;
}

- (NSString *) localizedAddtionalDescription
- (void) setLocalizedDescription: (NSString *)localDescription
{
return [NSString stringWithFormat: @"%@ minute(s) remaining",
[self estimatedTimeRemaining]];
ASSIGNCOPY(internal->_localizedDescription, localDescription);
}

- (NSString *) localizedAdditionalDescription
{
return internal->_localizedAdditionalDescription;
}

- (void) setLocalizedAdditionalDescription: (NSString *)localDescription
{
ASSIGNCOPY(internal->_localizedAdditionalDescription, localDescription);
}

// Observing progress
Expand All @@ -237,9 +251,17 @@ - (BOOL) isCancelled
return internal->_cancelled;
}

- (BOOL) cancelled
{
return internal->_cancelled;
}

- (void) cancel
{
[self willChangeValueForKey: @"cancelled"];
CALL_BLOCK_NO_ARGS(internal->_cancellationHandler);
internal->_cancelled = YES;
[self didChangeValueForKey: @"cancelled"];
}

- (void) setCancellationHandler: (GSProgressCancellationHandler) handler
Expand All @@ -259,8 +281,10 @@ - (BOOL) isPaused

- (void) pause
{
[self willChangeValueForKey: @"paused"];
CALL_BLOCK_NO_ARGS(internal->_pausingHandler);
internal->_paused = YES;
[self didChangeValueForKey: @"paused"];
}

- (void) setPausingHandler: (GSProgressPausingHandler) handler
Expand All @@ -284,9 +308,16 @@ - (BOOL) isIndeterminate
return internal->_indeterminate;
}

- (BOOL) indeterminate
{
return internal->_indeterminate;
}

- (void) setIndeterminate: (BOOL)flag
{
[self willChangeValueForKey: @"indeterminate"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unnecessary as the basic KVO mechanism should create this automatically. This is also true for some of the following methods. We should only need to add willChange.. and didChange.. methods to support different names and even for these there was a better way, if I remember correctly.

internal->_indeterminate = flag;
[self didChangeValueForKey: @"indeterminate"];
}

- (NSProgressKind) kind
Expand All @@ -296,7 +327,9 @@ - (NSProgressKind) kind

- (void) setKind: (NSProgressKind)k
{
[self willChangeValueForKey: @"kind"];
ASSIGN(internal->_kind, k);
[self didChangeValueForKey: @"kind"];
}

- (void)setUserInfoObject: (id)obj
Expand All @@ -308,7 +341,9 @@ - (void)setUserInfoObject: (id)obj
// Instance property accessors...
- (void) setFileOperationKind: (NSProgressFileOperationKind)k;
{
[self willChangeValueForKey: @"fileOperationKind"];
ASSIGN(internal->_fileOperationKind, k);
[self didChangeValueForKey: @"fileOperationKind"];
}

- (NSProgressFileOperationKind) fileOperationKind
Expand All @@ -318,7 +353,9 @@ - (NSProgressFileOperationKind) fileOperationKind

- (void) setFileUrl: (NSURL *)u
{
[self willChangeValueForKey: @"fileUrl"];
ASSIGN(internal->_fileUrl, u);
[self didChangeValueForKey: @"fileUrl"];
}

- (NSURL*) fileUrl
Expand All @@ -331,14 +368,26 @@ - (BOOL) isFinished
return internal->_finished;
}

- (BOOL) finished
{
return internal->_finished;
}

- (BOOL) isOld
{
return internal->_old;
}

- (BOOL) old
{
return internal->_old;
}

- (void) setEstimatedTimeRemaining: (NSNumber *)n
{
[self willChangeValueForKey: @"estimatedTimeRemaining"];
ASSIGNCOPY(internal->_estimatedTimeRemaining, n);
[self didChangeValueForKey: @"estimatedTimeRemaining"];
}

- (NSNumber *) estimatedTimeRemaining
Expand All @@ -358,7 +407,9 @@ - (NSNumber *) fileCompletedCount

- (void) setFileTotalCount: (NSNumber *)n
{
[self willChangeValueForKey: @"fileTotalCount"];
ASSIGNCOPY(internal->_fileTotalCount, n);
[self didChangeValueForKey: @"fileTotalCount"];
}

- (NSNumber *) fileTotalCount
Expand All @@ -368,7 +419,9 @@ - (NSNumber *) fileTotalCount

- (void) setThroughput: (NSNumber *)n
{
[self willChangeValueForKey: @"throughput"];
ASSIGNCOPY(internal->_throughput, n);
[self didChangeValueForKey: @"throughput"];
}

- (NSNumber *) throughtput
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions Tests/base/NSProgress/basic.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#import <Foundation/NSProgress.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSAutoreleasePool.h>
#import "ObjectTesting.h"

int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSDictionary *dict = [NSDictionary dictionary];
NSProgress *progress = [[NSProgress alloc] initWithParent: nil
userInfo: dict];
PASS(progress != nil, "[NSProgress initWithParent:userInfo:] returns instance");

progress = [NSProgress discreteProgressWithTotalUnitCount:100];
PASS(progress != nil, "[NSProgress discreteProgressWithTotalUnitCount:] returns instance");

progress = [NSProgress progressWithTotalUnitCount:100];
PASS(progress != nil, "[NSProgress progressWithTotalUnitCount:] returns instance");

progress = [NSProgress progressWithTotalUnitCount:100
parent:progress
pendingUnitCount:50];
PASS(progress != nil, "[NSProgress progressWithTotalUnitCount:] returns instance");

[progress becomeCurrentWithPendingUnitCount:50];
NSProgress *currentProgress = [NSProgress currentProgress];
PASS(currentProgress == progress, "Correct progress object associated with current thread");

NSProgress *new_progress = [NSProgress progressWithTotalUnitCount:100
parent:progress
pendingUnitCount:50];
[new_progress addChild:[[NSProgress alloc] initWithParent: nil userInfo: nil]
withPendingUnitCount:50];

[currentProgress resignCurrent];

PASS([NSProgress currentProgress] == nil, "Current progress is nil after resign current");

[arp release]; arp = nil;
return 0;
}