Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -69,10 +69,12 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ 4B0108C910EB8C9300631877 /* OFEnumerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFEnumerator.h; path = src/OFEnumerator.h; sourceTree = SOURCE_ROOT; }; 4B0108CA10EB8C9300631877 /* OFEnumerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFEnumerator.m; path = src/OFEnumerator.m; sourceTree = SOURCE_ROOT; }; + 4B175C1D116D130B003C99CB /* OFApplication.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFApplication.h; path = src/OFApplication.h; sourceTree = SOURCE_ROOT; }; + 4B175C1E116D130B003C99CB /* OFApplication.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFApplication.m; path = src/OFApplication.m; sourceTree = SOURCE_ROOT; }; 4B4986DF1101F64500A2CFDA /* objc_properties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = objc_properties.m; path = src/objc_properties.m; sourceTree = SOURCE_ROOT; }; 4B6799561099E7C50041064A /* asprintf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = asprintf.h; path = src/asprintf.h; sourceTree = ""; }; 4B6799581099E7C50041064A /* objc_sync.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = objc_sync.m; path = src/objc_sync.m; sourceTree = ""; }; 4B67995A1099E7C50041064A /* OFArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFArray.h; path = src/OFArray.h; sourceTree = ""; }; 4B67995B1099E7C50041064A /* OFArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFArray.m; path = src/OFArray.m; sourceTree = ""; }; @@ -133,10 +135,12 @@ /* Begin PBXGroup section */ 08FB7794FE84155DC02AAC07 /* ObjFW */ = { isa = PBXGroup; children = ( + 4B175C1D116D130B003C99CB /* OFApplication.h */, + 4B175C1E116D130B003C99CB /* OFApplication.m */, 4B67995A1099E7C50041064A /* OFArray.h */, 4B67995B1099E7C50041064A /* OFArray.m */, 4B67995C1099E7C50041064A /* OFAutoreleasePool.h */, 4B67995D1099E7C50041064A /* OFAutoreleasePool.m */, 4B67995E1099E7C50041064A /* OFConstString.h */, Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -2,11 +2,12 @@ LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = ${OBJFW_LIB_MAJOR} LIB_MINOR = ${OBJFW_LIB_MINOR} -SRCS = OFArray.m \ +SRCS = OFApplication.m \ + OFArray.m \ OFAutoreleasePool.m \ OFConstString.m \ OFDataArray.m \ OFDictionary.m \ OFExceptions.m \ ADDED src/OFApplication.h Index: src/OFApplication.h ================================================================== --- src/OFApplication.h +++ src/OFApplication.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2008 - 2010 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "OFObject.h" + +@class OFArray; +@class OFMutableArray; + +/** + * \brief A protocol for delegates of OFApplication, + */ +@protocol OFApplicationDelegate +/** + * This method is called when the application was initialized and is running + * now. + */ +- (void)applicationDidFinishLaunching; + +/** + * This method is called when the application is going to get terminated. + */ +- (void)applicationWillTerminate; +@end + +/** + * \brief Represents the application as an object. + */ +@interface OFApplication: OFObject +{ + OFMutableArray *arguments; + id delegate; +} + +/** + * \return The only OFApplication instance in the application + */ ++ sharedApplication; + +/** + * \return The arguments passed to the application + */ ++ (OFArray*)arguments; + +/** + * Terminates the application. + */ ++ (void)terminate; + +/** + * Sets argc and argv. + * + * You should not call this directly! Use of_application_main instead! + * + * \param argc The number of arguments + * \param argv The argument values + */ +- setArgumentCount: (int)argc + andArgumentValues: (char**)argv; + +/** + * \return The arguments passed to the application + */ +- (OFArray*)arguments; + +/** + * \return The delegate of the application + */ +- (id)delegate; + +/** + * Sets the delegate of the application. + * + * \param delegate The delegate for the application + */ +- setDelegate: (id)delegate; + +/** + * Starts the application after everything has been initialized. + */ +- run; + +/** + * Terminates the application. + */ +- (void)terminate; +@end + +@interface OFObject (OFApplicationDelegate) +@end + +extern int of_application_main(int, char*[], Class); ADDED src/OFApplication.m Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2008 - 2010 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#include "config.h" + +#include + +#import "OFApplication.h" +#import "OFArray.h" +#import "OFString.h" +#import "OFAutoreleasePool.h" + +OFApplication *app = nil; + +static void +atexit_handler() +{ + id delegate = [app delegate]; + + [delegate applicationWillTerminate]; +} + +int +of_application_main(int argc, char *argv[], Class cls) +{ + OFApplication *app; + id delegate = nil; + + if (cls != Nil) + delegate = [[cls alloc] init]; + + app = [OFApplication sharedApplication]; + + [app setArgumentCount: argc + andArgumentValues: argv]; + + [app setDelegate: delegate]; + [delegate release]; + + [app run]; + + return 0; +} + +@implementation OFApplication ++ sharedApplication +{ + if (app == nil) + app = [[self alloc] init]; + + return app; +} + ++ (OFArray*)arguments +{ + return [app arguments]; +} + ++ (void)terminate +{ + exit(0); +} + +- init +{ + self = [super init]; + + atexit(atexit_handler); + + return self; +} + +- setArgumentCount: (int)argc + andArgumentValues: (char**)argv +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + int i; + + if (arguments != nil) + [arguments release]; + + arguments = [[OFMutableArray alloc] init]; + + for (i = 0; i < argc; i++) + [arguments addObject: [OFString stringWithCString: argv[i]]]; + + [pool release]; + + return self; +} + +- (OFArray*)arguments +{ + return [[arguments retain] autorelease]; +} + +- (id)delegate +{ + return [[delegate retain] autorelease]; +} + +- setDelegate: (id)delegate_ +{ + id old = delegate; + delegate = [delegate_ retain]; + [old release]; + + return self; +} + +- run +{ + [delegate applicationDidFinishLaunching]; + + return self; +} + +- (void)terminate +{ + exit(0); +} + +- (void)dealloc +{ + [arguments release]; + [delegate release]; + + [super dealloc]; +} +@end + +@implementation OFObject (OFApplicationDelegate) +- (void)applicationDidFinishLaunching +{ +} + +- (void)applicationWillTerminate +{ +} +@end Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -34,10 +34,12 @@ #import "OFSocket.h" #import "OFTCPSocket.h" #import "OFHashes.h" #import "OFXMLElement.h" + +#import "OFApplication.h" #import "macros.h" #ifdef OF_PLUGINS # import "OFPlugin.h"