luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AsyncCaller.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef ASYNCCALLER_H
23 #define ASYNCCALLER_H
24 
25 #include "Common.h"
26 
27 #include <glib.h>
28 
30 {
31 public:
32 
33  AsyncCallerBase(GMainLoop* loop, gint sourcePriority);
34  virtual ~AsyncCallerBase();
35 
36  void call();
37 
38 protected:
39 
40  virtual void dispatch() = 0;
41 
42 private:
43 
44  static gboolean callback(GIOChannel* channel, GIOCondition condition, gpointer arg);
45 
46  int m_pipeFd[2];
47  GIOChannel* m_ioChannel;
48  GSource* m_ioSource;
49  GStaticRecMutex* m_mutex;
50 };
51 
52 template <class Target>
54 {
55 public:
56 
57  typedef void (Target::*DispatchFunction)();
58 
59  AsyncCaller(Target* target, DispatchFunction function, GMainLoop* loop, int sourcePriority=G_PRIORITY_DEFAULT)
60  : AsyncCallerBase(loop, sourcePriority)
61  , m_target(target)
62  , m_function(function) {}
63 
64 private:
65 
66  virtual void dispatch() { return (m_target->*m_function)(); }
67 
68  Target* m_target;
69  DispatchFunction m_function;
70 };
71 
72 
73 #endif /* ASYNCCALLER_H */