diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18e3c60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ + +*.xcworkspacedata + +*.xcuserstate + +*.xcscheme + +*/xcschememanagement.plist + +Scroller.xcodeproj/xcuserdata/andrewromanov.xcuserdatad/xcschemes/xcschememanagement.plist diff --git a/Classes/HorizontalTableView.h b/Classes/HorizontalTableView.h index 9cc821f..6251c8f 100644 --- a/Classes/HorizontalTableView.h +++ b/Classes/HorizontalTableView.h @@ -22,6 +22,8 @@ // OR OTHER DEALINGS IN THE SOFTWARE. #import +#import "Memory.h" + @class HorizontalTableView; @@ -44,12 +46,10 @@ NSInteger _visibleColumnCount; NSNumber *_columnWidth; - id _delegate; - NSMutableArray *_columnPool; } -@property (assign) IBOutlet id delegate; +@property (MWeakProperty) IBOutlet id delegate; - (void)refreshData; - (UIView *)dequeueColumnView; diff --git a/Classes/HorizontalTableView.m b/Classes/HorizontalTableView.m index df805f1..42361fc 100644 --- a/Classes/HorizontalTableView.m +++ b/Classes/HorizontalTableView.m @@ -23,6 +23,7 @@ #import "HorizontalTableView.h" + #define kColumnPoolSize 3 @interface HorizontalTableView() @@ -44,6 +45,13 @@ - (void)removeColumn:(NSInteger)index; @end +@interface HorizontalTableView (Private) + +- (void)_removePages; + +@end + + @implementation HorizontalTableView @synthesize pageViews=_pageViews; @@ -53,103 +61,146 @@ @implementation HorizontalTableView @synthesize columnPool=_columnPool; - -- (void)refreshData { +- (void)refreshData +{ + [self _removePages]; self.pageViews = [NSMutableArray array]; // to save time and memory, we won't load the page views immediately NSUInteger numberOfPhysicalPages = [self numberOfPages]; for (NSUInteger i = 0; i < numberOfPhysicalPages; ++i) + { [self.pageViews addObject:[NSNull null]]; - + } + [self setNeedsLayout]; } -- (NSUInteger)numberOfPages { + +- (NSUInteger)numberOfPages +{ NSInteger numPages = 0; - if (_delegate) + + if (_delegate) + { numPages = [_delegate numberOfColumnsForTableView:self]; - return numPages; + } + + return numPages; } -- (UIView *)viewForPhysicalPage:(NSUInteger)pageIndex { + +- (UIView *)viewForPhysicalPage:(NSUInteger)pageIndex +{ NSParameterAssert(pageIndex >= 0); NSParameterAssert(pageIndex < [self.pageViews count]); UIView *pageView; - if ([self.pageViews objectAtIndex:pageIndex] == [NSNull null]) { - - if (_delegate) { + if ([self.pageViews objectAtIndex:pageIndex] == [NSNull null]) + { + if (_delegate) + { pageView = [_delegate tableView:self viewForIndex:pageIndex]; [self.pageViews replaceObjectAtIndex:pageIndex withObject:pageView]; [self.scrollView addSubview:pageView]; DLog(@"View loaded for page %d", pageIndex); } - } else { + } + else + { pageView = [self.pageViews objectAtIndex:pageIndex]; } + return pageView; } -- (CGSize)pageSize { - CGRect rect = self.scrollView.bounds; + +- (CGSize)pageSize +{ + CGRect rect = self.scrollView.bounds; return rect.size; } -- (CGFloat)columnWidth { - if (!_columnWidth) { - if (_delegate) { - CGFloat width = [_delegate columnWidthForTableView:self]; - _columnWidth = [[NSNumber numberWithFloat:width] retain]; + +- (CGFloat)columnWidth +{ + if (!_columnWidth) + { + if (_delegate) + { + CGFloat width = [_delegate columnWidthForTableView:self]; + MASSIGN_RETAIN(_columnWidth, @(width)); } } + return [_columnWidth floatValue]; - } -- (BOOL)isPhysicalPageLoaded:(NSUInteger)pageIndex { - return [self.pageViews objectAtIndex:pageIndex] != [NSNull null]; + +- (BOOL)isPhysicalPageLoaded:(NSUInteger)pageIndex +{ + BOOL isLoaded = [self.pageViews objectAtIndex:pageIndex] != [NSNull null]; + return isLoaded; } -- (void)layoutPhysicalPage:(NSUInteger)pageIndex { + +- (void)layoutPhysicalPage:(NSUInteger)pageIndex +{ UIView *pageView = [self viewForPhysicalPage:pageIndex]; - CGFloat viewWidth = pageView.bounds.size.width; + CGFloat viewWidth = pageView.bounds.size.width; CGSize pageSize = [self pageSize]; - - CGRect rect = CGRectMake(viewWidth * pageIndex, 0, viewWidth, pageSize.height); + + CGRect rect = CGRectMake(viewWidth * pageIndex, 0, viewWidth, pageSize.height); pageView.frame = rect; } -- (void)awakeFromNib { - [self prepareView]; + +- (void)awakeFromNib +{ + [super awakeFromNib]; + [self prepareView]; } -- (void)queueColumnView:(UIView *)vw { - if ([self.columnPool count] >= kColumnPoolSize) { + +- (void)queueColumnView:(UIView *)vw +{ + if ([self.columnPool count] >= kColumnPoolSize) + { return; } + [self.columnPool addObject:vw]; } -- (UIView *)dequeueColumnView { - UIView *vw = [[self.columnPool lastObject] retain]; - if (vw) { + +- (UIView *)dequeueColumnView +{ + UIView *vw = nil; + MASSIGN_RETAIN(vw, [self.columnPool lastObject]); + if (vw) + { [self.columnPool removeLastObject]; DLog(@"Supply from reuse pool"); - } - return [vw autorelease]; + } + + return MReturnAutoreleased(vw); } -- (void)removeColumn:(NSInteger)index { - if ([self.pageViews objectAtIndex:index] != [NSNull null]) { - DLog(@"Removing view at position %d", index); - UIView *vw = [self.pageViews objectAtIndex:index]; - [self queueColumnView:vw]; - [vw removeFromSuperview]; - [self.pageViews replaceObjectAtIndex:index withObject:[NSNull null]]; - } + +- (void)removeColumn:(NSInteger)index +{ + if ([self.pageViews objectAtIndex:index] != [NSNull null]) + { + DLog(@"Removing view at position %d", index); + UIView *vw = [self.pageViews objectAtIndex:index]; + [self queueColumnView:vw]; + [vw removeFromSuperview]; + [self.pageViews replaceObjectAtIndex:index withObject:[NSNull null]]; + } } -- (void)currentPageIndexDidChange { + +- (void)currentPageIndexDidChange +{ CGSize pageSize = [self pageSize]; CGFloat columnWidth = [self columnWidth]; _visibleColumnCount = pageSize.width / columnWidth + 2; @@ -157,50 +208,62 @@ - (void)currentPageIndexDidChange { NSInteger leftMostPageIndex = -1; NSInteger rightMostPageIndex = 0; - for (NSInteger i = -2; i < _visibleColumnCount; i++) { + for (NSInteger i = -2; i < _visibleColumnCount; i++) + { NSInteger index = _currentPhysicalPageIndex + i; - if (index < [self.pageViews count] && (index >= 0)) { + if (index < [self.pageViews count] && (index >= 0)) + { [self layoutPhysicalPage:index]; if (leftMostPageIndex < 0) - leftMostPageIndex = index; + { + leftMostPageIndex = index; + } rightMostPageIndex = index; } } // clear out views to the left - for (NSInteger i = 0; i < leftMostPageIndex; i++) { + for (NSInteger i = 0; i < leftMostPageIndex; i++) + { [self removeColumn:i]; } // clear out views to the right - for (NSInteger i = rightMostPageIndex + 1; i < [self.pageViews count]; i++) { + for (NSInteger i = rightMostPageIndex + 1; i < [self.pageViews count]; i++) + { [self removeColumn:i]; } - - } -- (void)layoutPages { + +- (void)layoutPages +{ CGSize pageSize = self.bounds.size; self.scrollView.contentSize = CGSizeMake([self.pageViews count] * [self columnWidth], pageSize.height); } -- (id)init { + +- (id)init +{ self = [super init]; - if (self) { + if (self) + { [self prepareView]; } + return self; } -- (void)prepareView { - _columnPool = [[NSMutableArray alloc] initWithCapacity:kColumnPoolSize]; - _columnWidth = nil; + +- (void)prepareView +{ + MASSIGN_RETAIN(_columnPool, [NSMutableArray arrayWithCapacity:kColumnPoolSize]); + MRELEASE_NIL(_columnWidth); [self setClipsToBounds:YES]; self.autoresizesSubviews = YES; - UIScrollView *scroller = [[UIScrollView alloc] init]; + UIScrollView *scroller = MReturnAutoreleased([[UIScrollView alloc] init]); CGRect rect = self.bounds; scroller.frame = rect; scroller.backgroundColor = [UIColor blackColor]; @@ -214,16 +277,18 @@ - (void)prepareView { scroller.alwaysBounceVertical = NO; self.scrollView = scroller; [self addSubview:scroller]; - [scroller release], scroller = nil; } -- (NSUInteger)physicalPageIndex { +- (NSUInteger)physicalPageIndex +{ NSUInteger page = self.scrollView.contentOffset.x / [self columnWidth]; return page; } -- (void)setPhysicalPageIndex:(NSUInteger)newIndex { + +- (void)setPhysicalPageIndex:(NSUInteger)newIndex +{ self.scrollView.contentOffset = CGPointMake(newIndex * [self pageSize].width, 0); } @@ -231,7 +296,8 @@ - (void)setPhysicalPageIndex:(NSUInteger)newIndex { #pragma mark - #pragma mark UIScrollViewDelegate methods -- (void)scrollViewDidScroll:(UIScrollView *)scrollView { +- (void)scrollViewDidScroll:(UIScrollView *)scrollView +{ //DLog(@"Did Scroll"); NSUInteger newPageIndex = self.physicalPageIndex; @@ -245,24 +311,33 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { DLog(@"CSize = %@", NSStringFromCGSize(rect)); } -- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { + +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate +{ } -- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { + +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView +{ DLog(@"scrollViewDidEndDecelerating"); } -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation +{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -- (void)layoutSubviews { +- (void)layoutSubviews +{ [self layoutPages]; [self currentPageIndexDidChange]; } -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { + +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation +{ // adjust frames according to the new page size - this does not cause any visible changes [self layoutPages]; self.physicalPageIndex = _currentPhysicalPageIndex; @@ -278,12 +353,31 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO } -- (void)dealloc { - [_columnPool release], _columnPool = nil; - [_columnWidth release], _columnWidth = nil; - [_pageViews release], _pageViews = nil; - [_scrollView release], _scrollView = nil; - [super dealloc]; +- (void)dealloc +{ + MRELEASE_NIL(_columnPool); + MRELEASE_NIL(_columnWidth); + MRELEASE_NIL(_pageViews); + MRELEASE_NIL(_scrollView); + MSuperDealloc; +} + +@end + + +#pragma mark - +@implementation HorizontalTableView (Private) + +- (void)_removePages +{ + [self.pageViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if ([obj respondsToSelector:@selector(removeFromSuperview)]) + { + [obj removeFromSuperview]; + } + }]; + + [self.pageViews removeAllObjects]; } @end diff --git a/Classes/PageLoopViewController.m b/Classes/PageLoopViewController.m index d579e84..021bd27 100644 --- a/Classes/PageLoopViewController.m +++ b/Classes/PageLoopViewController.m @@ -23,6 +23,8 @@ #import "PageLoopViewController.h" #import +#import "Memory.h" + @implementation PageLoopViewController @@ -32,16 +34,8 @@ @implementation PageLoopViewController #pragma mark - #pragma mark Memory management - -- (void)didReceiveMemoryWarning { - // Releases the view if it doesn't have a superview. - [super didReceiveMemoryWarning]; - - // Release any cached data, images, etc that aren't in use. -} - - -- (void)viewDidUnload { +- (void)viewDidUnload +{ [super viewDidUnload]; // Release any retained subviews of the main view. self.tableView = nil; @@ -49,27 +43,18 @@ - (void)viewDidUnload { } -- (void)dealloc { - [tableView release], tableView = nil; - [columnView release], columnView = nil; - [super dealloc]; +- (void)dealloc +{ + tableView.delegate = nil; + MRELEASE_NIL(tableView); + MRELEASE_NIL(columnView); + MSuperDealloc; } #pragma mark - - -/* - // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. -- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { - if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { - // Custom initialization - } - return self; -} -*/ - - // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. -- (void)viewDidLoad { +- (void)viewDidLoad +{ [super viewDidLoad]; CALayer *layer = [self.tableView layer]; @@ -78,19 +63,20 @@ - (void)viewDidLoad { NSMutableArray *colorArray = [[NSMutableArray alloc] init]; NSInteger step = 5; - for (NSInteger i = 0; i < 255; i += step) { + for (NSInteger i = 0; i < 255; i += step) + { CGFloat f = (float)i/255.0f; UIColor *clr = [UIColor colorWithRed:f green:f blue:f alpha:1.0f]; [colorArray addObject:clr]; } + colors = colorArray; - // [self.tableView refreshData]; [self.tableView performSelector:@selector(refreshData) withObject:nil afterDelay:0.3f]; } - -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation +{ // Overriden to allow any orientation. return YES; } @@ -98,14 +84,17 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface #pragma mark - #pragma mark HorizontalTableViewDelegate methods -- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView { +- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView +{ return [colors count]; } -- (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger)index { - - UIView *vw = [aTableView dequeueColumnView]; - if (!vw) { + +- (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger)index +{ + UIView *vw = [aTableView dequeueColumnView]; + if (!vw) + { DLog(@"Constructing new view"); [[NSBundle mainBundle] loadNibNamed:@"ColumnView" owner:self options:nil]; @@ -114,7 +103,6 @@ - (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger) } [vw setBackgroundColor:[colors objectAtIndex:index]]; - UILabel *lbl = (UILabel *)[vw viewWithTag:1234]; lbl.text = [NSString stringWithFormat:@"%d", index]; @@ -122,7 +110,9 @@ - (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger) return vw; } -- (CGFloat)columnWidthForTableView:(HorizontalTableView *)tableView { + +- (CGFloat)columnWidthForTableView:(HorizontalTableView *)tableView +{ return 150.0f; } diff --git a/Classes/ScrollerAppDelegate.m b/Classes/ScrollerAppDelegate.m index 1a55e01..08d534e 100644 --- a/Classes/ScrollerAppDelegate.m +++ b/Classes/ScrollerAppDelegate.m @@ -23,12 +23,13 @@ #import "ScrollerAppDelegate.h" #import "PageLoopViewController.h" +#import "Memory.h" @implementation ScrollerAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - + viewController = [[PageLoopViewController alloc] init]; window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; @@ -42,9 +43,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( - (void)dealloc { - [viewController release]; - [window release]; - [super dealloc]; + MRELEASE_NIL(viewController); + MRELEASE_NIL(window); + + MSuperDealloc; } diff --git a/Default-568h@2x.png b/Default-568h@2x.png new file mode 100644 index 0000000..0891b7a Binary files /dev/null and b/Default-568h@2x.png differ diff --git a/Scroller.xcodeproj/project.pbxproj b/Scroller.xcodeproj/project.pbxproj index eafdcb6..38ec5ce 100755 --- a/Scroller.xcodeproj/project.pbxproj +++ b/Scroller.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 1356407B18A4A58B0008C9EC /* Memory.m in Sources */ = {isa = PBXBuildFile; fileRef = 1356407A18A4A58B0008C9EC /* Memory.m */; }; + 1356407D18A4AB620008C9EC /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 1356407C18A4AB620008C9EC /* Default-568h@2x.png */; }; 1D3623260D0F684500981E51 /* ScrollerAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* ScrollerAppDelegate.m */; }; 1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; }; 1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; @@ -21,6 +23,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1356407918A4A58B0008C9EC /* Memory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Memory.h; sourceTree = ""; }; + 1356407A18A4A58B0008C9EC /* Memory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Memory.m; sourceTree = ""; }; + 1356407C18A4AB620008C9EC /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 1D3623240D0F684500981E51 /* ScrollerAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollerAppDelegate.h; sourceTree = ""; }; 1D3623250D0F684500981E51 /* ScrollerAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ScrollerAppDelegate.m; sourceTree = ""; }; @@ -68,6 +73,23 @@ path = Classes; sourceTree = ""; }; + 1356407718A4A58B0008C9EC /* libs */ = { + isa = PBXGroup; + children = ( + 1356407818A4A58B0008C9EC /* Memory */, + ); + path = libs; + sourceTree = ""; + }; + 1356407818A4A58B0008C9EC /* Memory */ = { + isa = PBXGroup; + children = ( + 1356407918A4A58B0008C9EC /* Memory.h */, + 1356407A18A4A58B0008C9EC /* Memory.m */, + ); + path = Memory; + sourceTree = ""; + }; 19C28FACFE9D520D11CA2CBB /* Products */ = { isa = PBXGroup; children = ( @@ -79,6 +101,8 @@ 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( + 1356407C18A4AB620008C9EC /* Default-568h@2x.png */, + 1356407718A4A58B0008C9EC /* libs */, 080E96DDFE201D6D7F000001 /* Classes */, 29B97315FDCFA39411CA2CEA /* Other Sources */, 29B97317FDCFA39411CA2CEA /* Resources */, @@ -144,9 +168,15 @@ /* Begin PBXProject section */ 29B97313FDCFA39411CA2CEA /* Project object */ = { isa = PBXProject; + attributes = { + }; buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Scroller" */; compatibilityVersion = "Xcode 3.1"; + developmentRegion = English; hasScannedForEncodings = 1; + knownRegions = ( + en, + ); mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; projectDirPath = ""; projectRoot = ""; @@ -162,6 +192,7 @@ buildActionMask = 2147483647; files = ( 5B5084E811A6EB8800791658 /* ipadicon.png in Resources */, + 1356407D18A4AB620008C9EC /* Default-568h@2x.png in Resources */, 5B88014C11A88BA400C7895C /* PageLoopViewController.xib in Resources */, 5B83CCE911A992F2002573B0 /* ColumnView.xib in Resources */, ); @@ -177,6 +208,7 @@ 1D60589B0D05DD56006BFB54 /* main.m in Sources */, 1D3623260D0F684500981E51 /* ScrollerAppDelegate.m in Sources */, 5B50846C11A6E69200791658 /* HorizontalTableView.m in Sources */, + 1356407B18A4A58B0008C9EC /* Memory.m in Sources */, 5B88014B11A88BA400C7895C /* PageLoopViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -195,6 +227,7 @@ GCC_PREFIX_HEADER = Scroller_Prefix.pch; INFOPLIST_FILE = "Scroller-Info.plist"; PRODUCT_NAME = Scroller; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; @@ -207,6 +240,7 @@ GCC_PREFIX_HEADER = Scroller_Prefix.pch; INFOPLIST_FILE = "Scroller-Info.plist"; PRODUCT_NAME = Scroller; + TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -214,8 +248,10 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; "ARCHS[sdk=iphoneos*]" = armv7; "ARCHS[sdk=iphonesimulator*]" = i386; + CLANG_ENABLE_OBJC_ARC = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; GCC_C_LANGUAGE_STANDARD = c99; GCC_PREPROCESSOR_DEFINITIONS = DEBUG; @@ -230,8 +266,10 @@ C01FCF5008A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"; "ARCHS[sdk=iphoneos*]" = armv7; "ARCHS[sdk=iphonesimulator*]" = i386; + CLANG_ENABLE_OBJC_ARC = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; diff --git a/libs/Memory/Memory.h b/libs/Memory/Memory.h new file mode 100644 index 0000000..1b317ab --- /dev/null +++ b/libs/Memory/Memory.h @@ -0,0 +1,68 @@ +// +// Memory.h +// +// Created by Andrew Romanov on 30.12.09. +// Copyright 2009 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface Memory : NSObject +{ + +} ++ (void)setRetain:(NSObject**)currObj newObj:(NSObject*)newObj; ++ (void)assignCurrObj:(NSObject**)currObj newObj:(NSObject*)newObj; ++ (void)releaseWithNil:(NSObject**)obj; +@end + + +#if ! __has_feature(objc_arc) +#define MAutorelease(__v) ([__v autorelease]) +#define MReturnAutoreleased(__v) ([__v autorelease]) + +#define MRetain(__v) ([__v retain]) +#define MReturnRetained MRetain + +#define MRelease(__v) ([__v release]) +#define MSafeRelease(__v) ([__v release], __v = nil) +#define MSuperDealloc ([super dealloc]) + +#define MWeak +#define MStrong + +#define MASSIGN_RETAIN(dst, src) [Memory setRetain:&dst newObj:src] +#define MRELEASE_NIL(obj) [Memory releaseWithNil:&obj] + +#define MStartAutoreleasePoll NSAutoreleasePool* _autoreleasePoolStarted = [[NSAutoreleasePool alloc] init]; +#define MEndAutoreleasePoll [_autoreleasePoolStarted drain]; + +#define MWeakProperty assign + +#else +// -fobjc-arc +#define MAutorelease(__v) +#define MReturnAutoreleased(__v) (__v) + +#define MRetain(__v) +#define MReturnRetained(__v) (__v) + +#define MRelease(__v) +#define MSafeRelease(__v) (__v = nil) +#define MSuperDealloc + +#define MWeak __weak +#define MStrong __strong + +#define MASSIGN_RETAIN(dst, src) (dst = src) +#define MRELEASE_NIL(obj) obj = nil + +#define MStartAutoreleasePoll @autoreleasepool { +#define MEndAutoreleasePoll } + +#define MWeakProperty strong + +#endif + + diff --git a/libs/Memory/Memory.m b/libs/Memory/Memory.m new file mode 100644 index 0000000..440bf22 --- /dev/null +++ b/libs/Memory/Memory.m @@ -0,0 +1,49 @@ +// +// Memory.h +// +// Created by Andrew Romanov on 30.12.09. +// Copyright 2009 __MyCompanyName__. All rights reserved. +// + +#import "Memory.h" + + +@implementation Memory +//---------------------------------------------------------- ++ (void)setRetain:(NSObject**)currObj newObj:(NSObject*)newObj +{ + if(*currObj != newObj) + { + if(*currObj) + { + MRelease(*currObj); + } + + *currObj = MReturnRetained(newObj); + } +} +//--------------------------------------------------------- ++ (void)releaseWithNil:(NSObject**)obj +{ + if(*obj) + { + MRelease(*obj); + *obj = nil; + } +} +//---------------------------------------------------------- ++ (void)assignCurrObj:(NSObject**)currObj newObj:(NSObject*)newObj +{ + if(*currObj != newObj) + { + #ifndef __OPTIMIZE__ + if((*currObj != nil) && (newObj == nil)) + { + NSLog(@"WARNING:assign nil object to not nil poiner"); + } + #endif + *currObj = newObj; + } +} + +@end diff --git a/main.m b/main.m index 08ae1c4..c582eca 100644 --- a/main.m +++ b/main.m @@ -22,11 +22,13 @@ // OR OTHER DEALINGS IN THE SOFTWARE. #import +#import "Memory.h" -int main(int argc, char *argv[]) { - - NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - int retVal = UIApplicationMain(argc, argv, nil, @"ScrollerAppDelegate"); - [pool release]; - return retVal; +int main(int argc, char *argv[]) +{ + int retVal = 0; + MStartAutoreleasePoll + retVal = UIApplicationMain(argc, argv, nil, @"ScrollerAppDelegate"); + MEndAutoreleasePoll + return retVal; }