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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#ifndef ICE_ASYNC_RESULT_H
#define ICE_ASYNC_RESULT_H
#include <IceUtil/Monitor.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/UniquePtr.h>
#include <Ice/LocalObject.h>
#include <Ice/CommunicatorF.h>
#include <Ice/ConnectionF.h>
#include <Ice/ProxyF.h>
#include <Ice/InstanceF.h>
#include <Ice/RequestHandlerF.h>
#include <Ice/AsyncResultF.h>
#include <Ice/ObserverHelper.h>
#include <Ice/BasicStream.h>
#ifdef ICE_CPP11
# include <functional> // for std::function
#endif
namespace IceInternal
{
class CallbackBase;
typedef IceUtil::Handle<CallbackBase> CallbackBasePtr;
}
namespace Ice
{
class ICE_API AsyncResult : public Ice::LocalObject, private IceUtil::noncopyable
{
public:
void cancel();
Int getHash() const;
CommunicatorPtr getCommunicator() const;
virtual ConnectionPtr getConnection() const;
virtual ObjectPrx getProxy() const;
bool isCompleted() const;
void waitForCompleted();
bool isSent() const;
void waitForSent();
void throwLocalException() const;
bool sentSynchronously() const;
LocalObjectPtr getCookie() const;
const std::string& getOperation() const;
::IceInternal::BasicStream* __startReadParams()
{
_is.startReadEncaps();
return &_is;
}
void __endReadParams()
{
_is.endReadEncaps();
}
void __readEmptyParams()
{
_is.skipEmptyEncaps();
}
void __readParamEncaps(const ::Ice::Byte*& encaps, ::Ice::Int& sz)
{
_is.readEncaps(encaps, sz);
}
void __throwUserException();
bool __wait();
static void __check(const AsyncResultPtr&, const ::IceProxy::Ice::Object*, const ::std::string&);
static void __check(const AsyncResultPtr&, const Connection*, const ::std::string&);
static void __check(const AsyncResultPtr&, const Communicator*, const ::std::string&);
protected:
static void __check(const AsyncResultPtr&, const ::std::string&);
AsyncResult(const CommunicatorPtr&, const IceInternal::InstancePtr&, const std::string&,
const IceInternal::CallbackBasePtr&, const LocalObjectPtr&);
virtual ~AsyncResult(); // Must be heap-allocated
bool sent(bool);
bool finished(bool);
bool finished(const Exception&);
void invokeSentAsync();
void invokeCompletedAsync();
void invokeSent();
void invokeCompleted();
void cancel(const LocalException&);
virtual void cancelable(const IceInternal::CancellationHandlerPtr&);
void checkCanceled();
void warning(const std::exception&) const;
void warning() const;
//
// This virtual method is necessary for the communicator flush
// batch requests implementation.
//
virtual IceInternal::InvocationObserver& getObserver()
{
return _observer;
}
const IceInternal::InstancePtr _instance;
IceInternal::InvocationObserver _observer;
Ice::ConnectionPtr _cachedConnection;
bool _sentSynchronously;
IceInternal::BasicStream _is;
IceUtil::Monitor<IceUtil::Mutex> _monitor;
private:
const CommunicatorPtr _communicator;
const std::string& _operation;
const IceInternal::CallbackBasePtr _callback;
const LocalObjectPtr _cookie;
IceUtil::UniquePtr<Exception> _exception;
IceInternal::CancellationHandlerPtr _cancellationHandler;
IceUtil::UniquePtr<Ice::LocalException> _cancellationException;
static const unsigned char OK;
static const unsigned char Done;
static const unsigned char Sent;
static const unsigned char EndCalled;
static const unsigned char Canceled;
unsigned char _state;
};
}
namespace IceInternal
{
//
// Base class for all callbacks.
//
class ICE_API CallbackBase : public IceUtil::Shared
{
public:
void checkCallback(bool, bool);
virtual void completed(const ::Ice::AsyncResultPtr&) const = 0;
virtual CallbackBasePtr verify(const ::Ice::LocalObjectPtr&) = 0;
virtual void sent(const ::Ice::AsyncResultPtr&) const = 0;
virtual bool hasSentCallback() const = 0;
};
//
// Base class for generic callbacks.
//
class ICE_API GenericCallbackBase : virtual public CallbackBase
{
};
//
// See comments in OutgoingAsync.cpp
//
extern ICE_API CallbackBasePtr __dummyCallback;
//
// Generic callback template that requires the caller to down-cast the
// proxy and the cookie that are obtained from the AsyncResult.
//
template<class T>
class AsyncCallback : public GenericCallbackBase
{
public:
typedef T callback_type;
typedef IceUtil::Handle<T> TPtr;
typedef void (T::*Callback)(const ::Ice::AsyncResultPtr&);
AsyncCallback(const TPtr& instance, Callback cb, Callback sentcb = 0) :
_callback(instance), _completed(cb), _sent(sentcb)
{
checkCallback(instance, cb != 0);
}
virtual void completed(const ::Ice::AsyncResultPtr& result) const
{
(_callback.get()->*_completed)(result);
}
virtual CallbackBasePtr verify(const ::Ice::LocalObjectPtr&)
{
return this; // Nothing to do, the cookie is not type-safe.
}
virtual void sent(const ::Ice::AsyncResultPtr& result) const
{
if(_sent)
{
(_callback.get()->*_sent)(result);
}
}
virtual bool hasSentCallback() const
{
return _sent != 0;
}
private:
TPtr _callback;
Callback _completed;
Callback _sent;
};
#ifdef ICE_CPP11
template<typename T> struct callback_type
{
static const int value = 1;
};
template<> struct callback_type<void(const ::Ice::AsyncResultPtr&)>
{
static const int value = 2;
};
template<> struct callback_type<void(const ::Ice::Exception&)>
{
static const int value = 3;
};
template<typename Callable, typename = void> struct callable_type
{
static const int value = 1;
};
template<class Callable> struct callable_type<Callable, typename ::std::enable_if<
::std::is_class<Callable>::value &&
!::std::is_bind_expression<Callable>::value>::type>
{
template<typename T, T> struct TypeCheck;
template<typename T> struct AsyncResultCallback
{
typedef void (T::*ok)(const ::Ice::AsyncResultPtr&) const;
};
template<typename T> struct ExceptionCallback
{
typedef void (T::*ok)(const ::Ice::Exception&) const;
};
typedef char (&other)[1];
typedef char (&asyncResult)[2];
typedef char (&exception)[3];
template<typename T> static other check(...);
template<typename T> static asyncResult check(TypeCheck<typename AsyncResultCallback<T>::ok, &T::operator()>*);
template<typename T> static exception check(TypeCheck<typename ExceptionCallback<T>::ok, &T::operator()>*);
enum { value = sizeof(check<Callable>(0)) };
};
template<> struct callable_type<void(*)(const ::Ice::AsyncResultPtr&)>
{
static const int value = 2;
};
template<> struct callable_type<void(*)(const ::Ice::Exception&)>
{
static const int value = 3;
};
template<typename Callable, typename Callback> struct is_callable
{
static const bool value = callable_type<Callable>::value == callback_type<Callback>::value;
};
template<class S> class Function : public std::function<S>
{
public:
template<typename T> Function(T f, typename ::std::enable_if<is_callable<T, S>::value>::type* = 0)
: std::function<S>(f)
{
}
Function()
{
}
Function(::std::nullptr_t) : ::std::function<S>(nullptr)
{
}
};
#else
template<class S> class Function
{
public:
Function()
{
}
};
#endif
}
namespace Ice
{
typedef IceUtil::Handle< ::IceInternal::GenericCallbackBase> CallbackPtr;
template<class T> CallbackPtr
newCallback(const IceUtil::Handle<T>& instance,
void (T::*cb)(const AsyncResultPtr&),
void (T::*sentcb)(const AsyncResultPtr&) = 0)
{
return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);
}
template<class T> CallbackPtr
newCallback(T* instance,
void (T::*cb)(const AsyncResultPtr&),
void (T::*sentcb)(const AsyncResultPtr&) = 0)
{
return new ::IceInternal::AsyncCallback<T>(instance, cb, sentcb);
}
#ifdef ICE_CPP11
ICE_API CallbackPtr
newCallback(const ::IceInternal::Function<void (const AsyncResultPtr&)>&,
const ::IceInternal::Function<void (const AsyncResultPtr&)>& =
::IceInternal::Function<void (const AsyncResultPtr&)>());
#endif
//
// Operation callbacks are specified in Proxy.h
//
}
#endif
|