AsyncObject.h
1/* -*- C++ -*-
2 * Copyright (c) 2006-2009, Isode Limited, London, England.
3 * All rights reserved.
4 *
5 * Acquisition and use of this software and related materials for any
6 * purpose requires a written licence agreement from Isode Limited,
7 * or a written licence from an organisation licenced by Isode Limited
8 * to grant such a licence.
9 */
10
11/*
12 *
13 * \file AsyncObj.h
14 *
15 * \brief Enabler for facilitating submission of events to event manager
16 *
17 * @VERSION@
18 */
19
20#ifndef _ASYNCOBJ_H_
21#define _ASYNCOBJ_H_
22
23#include "EventSvc.h"
24
25#ifdef __cplusplus
26
27namespace Event {
28
30
31 class AsyncObject : public AsyncEvent {
32 public:
34 typedef void (*FuncPtr) (void *);
35
36 private:
37 FuncPtr func;
38 void *farg;
39
41 AsyncObject () : func (0), farg(0) {}
42
43 public:
45 virtual ~AsyncObject() {}
46
48 virtual void Deliver () {
49 if ( func ) (*func)(farg);
50 delete this;
51 }
52
54 inline static void Invoke (FuncPtr fnx, void *arg) {
55 AsyncObject *obj = new AsyncObject;
56 obj->func = fnx;
57 obj->farg = arg;
58 Event::Manager::GetManager()->Queue (obj, 0);
59 }
60
62 inline static void Invoke (FuncPtr fnx, void *arg, unsigned msecs) {
63 AsyncObject *obj = new AsyncObject;
64 obj->func = fnx;
65 obj->farg = arg;
66 Event::Manager::GetManager()->QueueAt (obj, msecs);
67 }
68
70 inline static void Invoke (FuncPtr fnx, void *arg,
71 const struct timespec *abstime) {
72 AsyncObject *obj = new AsyncObject;
73 obj->func = fnx;
74 obj->farg = arg;
75 Event::Manager::GetManager()->QueueAt (obj, abstime);
76 }
77 };
78
80 // to be invoked on the object. No arguments are passed to the method
81
82 template <class C> class AsyncMethod : public AsyncEvent {
84 typedef void (C::*MethodPtr) ();
85
86 MethodPtr method;
87 C *object;
88
90 AsyncMethod () : method (0) {}
91
92 public:
94 virtual ~AsyncMethod() {}
95
97 virtual void Deliver () {
98 if ( method && object ) (object->*method)();
99 delete this;
100 }
101
103 inline static void Invoke (C *obj, MethodPtr fnx) {
104 AsyncMethod *aobj = new AsyncMethod;
105 aobj->object = obj;
106 aobj->method = fnx;
107 Event::Manager::GetManager()->Queue (aobj, 0);
108 }
109 };
110
112 // to be invoked on the object. One argument is passed to the method
113
114 template <class C, class A> class AsyncMethodArg : public AsyncEvent {
116 typedef void (C::*MethodPtr) (A argument);
117
118 MethodPtr method;
119 C *object;
120 A marg;
121
123 AsyncMethodArg () : method (0), marg(0) {}
124
125 public:
127 virtual ~AsyncMethodArg() {}
128
130 virtual void Deliver () {
131 if ( method && object ) (object->*method)(marg);
132 delete this;
133 }
134
136 inline static void Invoke (C *obj, MethodPtr fnx, A arg) {
137 AsyncMethodArg *aobj = new AsyncMethodArg;
138 aobj->object = obj;
139 aobj->method = fnx;
140 aobj->marg = arg;
141 Event::Manager::GetManager()->Queue (aobj, 0);
142 }
143 };
144}
145
146#endif /* __cplusplus */
147
150IC_CDECL EVENTSVC_DLL void EventAsyncDispatch (
151 void (*func)(void *),
152 void *param
153);
154
155#endif /* _ASYNCOBJ_H_ */

All rights reserved © 2002 - 2024 Isode Ltd.