Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert premature (read: broken) change to make +sharedInstance implem…

…entations in SBJsonStreamParserState* and SBJsonStreamWriterState* more efficient and readable.

This reverts commit 49725df.
This reverts commit 2f92db2.
  • Loading branch information...
commit 4bc6ae1f443fedc0e8353c00a1b69cae107fb9b2 1 parent 2f92db2
Stig Brautaset authored
View
1  Classes/SBJsonStreamParserState.h
@@ -57,6 +57,7 @@
@interface SBJsonStreamParserStateError : SBJsonStreamParserState
@end
+
@interface SBJsonStreamParserStateObjectStart : SBJsonStreamParserState
@end
View
64 Classes/SBJsonStreamParserState.m
@@ -33,21 +33,15 @@
#import "SBJsonStreamParserState.h"
#import "SBJsonStreamParser.h"
-static SBJsonStreamParserState *kStart;
-static SBJsonStreamParserState *kComplete;
-static SBJsonStreamParserState *kError;
-static SBJsonStreamParserState *kObjectStart;
-static SBJsonStreamParserState *kObjectGotKey;
-static SBJsonStreamParserState *kObjectSeparator;
-static SBJsonStreamParserState *kObjectGotValue;
-static SBJsonStreamParserState *kObjectNeedKey;
-static SBJsonStreamParserState *kArrayStart;
-static SBJsonStreamParserState *kArrayGotValue;
-static SBJsonStreamParserState *kArrayNeedValue;
+#define SINGLETON \
++ (id)sharedInstance { \
+ static id state; \
+ if (!state) state = [[self alloc] init]; \
+ return state; \
+}
@implementation SBJsonStreamParserState
-// Return nil as we should not instantiate the base class.
+ (id)sharedInstance { return nil; }
- (BOOL)parser:(SBJsonStreamParser*)parser shouldAcceptToken:(sbjson_token_t)token {
@@ -78,9 +72,7 @@ - (BOOL)isError {
@implementation SBJsonStreamParserStateStart
-+ (void)initialize { kStart = [[self alloc] init]; }
-
-- (id)sharedInstance { return kStart; }
+SINGLETON
- (BOOL)parser:(SBJsonStreamParser*)parser shouldAcceptToken:(sbjson_token_t)token {
return token == sbjson_token_array_start || token == sbjson_token_object_start;
@@ -126,10 +118,7 @@ - (NSString*)name { return @"before outer-most array or object"; }
@implementation SBJsonStreamParserStateComplete
-+ (void)initialize { kComplete = [[self alloc] init]; }
-
-- (id)sharedInstance { return kComplete; }
-
+SINGLETON
- (NSString*)name { return @"after outer-most array or object"; }
@@ -143,9 +132,7 @@ - (SBJsonStreamParserStatus)parserShouldReturn:(SBJsonStreamParser*)parser {
@implementation SBJsonStreamParserStateError
-+ (void)initialize { kError = [[self alloc] init]; }
-
-- (id)sharedInstance { return kError; }
+SINGLETON
- (NSString*)name { return @"in error"; }
@@ -163,9 +150,7 @@ - (BOOL)isError {
@implementation SBJsonStreamParserStateObjectStart
-+ (void)initialize { kObjectStart = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectStart; }
+SINGLETON
- (NSString*)name { return @"at beginning of object"; }
@@ -195,9 +180,7 @@ - (BOOL)needKey {
@implementation SBJsonStreamParserStateObjectGotKey
-+ (void)initialize { kObjectGotKey = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectGotKey; }
+SINGLETON
- (NSString*)name { return @"after object key"; }
@@ -215,9 +198,7 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectSeparator
-+ (void)initialize { kObjectSeparator = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectSeparator; }
+SINGLETON
- (NSString*)name { return @"as object value"; }
@@ -249,9 +230,7 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectGotValue
-+ (void)initialize { kObjectGotValue = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectGotValue; }
+SINGLETON
- (NSString*)name { return @"after object value"; }
@@ -278,9 +257,7 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectNeedKey
-+ (void)initialize { kObjectNeedKey = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectNeedKey; }
+SINGLETON
- (NSString*)name { return @"in place of object key"; }
@@ -302,9 +279,7 @@ - (BOOL)needKey {
@implementation SBJsonStreamParserStateArrayStart
-+ (void)initialize { kArrayStart = [[self alloc] init]; }
-
-- (id)sharedInstance { return kArrayStart; }
+SINGLETON
- (NSString*)name { return @"at array start"; }
@@ -332,9 +307,7 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateArrayGotValue
-+ (void)initialize { kArrayGotValue = [[self alloc] init]; }
-
-- (id)sharedInstance { return kArrayGotValue; }
+SINGLETON
- (NSString*)name { return @"after array value"; }
@@ -354,12 +327,11 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateArrayNeedValue
-+ (void)initialize { kArrayNeedValue = [[self alloc] init]; }
-
-- (id)sharedInstance { return kArrayNeedValue; }
+SINGLETON
- (NSString*)name { return @"as array value"; }
+
- (BOOL)parser:(SBJsonStreamParser*)parser shouldAcceptToken:(sbjson_token_t)token {
switch (token) {
case sbjson_token_array_end:
View
46 Classes/SBJsonStreamWriterState.m
@@ -33,14 +33,12 @@
#import "SBJsonStreamWriterState.h"
#import "SBJsonStreamWriter.h"
-static SBJsonStreamWriterState *kStart;
-static SBJsonStreamWriterState *kComplete;
-static SBJsonStreamWriterState *kError;
-static SBJsonStreamWriterState *kObjectStart;
-static SBJsonStreamWriterState *kObjectKey;
-static SBJsonStreamWriterState *kObjectValue;
-static SBJsonStreamWriterState *kArrayStart;
-static SBJsonStreamWriterState *kArrayValue;
+#define SINGLETON \
++ (id)sharedInstance { \
+ static id state; \
+ if (!state) state = [[self alloc] init]; \
+ return state; \
+}
@implementation SBJsonStreamWriterState
@@ -58,9 +56,7 @@ - (void)appendWhitespace:(SBJsonStreamWriter*)writer {
@implementation SBJsonStreamWriterStateObjectStart
-+ (void)initialize { kObjectStart = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectStart; }
+SINGLETON
- (void)transitionState:(SBJsonStreamWriter *)writer {
writer.state = [SBJsonStreamWriterStateObjectValue sharedInstance];
@@ -73,9 +69,7 @@ - (BOOL)expectingKey:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateObjectKey
-+ (void)initialize { kObjectKey = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectKey; }
+SINGLETON
- (void)appendSeparator:(SBJsonStreamWriter *)writer {
[writer appendBytes:"," length:1];
@@ -84,9 +78,7 @@ - (void)appendSeparator:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateObjectValue
-+ (void)initialize { kObjectValue = [[self alloc] init]; }
-
-- (id)sharedInstance { return kObjectValue; }
+SINGLETON
- (void)appendSeparator:(SBJsonStreamWriter *)writer {
[writer appendBytes:":" length:1];
@@ -101,9 +93,7 @@ - (void)appendWhitespace:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateArrayStart
-+ (void)initialize { kArrayStart = [[self alloc] init]; }
-
-- (id)sharedInstance { return kArrayStart; }
+SINGLETON
- (void)transitionState:(SBJsonStreamWriter *)writer {
writer.state = [SBJsonStreamWriterStateArrayValue sharedInstance];
@@ -112,9 +102,7 @@ - (void)transitionState:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateArrayValue
-+ (void)initialize { kArrayValue = [[self alloc] init]; }
-
-- (id)sharedInstance { return kArrayValue; }
+SINGLETON
- (void)appendSeparator:(SBJsonStreamWriter *)writer {
[writer appendBytes:"," length:1];
@@ -123,9 +111,8 @@ - (void)appendSeparator:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateStart
-+ (void)initialize { kStart = [[self alloc] init]; }
+SINGLETON
-- (id)sharedInstance { return kStart; }
- (void)transitionState:(SBJsonStreamWriter *)writer {
writer.state = [SBJsonStreamWriterStateComplete sharedInstance];
@@ -136,9 +123,7 @@ - (void)appendSeparator:(SBJsonStreamWriter *)writer {
@implementation SBJsonStreamWriterStateComplete
-+ (void)initialize { kComplete = [[self alloc] init]; }
-
-- (id)sharedInstance { return kComplete; }
+SINGLETON
- (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
writer.error = @"Stream is closed";
@@ -148,10 +133,7 @@ - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
@implementation SBJsonStreamWriterStateError
-+ (void)initialize { kError = [[self alloc] init]; }
-
-- (id)sharedInstance { return kError; }
-
+SINGLETON
@end
View
1  NEWS.md
@@ -17,7 +17,6 @@ Miscellaneous
* Added an optional comparator that is used when sorting keys.
* Be more memory-efficient when parsing long strings containing escaped characters.
* Add a Workspace that includes the sample projects, for ease of browsing.
-* Slight readability improvement on internal code.
3.0 (June 18th, 2011)
Please sign in to comment.
Something went wrong with that request. Please try again.