Permalink
Browse files

Make +sharedInstance implementations in SBJsonStreamParserState* and …

…SBJsonStreamWriterState* _slightly_ more efficient, and _a lot_ more readable
  • Loading branch information...
Stig Brautaset Stig Brautaset
Stig Brautaset authored and Stig Brautaset committed Mar 14, 2012
1 parent d6e735b commit 49725dfde98cb9db93c2dbda70cd1995400695fc
Showing with 46 additions and 19 deletions.
  1. +0 −1 Classes/SBJsonStreamParserState.h
  2. +46 −18 Classes/SBJsonStreamParserState.m
@@ -57,7 +57,6 @@
@interface SBJsonStreamParserStateError : SBJsonStreamParserState
@end
-
@interface SBJsonStreamParserStateObjectStart : SBJsonStreamParserState
@end
@@ -33,15 +33,21 @@
#import "SBJsonStreamParserState.h"
#import "SBJsonStreamParser.h"
-#define SINGLETON \
-+ (id)sharedInstance { \
- static id state; \
- if (!state) state = [[self alloc] init]; \
- return state; \
-}
+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;
@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 {
@@ -72,7 +78,9 @@ - (BOOL)isError {
@implementation SBJsonStreamParserStateStart
-SINGLETON
++ (void)initialize { kStart = [[self alloc] init]; }
+
+- (id)sharedInstance { return kStart; }
- (BOOL)parser:(SBJsonStreamParser*)parser shouldAcceptToken:(sbjson_token_t)token {
return token == sbjson_token_array_start || token == sbjson_token_object_start;
@@ -118,7 +126,10 @@ - (NSString*)name { return @"before outer-most array or object"; }
@implementation SBJsonStreamParserStateComplete
-SINGLETON
++ (void)initialize { kComplete = [[self alloc] init]; }
+
+- (id)sharedInstance { return kComplete; }
+
- (NSString*)name { return @"after outer-most array or object"; }
@@ -132,7 +143,9 @@ - (SBJsonStreamParserStatus)parserShouldReturn:(SBJsonStreamParser*)parser {
@implementation SBJsonStreamParserStateError
-SINGLETON
++ (void)initialize { kError = [[self alloc] init]; }
+
+- (id)sharedInstance { return kError; }
- (NSString*)name { return @"in error"; }
@@ -150,7 +163,9 @@ - (BOOL)isError {
@implementation SBJsonStreamParserStateObjectStart
-SINGLETON
++ (void)initialize { kObjectStart = [[self alloc] init]; }
+
+- (id)sharedInstance { return kObjectStart; }
- (NSString*)name { return @"at beginning of object"; }
@@ -180,7 +195,9 @@ - (BOOL)needKey {
@implementation SBJsonStreamParserStateObjectGotKey
-SINGLETON
++ (void)initialize { kObjectGotKey = [[self alloc] init]; }
+
+- (id)sharedInstance { return kObjectGotKey; }
- (NSString*)name { return @"after object key"; }
@@ -198,7 +215,9 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectSeparator
-SINGLETON
++ (void)initialize { kObjectSeparator = [[self alloc] init]; }
+
+- (id)sharedInstance { return kObjectSeparator; }
- (NSString*)name { return @"as object value"; }
@@ -230,7 +249,9 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectGotValue
-SINGLETON
++ (void)initialize { kObjectGotValue = [[self alloc] init]; }
+
+- (id)sharedInstance { return kObjectGotValue; }
- (NSString*)name { return @"after object value"; }
@@ -257,7 +278,9 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateObjectNeedKey
-SINGLETON
++ (void)initialize { kObjectNeedKey = [[self alloc] init]; }
+
+- (id)sharedInstance { return kObjectNeedKey; }
- (NSString*)name { return @"in place of object key"; }
@@ -279,7 +302,9 @@ - (BOOL)needKey {
@implementation SBJsonStreamParserStateArrayStart
-SINGLETON
++ (void)initialize { kArrayStart = [[self alloc] init]; }
+
+- (id)sharedInstance { return kArrayStart; }
- (NSString*)name { return @"at array start"; }
@@ -307,7 +332,9 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateArrayGotValue
-SINGLETON
++ (void)initialize { kArrayGotValue = [[self alloc] init]; }
+
+- (id)sharedInstance { return kArrayGotValue; }
- (NSString*)name { return @"after array value"; }
@@ -327,10 +354,11 @@ - (void)parser:(SBJsonStreamParser*)parser shouldTransitionTo:(sbjson_token_t)to
@implementation SBJsonStreamParserStateArrayNeedValue
-SINGLETON
++ (void)initialize { kArrayNeedValue = [[self alloc] init]; }
-- (NSString*)name { return @"as array value"; }
+- (id)sharedInstance { return kArrayNeedValue; }
+- (NSString*)name { return @"as array value"; }
- (BOOL)parser:(SBJsonStreamParser*)parser shouldAcceptToken:(sbjson_token_t)token {
switch (token) {

0 comments on commit 49725df

Please sign in to comment.