aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc/utils/MCOIndexSet.mm
blob: 45a00ccc56a37aa7aaeb024907b121a1bc50b3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
//  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];
}

- (mailcore::Object *) mco_mcObject
{
    return _indexSet;
}

- (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