SqlLite sample
@interface SingletonBoilerPlate : NSObject {
}
+ (id)singleton;
+ (id)singletonWithZone:(NSZone*)zone;
//designated initializer, subclasses must implement and call supers implementation
- (id)initSingleton;
@end
#import "SingletonBoilerPlate.h"
@implementation SingletonBoilerPlate
static NSMutableDictionary *s_AbstractSingleton_singletons = nil;
+ (void)initialize
{
@synchronized([SingletonBoilerPlate class]) {
if (s_AbstractSingleton_singletons == nil) {
s_AbstractSingleton_singletons = [[NSMutableDictionary alloc] init];
}
}
}
// Should be considered private to the abstract singleton class,
// wrap with a "sharedXxx" class method
+ (id)singleton
{
return [self singletonWithZone:[self zone]];
}
// Should be considered private to the abstract singleton class
+ (id)singletonWithZone:(NSZone*)zone
{
id singleton = nil;
Class class = [self class];
if (class == [SingletonBoilerPlate class]) {
[NSException raise:NSInternalInconsistencyException
format:@"Not valid to request the abstract singleton."];
}
@synchronized([SingletonBoilerPlate class]) {
singleton = [s_AbstractSingleton_singletons objectForKey:class];
if (singleton == nil) {
// the analyhzer thinks this is a leak because singleton is not retained
// but the setObject below will retain it
singleton = NSAllocateObject(class, 0U, zone);
NSIncrementExtraRefCount(singleton);
if ((singleton = [singleton initSingleton]) != nil) {
[s_AbstractSingleton_singletons setObject:singleton forKey:class];
}
NSDecrementExtraRefCountWasZero(singleton);
}
}
return singleton;
}
// Designated initializer for instances. If subclasses override they
// must call this implementation.
- (id)initSingleton
{
return [super init];
}
// Disallow the normal default initializer for instances.
- (id)init
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
// ------------------------------------------------------------------------------
// The following overrides attempt to enforce singleton behavior.
+ (id)new
{
return [self singleton];
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self singletonWithZone:zone];
}
+ (id)alloc
{
return [self singleton];
}
- (id)copy
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)mutableCopy
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
#define DLog(...)
#import <Foundation/Foundation.h>
#import "SingletonBoilerPlate.h"
#import <sqlite3.h>
@interface Database : SingletonBoilerPlate {
sqlite3 *database;
}
+ (Database*)instance;
// you need to call these when you want to open/close the database
- (void)initializeDatabase;
- (void)shutDown;
-(void)persistSongs:(NSArray*)songlist;
-(void)loadSongs:(NSMutableArray*)songs;
-(void)deleteSongs;
@end
- (unsigned)retainCount
{
return UINT_MAX;
}
- (oneway void)release
{
}
- (id)retain
{
return self;
}
- (id)autorelease
{
return self;
}
- (void)dealloc
{
[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
[super dealloc];
}
// ------------------------------------------------------------------------------
@end
#import "Database.h"
@implementation Database
- (void)initializeDatabase{
}
-(void)persistSongs:(NSArray*)songlist{
}
-(void)loadSongs:(NSMutableArray*)songs{
}
-(void)deleteSongs{
}
+ (Database*)instance
{
static Database *s_Database = nil;
if (s_Database == nil) {
@synchronized([Database class]) {
s_Database = [self singleton];
}
}
return s_Database;
}
- (void) dealloc
{
[self shutDown];
[super dealloc];
}
-(void)shutDown
{
DLog(@"");
if (database)
{
@synchronized(self)
{
// I think alot of these queries are done so irregularly that we shouln't kepe them around for the
// whole time and just create/destroy as needed
sqlite3_close(database);
database = NULL;
}
}
}
@end
}
+ (id)singleton;
+ (id)singletonWithZone:(NSZone*)zone;
//designated initializer, subclasses must implement and call supers implementation
- (id)initSingleton;
@end
#import "SingletonBoilerPlate.h"
@implementation SingletonBoilerPlate
static NSMutableDictionary *s_AbstractSingleton_singletons = nil;
+ (void)initialize
{
@synchronized([SingletonBoilerPlate class]) {
if (s_AbstractSingleton_singletons == nil) {
s_AbstractSingleton_singletons = [[NSMutableDictionary alloc] init];
}
}
}
// Should be considered private to the abstract singleton class,
// wrap with a "sharedXxx" class method
+ (id)singleton
{
return [self singletonWithZone:[self zone]];
}
// Should be considered private to the abstract singleton class
+ (id)singletonWithZone:(NSZone*)zone
{
id singleton = nil;
Class class = [self class];
if (class == [SingletonBoilerPlate class]) {
[NSException raise:NSInternalInconsistencyException
format:@"Not valid to request the abstract singleton."];
}
@synchronized([SingletonBoilerPlate class]) {
singleton = [s_AbstractSingleton_singletons objectForKey:class];
if (singleton == nil) {
// the analyhzer thinks this is a leak because singleton is not retained
// but the setObject below will retain it
singleton = NSAllocateObject(class, 0U, zone);
NSIncrementExtraRefCount(singleton);
if ((singleton = [singleton initSingleton]) != nil) {
[s_AbstractSingleton_singletons setObject:singleton forKey:class];
}
NSDecrementExtraRefCountWasZero(singleton);
}
}
return singleton;
}
// Designated initializer for instances. If subclasses override they
// must call this implementation.
- (id)initSingleton
{
return [super init];
}
// Disallow the normal default initializer for instances.
- (id)init
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
// ------------------------------------------------------------------------------
// The following overrides attempt to enforce singleton behavior.
+ (id)new
{
return [self singleton];
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self singletonWithZone:zone];
}
+ (id)alloc
{
return [self singleton];
}
- (id)copy
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)mutableCopy
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
return self;
}
#define DLog(...)
#import <Foundation/Foundation.h>
#import "SingletonBoilerPlate.h"
#import <sqlite3.h>
@interface Database : SingletonBoilerPlate {
sqlite3 *database;
}
+ (Database*)instance;
// you need to call these when you want to open/close the database
- (void)initializeDatabase;
- (void)shutDown;
-(void)persistSongs:(NSArray*)songlist;
-(void)loadSongs:(NSMutableArray*)songs;
-(void)deleteSongs;
@end
- (unsigned)retainCount
{
return UINT_MAX;
}
- (oneway void)release
{
}
- (id)retain
{
return self;
}
- (id)autorelease
{
return self;
}
- (void)dealloc
{
[self doesNotRecognizeSelector:_cmd]; //optional, do if you want to force certain usage pattern
[super dealloc];
}
// ------------------------------------------------------------------------------
@end
#import "Database.h"
@implementation Database
- (void)initializeDatabase{
}
-(void)persistSongs:(NSArray*)songlist{
}
-(void)loadSongs:(NSMutableArray*)songs{
}
-(void)deleteSongs{
}
+ (Database*)instance
{
static Database *s_Database = nil;
if (s_Database == nil) {
@synchronized([Database class]) {
s_Database = [self singleton];
}
}
return s_Database;
}
- (void) dealloc
{
[self shutDown];
[super dealloc];
}
-(void)shutDown
{
DLog(@"");
if (database)
{
@synchronized(self)
{
// I think alot of these queries are done so irregularly that we shouln't kepe them around for the
// whole time and just create/destroy as needed
sqlite3_close(database);
database = NULL;
}
}
}
@end