OSDN Git Service

6920462d9b2e30bb3b190f3aaa6db56ac9912ba4
[qt-creator-jp/qt-creator-jp.git] / src / shared / symbianutils / callback.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef DEBUGGER_CALLBACK_H
35 #define DEBUGGER_CALLBACK_H
36
37 #include "symbianutils_global.h"
38
39 namespace trk {
40 namespace Internal {
41
42 /* Helper class for the 1-argument functor:
43  * Cloneable base class for the implementation which is
44  * invokeable with the argument. */
45 template <class Argument>
46 class CallbackImplBase
47 {
48     Q_DISABLE_COPY(CallbackImplBase)
49 public:
50     CallbackImplBase() {}
51     virtual CallbackImplBase *clone() const = 0;
52     virtual void invoke(Argument a) = 0;
53     virtual ~CallbackImplBase() {}
54 };
55
56 /* Helper class for the 1-argument functor: Implementation for
57  * a class instance with a member function pointer. */
58 template <class Class, class Argument>
59 class CallbackMemberPtrImpl : public CallbackImplBase<Argument>
60 {
61 public:
62     typedef void (Class::*MemberFuncPtr)(Argument);
63
64     CallbackMemberPtrImpl(Class *instance,
65                           MemberFuncPtr memberFunc) :
66                           m_instance(instance),
67                           m_memberFunc(memberFunc) {}
68
69     virtual CallbackImplBase<Argument> *clone() const
70     {
71         return new CallbackMemberPtrImpl<Class, Argument>(m_instance, m_memberFunc);
72     }
73
74     virtual void invoke(Argument a)
75         { (m_instance->*m_memberFunc)(a); }
76 private:
77     Class *m_instance;
78     MemberFuncPtr m_memberFunc;
79 };
80
81 } // namespace Internal
82
83 /* Default-constructible, copyable 1-argument functor providing an
84  * operator()(Argument) that invokes a member function of a class:
85  * \code
86 class Foo {
87 public:
88     void print(const std::string &);
89 };
90 ...
91 Foo foo;
92 Callback<const std::string &> f1(&foo, &Foo::print);
93 f1("test");
94 \endcode */
95
96 template <class Argument>
97 class Callback
98 {
99 public:
100     Callback() : m_impl(0) {}
101
102     template <class Class>
103     Callback(Class *instance, void (Class::*memberFunc)(Argument)) :
104         m_impl(new Internal::CallbackMemberPtrImpl<Class,Argument>(instance, memberFunc))
105     {}
106
107     ~Callback()
108     {
109         clean();
110     }
111
112     Callback(const Callback &rhs) :
113         m_impl(0)
114     {
115         if (rhs.m_impl)
116             m_impl = rhs.m_impl->clone();
117     }
118
119     Callback &operator=(const Callback &rhs)
120     {
121         if (this != &rhs) {
122             clean();
123             if (rhs.m_impl)
124                 m_impl = rhs.m_impl->clone();
125         }
126         return *this;
127     }
128
129     bool isNull() const { return m_impl == 0; }
130     operator bool() const { return !isNull(); }
131
132     void operator()(Argument a)
133     {
134         if (m_impl)
135             m_impl->invoke(a);
136     }
137
138 private:
139     void clean()
140     {
141         if (m_impl) {
142             delete m_impl;
143             m_impl = 0;
144         }
145     }
146
147     Internal::CallbackImplBase<Argument> *m_impl;
148 };
149
150 } // namespace trk
151
152 #endif // DEBUGGER_CALLBACK_H