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