diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-03-24 23:26:55 -0700 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-03-24 23:26:55 -0700 |
commit | b41efcc17b184ce75a648e26f85662c68756a912 (patch) | |
tree | ad8932dc955dff61ccb0edc5764f53d98b2570ed /src/objc/utils | |
parent | 2a6128d2d7782be77bcf5dc8d5087dd8eb493eab (diff) |
Implemented IMAP ObjC API
Diffstat (limited to 'src/objc/utils')
-rw-r--r-- | src/objc/utils/MCOIndexSet+Private.h | 20 | ||||
-rw-r--r-- | src/objc/utils/MCOIndexSet.h | 33 | ||||
-rw-r--r-- | src/objc/utils/MCOIndexSet.mm | 145 | ||||
-rw-r--r-- | src/objc/utils/MCORange.h | 39 | ||||
-rw-r--r-- | src/objc/utils/MCORange.mm | 79 | ||||
-rw-r--r-- | src/objc/utils/NSObject+MCO.mm | 1 |
6 files changed, 317 insertions, 0 deletions
diff --git a/src/objc/utils/MCOIndexSet+Private.h b/src/objc/utils/MCOIndexSet+Private.h new file mode 100644 index 00000000..78a3554e --- /dev/null +++ b/src/objc/utils/MCOIndexSet+Private.h @@ -0,0 +1,20 @@ +// +// MCOIndexSet+Private.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/24/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#ifndef mailcore2_MCOIndexSet_Private_h +#define mailcore2_MCOIndexSet_Private_h + +#ifdef __cplusplus +@interface MCOIndexSet (Private) + +- (id) initWithMCIndexSet:(mailcore::IndexSet *)indexSet; + +@end +#endif + +#endif diff --git a/src/objc/utils/MCOIndexSet.h b/src/objc/utils/MCOIndexSet.h new file mode 100644 index 00000000..3e9cc613 --- /dev/null +++ b/src/objc/utils/MCOIndexSet.h @@ -0,0 +1,33 @@ +// +// MCOIndexSet.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/23/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import <Foundation/Foundation.h> + +#import <mailcore/MCORange.h> + +// similar to NSMutableIndexSet but supports int64_t + +@interface MCOIndexSet : NSObject <NSCopying> + ++ (MCOIndexSet *) indexSet; ++ (MCOIndexSet *) indexSetWithRange:(MCORange)range; ++ (MCOIndexSet *) indexSetWithIndex:(uint64_t)idx; + +- (unsigned int) count; +- (void) addIndex:(uint64_t)idx; +- (void) removeIndex:(uint64_t)idx; +- (BOOL) containsIndex:(uint64_t)idx; + +- (void) addRange:(MCORange)range; +- (void) removeRange:(MCORange)range; +- (void) intersectsRange:(MCORange)range; + +- (MCORange *) allRanges; +- (unsigned int) rangesCount; + +@end diff --git a/src/objc/utils/MCOIndexSet.mm b/src/objc/utils/MCOIndexSet.mm new file mode 100644 index 00000000..ea8abaab --- /dev/null +++ b/src/objc/utils/MCOIndexSet.mm @@ -0,0 +1,145 @@ +// +// MCOIndexSet.m +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/23/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import "MCOIndexSet.h" + +#include <mailcore/MCBaseTypes.h> + +#import "NSObject+MCO.h" + +@implementation MCOIndexSet { + mailcore::IndexSet * _indexSet; +} + +#define nativeType mailcore::IndexSet + ++ (void) load +{ + MCORegisterClass(self, &typeid(nativeType)); +} + +- (id) copyWithZone:(NSZone *)zone +{ + nativeType * nativeObject = (nativeType *) [self mco_mcObject]->copy(); + id result = [[self class] mco_objectWithMCObject:nativeObject]; + MC_SAFE_RELEASE(nativeObject); + return [result retain]; +} + ++ (id) mco_objectWithMCObject:(mailcore::Object *)object +{ + mailcore::IndexSet * part = (mailcore::IndexSet *) object; + return [[[self alloc] initWithMCIndexSet:part] autorelease]; +} + +- (id) init +{ + mailcore::IndexSet * indexSet = new mailcore::IndexSet(); + self = [self initWithMCIndexSet:indexSet]; + indexSet->release(); + return self; +} + +- (id) initWithMCIndexSet:(mailcore::IndexSet *)indexSet +{ + self = [super init]; + _indexSet = indexSet; + _indexSet->retain(); + return self; +} + +- (void) dealloc +{ + MC_SAFE_RELEASE(_indexSet); + [super dealloc]; +} + ++ (MCOIndexSet *) indexSet +{ + return [[[MCOIndexSet alloc] init] autorelease]; +} + ++ (MCOIndexSet *) indexSetWithRange:(MCORange)range +{ + MCOIndexSet * indexSet; + indexSet = [[[MCOIndexSet alloc] init] autorelease]; + [indexSet addRange:range]; + return indexSet; +} + ++ (MCOIndexSet *) indexSetWithIndex:(uint64_t)idx +{ + MCOIndexSet * indexSet; + indexSet = [[[MCOIndexSet alloc] init] autorelease]; + [indexSet addIndex:idx]; + return indexSet; +} + +- (NSString *) description +{ + return MCO_OBJC_BRIDGE_GET(description); +} + +- (unsigned int) count +{ + return _indexSet->count(); +} + +- (void) addIndex:(uint64_t)idx +{ + _indexSet->addIndex(idx); +} + +- (void) removeIndex:(uint64_t)idx +{ + _indexSet->removeIndex(idx); +} + +- (BOOL) containsIndex:(uint64_t)idx +{ + return _indexSet->containsIndex(idx); +} + +- (void) addRange:(MCORange)range +{ + _indexSet->addRange(MCORangeToMCRange(range)); +} + +- (void) removeRange:(MCORange)range +{ + _indexSet->removeRange(MCORangeToMCRange(range)); +} + +- (void) intersectsRange:(MCORange)range +{ + _indexSet->intersectsRange(MCORangeToMCRange(range)); +} + +- (MCORange *) allRanges +{ +#if 0 + NSMutableData * result = [[[NSMutableData alloc] init] autorelease]; + unsigned int count = _indexSet->rangesCount(); + mailcore::Range * mcRanges = _indexSet->allRanges(); + [result setLength:count * sizeof(mailcore::Range)]; + MCORange * ranges = (MCORange *) [result mutableBytes]; + for(unsigned int i = 0 ; i < count ; i ++) { + ranges[i] = MCORangeWithMCRange(mcRanges[i]); + } + return ranges; +#else + return (MCORange *) _indexSet->allRanges(); +#endif +} + +- (unsigned int) rangesCount +{ + return _indexSet->rangesCount(); +} + +@end diff --git a/src/objc/utils/MCORange.h b/src/objc/utils/MCORange.h new file mode 100644 index 00000000..d918bf9e --- /dev/null +++ b/src/objc/utils/MCORange.h @@ -0,0 +1,39 @@ +// +// MCORange.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/24/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#ifndef mailcore2_MCORange_h +#define mailcore2_MCORange_h + +#import <Foundation/Foundation.h> + +#ifdef __cplusplus +#include <mailcore/MCBaseTypes.h> +#endif + +@class MCOIndexSet; + +typedef struct { + uint64_t location; + uint64_t length; +} MCORange; + +extern MCORange RangeEmpty; + +MCORange MCORangeMake(uint64_t location, uint64_t length); +MCOIndexSet * MCORangeRemoveRange(MCORange range1, MCORange range2); +MCOIndexSet * MCORangeUnion(MCORange range1, MCORange range2); +#ifdef __cplusplus +mailcore::Range MCORangeToMCRange(MCORange range); +MCORange MCORangeWithMCRange(mailcore::Range range); +#endif +MCORange MCORangeIntersection(MCORange range1, MCORange range2); +BOOL MCORangeHasIntersection(MCORange range1, MCORange range2); +uint64_t MCORangeLeftBound(MCORange range); +uint64_t MCORangeRightBound(MCORange range); + +#endif diff --git a/src/objc/utils/MCORange.mm b/src/objc/utils/MCORange.mm new file mode 100644 index 00000000..d1681f14 --- /dev/null +++ b/src/objc/utils/MCORange.mm @@ -0,0 +1,79 @@ +// +// MCORange.c +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/24/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import "MCORange.h" + +#import "MCOIndexSet.h" +#import "MCOIndexSet+Private.h" + +#include <string.h> + +MCORange MCORangeMake(uint64_t location, uint64_t length) +{ + MCORange result; + result.location = location; + result.length = length; + return result; +} + +MCOIndexSet * MCORangeRemoveRange(MCORange range1, MCORange range2) +{ + mailcore::Range mcRange1 = MCORangeToMCRange(range1); + mailcore::Range mcRange2 = MCORangeToMCRange(range2); + mailcore::IndexSet * indexSet = mailcore::RangeRemoveRange(mcRange1, mcRange2); + return [[[MCOIndexSet alloc] initWithMCIndexSet:indexSet] autorelease]; +} + +MCOIndexSet * MCORangeUnion(MCORange range1, MCORange range2) +{ + mailcore::Range mcRange1 = MCORangeToMCRange(range1); + mailcore::Range mcRange2 = MCORangeToMCRange(range2); + mailcore::IndexSet * indexSet = mailcore::RangeUnion(mcRange1, mcRange2); + return [[[MCOIndexSet alloc] initWithMCIndexSet:indexSet] autorelease]; +} + +mailcore::Range MCORangeToMCRange(MCORange range) +{ + return mailcore::RangeMake(range.location, range.length); +} + +MCORange MCORangeWithMCRange(mailcore::Range range) +{ + MCORange result; + result.location = range.location; + result.length = range.length; + return result; +} + +MCORange MCORangeIntersection(MCORange range1, MCORange range2) +{ + mailcore::Range mcRange1 = MCORangeToMCRange(range1); + mailcore::Range mcRange2 = MCORangeToMCRange(range2); + mailcore::Range mcResult = mailcore::RangeIntersection(mcRange1, mcRange2); + return MCORangeWithMCRange(mcResult); +} + +BOOL MCORangeHasIntersection(MCORange range1, MCORange range2) +{ + mailcore::Range mcRange1 = MCORangeToMCRange(range1); + mailcore::Range mcRange2 = MCORangeToMCRange(range2); + return mailcore::RangeHasIntersection(mcRange1, mcRange2); +} + +uint64_t MCORangeLeftBound(MCORange range) +{ + mailcore::Range mcRange = MCORangeToMCRange(range); + return mailcore::RangeLeftBound(mcRange); +} + +uint64_t MCORangeRightBound(MCORange range) +{ + mailcore::Range mcRange = MCORangeToMCRange(range); + return mailcore::RangeRightBound(mcRange); +} + diff --git a/src/objc/utils/NSObject+MCO.mm b/src/objc/utils/NSObject+MCO.mm index e4163553..27a9befe 100644 --- a/src/objc/utils/NSObject+MCO.mm +++ b/src/objc/utils/NSObject+MCO.mm @@ -108,6 +108,7 @@ static Class classWithTypeInfo(const std::type_info * info) } else { MCAssert(0); + return nil; } } |