callbackscheduler.hxx 9.18 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//=============================================================================================================================
//
// EasyAR Sense 4.2.0.8700-7bcbc8b1c
// Copyright (c) 2015-2021 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
// EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
// and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
//
//=============================================================================================================================

#ifndef __EASYAR_CALLBACKSCHEDULER_HXX__
#define __EASYAR_CALLBACKSCHEDULER_HXX__

#include "easyar/types.hxx"

namespace easyar {

/// <summary>
/// Callback scheduler.
/// There are two subclasses: `DelayedCallbackScheduler`_ and `ImmediateCallbackScheduler`_ .
/// `DelayedCallbackScheduler`_ is used to delay callback to be invoked manually, and it can be used in single-threaded environments (such as various UI environments).
/// `ImmediateCallbackScheduler`_ is used to mark callback to be invoked when event is dispatched, and it can be used in multi-threaded environments (such as server or service daemon).
/// </summary>
class CallbackScheduler
{
protected:
    easyar_CallbackScheduler * cdata_ ;
    void init_cdata(easyar_CallbackScheduler * cdata);
    virtual CallbackScheduler & operator=(const CallbackScheduler & data) { return *this; } //deleted
public:
    CallbackScheduler(easyar_CallbackScheduler * cdata);
    virtual ~CallbackScheduler();

    CallbackScheduler(const CallbackScheduler & data);
    const easyar_CallbackScheduler * get_cdata() const;
    easyar_CallbackScheduler * get_cdata();

};

/// <summary>
/// Delayed callback scheduler.
/// It is used to delay callback to be invoked manually, and it can be used in single-threaded environments (such as various UI environments).
/// All members of this class is thread-safe.
/// </summary>
class DelayedCallbackScheduler : public CallbackScheduler
{
protected:
    easyar_DelayedCallbackScheduler * cdata_ ;
    void init_cdata(easyar_DelayedCallbackScheduler * cdata);
    virtual DelayedCallbackScheduler & operator=(const DelayedCallbackScheduler & data) { return *this; } //deleted
public:
    DelayedCallbackScheduler(easyar_DelayedCallbackScheduler * cdata);
    virtual ~DelayedCallbackScheduler();

    DelayedCallbackScheduler(const DelayedCallbackScheduler & data);
    const easyar_DelayedCallbackScheduler * get_cdata() const;
    easyar_DelayedCallbackScheduler * get_cdata();

    DelayedCallbackScheduler();
    /// <summary>
    /// Executes a callback. If there is no callback to execute, false is returned.
    /// </summary>
    bool runOne();
    static void tryCastFromCallbackScheduler(CallbackScheduler * v, /* OUT */ DelayedCallbackScheduler * * Return);
};

/// <summary>
/// Immediate callback scheduler.
/// It is used to mark callback to be invoked when event is dispatched, and it can be used in multi-threaded environments (such as server or service daemon).
/// All members of this class is thread-safe.
/// </summary>
class ImmediateCallbackScheduler : public CallbackScheduler
{
protected:
    easyar_ImmediateCallbackScheduler * cdata_ ;
    void init_cdata(easyar_ImmediateCallbackScheduler * cdata);
    virtual ImmediateCallbackScheduler & operator=(const ImmediateCallbackScheduler & data) { return *this; } //deleted
public:
    ImmediateCallbackScheduler(easyar_ImmediateCallbackScheduler * cdata);
    virtual ~ImmediateCallbackScheduler();

    ImmediateCallbackScheduler(const ImmediateCallbackScheduler & data);
    const easyar_ImmediateCallbackScheduler * get_cdata() const;
    easyar_ImmediateCallbackScheduler * get_cdata();

    /// <summary>
    /// Gets a default immediate callback scheduler.
    /// </summary>
    static void getDefault(/* OUT */ ImmediateCallbackScheduler * * Return);
    static void tryCastFromCallbackScheduler(CallbackScheduler * v, /* OUT */ ImmediateCallbackScheduler * * Return);
};

}

#endif

#ifndef __IMPLEMENTATION_EASYAR_CALLBACKSCHEDULER_HXX__
#define __IMPLEMENTATION_EASYAR_CALLBACKSCHEDULER_HXX__

#include "easyar/callbackscheduler.h"

namespace easyar {

inline CallbackScheduler::CallbackScheduler(easyar_CallbackScheduler * cdata)
    :
    cdata_(NULL)
{
    init_cdata(cdata);
}
inline CallbackScheduler::~CallbackScheduler()
{
    if (cdata_) {
        easyar_CallbackScheduler__dtor(cdata_);
        cdata_ = NULL;
    }
}

inline CallbackScheduler::CallbackScheduler(const CallbackScheduler & data)
    :
    cdata_(NULL)
{
    easyar_CallbackScheduler * cdata = NULL;
    easyar_CallbackScheduler__retain(data.cdata_, &cdata);
    init_cdata(cdata);
}
inline const easyar_CallbackScheduler * CallbackScheduler::get_cdata() const
{
    return cdata_;
}
inline easyar_CallbackScheduler * CallbackScheduler::get_cdata()
{
    return cdata_;
}
inline void CallbackScheduler::init_cdata(easyar_CallbackScheduler * cdata)
{
    cdata_ = cdata;
}

inline DelayedCallbackScheduler::DelayedCallbackScheduler(easyar_DelayedCallbackScheduler * cdata)
    :
    CallbackScheduler(static_cast<easyar_CallbackScheduler *>(NULL)),
    cdata_(NULL)
{
    init_cdata(cdata);
}
inline DelayedCallbackScheduler::~DelayedCallbackScheduler()
{
    if (cdata_) {
        easyar_DelayedCallbackScheduler__dtor(cdata_);
        cdata_ = NULL;
    }
}

inline DelayedCallbackScheduler::DelayedCallbackScheduler(const DelayedCallbackScheduler & data)
    :
    CallbackScheduler(static_cast<easyar_CallbackScheduler *>(NULL)),
    cdata_(NULL)
{
    easyar_DelayedCallbackScheduler * cdata = NULL;
    easyar_DelayedCallbackScheduler__retain(data.cdata_, &cdata);
    init_cdata(cdata);
}
inline const easyar_DelayedCallbackScheduler * DelayedCallbackScheduler::get_cdata() const
{
    return cdata_;
}
inline easyar_DelayedCallbackScheduler * DelayedCallbackScheduler::get_cdata()
{
    return cdata_;
}
inline void DelayedCallbackScheduler::init_cdata(easyar_DelayedCallbackScheduler * cdata)
{
    cdata_ = cdata;
    {
        easyar_CallbackScheduler * cdata_inner = NULL;
        easyar_castDelayedCallbackSchedulerToCallbackScheduler(cdata, &cdata_inner);
        CallbackScheduler::init_cdata(cdata_inner);
    }
}
inline DelayedCallbackScheduler::DelayedCallbackScheduler()
    :
    CallbackScheduler(static_cast<easyar_CallbackScheduler *>(NULL)),
    cdata_(NULL)
{
    easyar_DelayedCallbackScheduler * _return_value_ = NULL;
    easyar_DelayedCallbackScheduler__ctor(&_return_value_);
    init_cdata(_return_value_);
}
inline bool DelayedCallbackScheduler::runOne()
{
    if (cdata_ == NULL) {
        return bool();
    }
    bool _return_value_ = easyar_DelayedCallbackScheduler_runOne(cdata_);
    return _return_value_;
}
inline void DelayedCallbackScheduler::tryCastFromCallbackScheduler(CallbackScheduler * v, /* OUT */ DelayedCallbackScheduler * * Return)
{
    if (v == NULL) {
        *Return = NULL;
        return;
    }
    easyar_DelayedCallbackScheduler * cdata = NULL;
    easyar_tryCastCallbackSchedulerToDelayedCallbackScheduler(v->get_cdata(), &cdata);
    if (cdata == NULL) {
        *Return = NULL;
        return;
    }
    *Return = new DelayedCallbackScheduler(cdata);
}

inline ImmediateCallbackScheduler::ImmediateCallbackScheduler(easyar_ImmediateCallbackScheduler * cdata)
    :
    CallbackScheduler(static_cast<easyar_CallbackScheduler *>(NULL)),
    cdata_(NULL)
{
    init_cdata(cdata);
}
inline ImmediateCallbackScheduler::~ImmediateCallbackScheduler()
{
    if (cdata_) {
        easyar_ImmediateCallbackScheduler__dtor(cdata_);
        cdata_ = NULL;
    }
}

inline ImmediateCallbackScheduler::ImmediateCallbackScheduler(const ImmediateCallbackScheduler & data)
    :
    CallbackScheduler(static_cast<easyar_CallbackScheduler *>(NULL)),
    cdata_(NULL)
{
    easyar_ImmediateCallbackScheduler * cdata = NULL;
    easyar_ImmediateCallbackScheduler__retain(data.cdata_, &cdata);
    init_cdata(cdata);
}
inline const easyar_ImmediateCallbackScheduler * ImmediateCallbackScheduler::get_cdata() const
{
    return cdata_;
}
inline easyar_ImmediateCallbackScheduler * ImmediateCallbackScheduler::get_cdata()
{
    return cdata_;
}
inline void ImmediateCallbackScheduler::init_cdata(easyar_ImmediateCallbackScheduler * cdata)
{
    cdata_ = cdata;
    {
        easyar_CallbackScheduler * cdata_inner = NULL;
        easyar_castImmediateCallbackSchedulerToCallbackScheduler(cdata, &cdata_inner);
        CallbackScheduler::init_cdata(cdata_inner);
    }
}
inline void ImmediateCallbackScheduler::getDefault(/* OUT */ ImmediateCallbackScheduler * * Return)
{
    easyar_ImmediateCallbackScheduler * _return_value_ = NULL;
    easyar_ImmediateCallbackScheduler_getDefault(&_return_value_);
    *Return = new ImmediateCallbackScheduler(_return_value_);
}
inline void ImmediateCallbackScheduler::tryCastFromCallbackScheduler(CallbackScheduler * v, /* OUT */ ImmediateCallbackScheduler * * Return)
{
    if (v == NULL) {
        *Return = NULL;
        return;
    }
    easyar_ImmediateCallbackScheduler * cdata = NULL;
    easyar_tryCastCallbackSchedulerToImmediateCallbackScheduler(v->get_cdata(), &cdata);
    if (cdata == NULL) {
        *Return = NULL;
        return;
    }
    *Return = new ImmediateCallbackScheduler(cdata);
}

}

#endif