aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc/utils/MCOOperation.mm
blob: fe589c78b85d3a906d361c0113f142128b307297 (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
//
//  MCOOperation.m
//  mailcore2
//
//  Created by Matt Ronge on 01/31/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "MCOOperation.h"
#import "MCOOperation+Private.h"

#import "MCOperation.h"
#import "MCOperationCallback.h"
#import "MCOObjectWrapper.h"
#import "MCOUtils.h"

#import <Foundation/Foundation.h>

using namespace mailcore;

@interface MCOOperation ()
- (void)_operationCompleted;
@end

class MCOOperationCompletionCallback : public Object, public OperationCallback {
public:
    MCOOperationCompletionCallback(MCOOperation *op) {
        mOperation = op;
    }

    virtual void operationFinished(Operation * op)
    {
        [mOperation _operationCompleted];
    }

    private:
        MCOOperation *mOperation;
};

@implementation MCOOperation {
    Operation * _operation;
    MCOOperationCompletionCallback * _callback;
    BOOL _started;
}

#define nativeType mailcore::Operation

+ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object
{
    mailcore::Operation * op = (mailcore::Operation *) object;
    return [[[self alloc] initWithMCOperation:op] autorelease];
}

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

- (instancetype) initWithMCOperation:(Operation *)op
{
    self = [super init];
    
    _operation = op;
    _operation->retain();
    
    _callback = new MCOOperationCompletionCallback(self);
    _operation->setCallback(_callback);
    
    return self;
}

- (void) dealloc
{
    _operation->release();
    _callback->release();
    [super dealloc];
}

- (BOOL) isCancelled
{
    return MCO_NATIVE_INSTANCE->isCancelled();
}

MCO_OBJC_SYNTHESIZE_SCALAR(dispatch_queue_t, dispatch_queue_t, setCallbackDispatchQueue, callbackDispatchQueue);
MCO_OBJC_SYNTHESIZE_SCALAR(BOOL, bool, setShouldRunWhenCancelled, shouldRunWhenCancelled);

- (void) cancel
{
    if (_started) {
        _started = NO;
        [self release];
    }
    _operation->cancel();
}

- (void) start
{
    _started = YES;
    [self retain];
    _operation->start();
}

- (void) _operationCompleted
{
    _started = NO;
    [self operationCompleted];
    [self release];
}

- (void) operationCompleted
{
}

@end