author | zecke <zecke> | 2003-08-27 08:01:57 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-08-27 08:01:57 (UTC) |
commit | 151c319a9a67ae420136fb23cc987d23059737f6 (patch) (unidiff) | |
tree | 79d8bff48e2e05b0934a90463364d84632bf68b6 /libopie | |
parent | dbd38957ae8a24bc29f45a6b80cc17f45d8776df (diff) | |
download | opie-151c319a9a67ae420136fb23cc987d23059737f6.zip opie-151c319a9a67ae420136fb23cc987d23059737f6.tar.gz opie-151c319a9a67ae420136fb23cc987d23059737f6.tar.bz2 |
Did I say I love C++ templates.
MetaProgramming is fun
Here is my template based on Lokis and Simons work to
quicklaunch applications
-rw-r--r-- | libopie/oapplicationfactory.h | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/libopie/oapplicationfactory.h b/libopie/oapplicationfactory.h new file mode 100644 index 0000000..418a82e --- a/dev/null +++ b/libopie/oapplicationfactory.h | |||
@@ -0,0 +1,262 @@ | |||
1 | /* | ||
2 | This work is derived from: | ||
3 | ---- | ||
4 | The Loki Library | ||
5 | Copyright (c) 2001 by Andrei Alexandrescu | ||
6 | This code accompanies the book: | ||
7 | Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design | ||
8 | Patterns Applied". Copyright (c) 2001. Addison-Wesley. | ||
9 | Permission to use, copy, modify, distribute and sell this software for any | ||
10 | purpose is hereby granted without fee, provided that the above copyright | ||
11 | notice appear in all copies and that both that copyright notice and this | ||
12 | permission notice appear in supporting documentation. | ||
13 | The author or Addison-Welsey Longman make no representations about the | ||
14 | suitability of this software for any purpose. It is provided "as is" | ||
15 | without express or implied warranty. | ||
16 | ---- | ||
17 | |||
18 | And KGenericFactor et all from Simon Hausmann <tronical@kde.org> | ||
19 | |||
20 | */ | ||
21 | |||
22 | #include <qstring.h> | ||
23 | #include <qmetaobject.h> | ||
24 | |||
25 | #include <qtopia/qcom.h> | ||
26 | #include <qtopia/applicationinterface.h> | ||
27 | |||
28 | namespace Opie { | ||
29 | struct NullType; | ||
30 | |||
31 | template <class T, class U> | ||
32 | struct Typelist | ||
33 | { | ||
34 | typedef T Head; | ||
35 | typedef U Tail; | ||
36 | }; | ||
37 | template< | ||
38 | typename T1 = NullType, typename T2 = NullType, typename T3 = NullType, | ||
39 | typename T4 = NullType, typename T5 = NullType, typename T6 = NullType, | ||
40 | typename T7 = NullType, typename T8 = NullType, typename T9 = NullType, | ||
41 | typename T10 = NullType, typename T11 = NullType, typename T12 = NullType, | ||
42 | typename T13 = NullType, typename T14 = NullType, typename T15 = NullType, | ||
43 | typename T16 = NullType, typename T17 = NullType, typename T18 = NullType | ||
44 | > | ||
45 | struct MakeTypelist{ | ||
46 | private: | ||
47 | typedef typename MakeTypelist | ||
48 | < | ||
49 | T2 , T3 , T4 , | ||
50 | T5 , T6 , T7 , | ||
51 | T8 , T9 , T10, | ||
52 | T11, T12, T13, | ||
53 | T14, T15, T16, | ||
54 | T17, T18 | ||
55 | > | ||
56 | ::Result TailResult; | ||
57 | |||
58 | public: | ||
59 | typedef Typelist<T1, TailResult> Result; | ||
60 | }; | ||
61 | |||
62 | template<> | ||
63 | struct MakeTypelist<> | ||
64 | { | ||
65 | typedef NullType Result; | ||
66 | }; | ||
67 | |||
68 | } | ||
69 | |||
70 | /** | ||
71 | * To allow your application to be quick launched some one needs | ||
72 | * to create the QWidget. | ||
73 | * This is this factory. Make surce your widget has static QString Widget::appName() | ||
74 | * as one of its functions. | ||
75 | * | ||
76 | * This template takes one QWidget and initialized it in the form of | ||
77 | * MyWidget::MyWidget( QWidget* parent, const char* name, WFlags f ); | ||
78 | * | ||
79 | * To use it on your app do that: | ||
80 | * typedef OApplicationFactory<MyWidget> MyFactory; | ||
81 | * OPIE_EXPORT_APP( MyFactory ) | ||
82 | * | ||
83 | */ | ||
84 | template <class Product> | ||
85 | struct OApplicationFactory : public ApplicationInterface { | ||
86 | QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) { | ||
87 | *iface = 0; | ||
88 | if ( uuid == IID_QUnknown ) *iface = this; | ||
89 | else if ( uuid == IID_QtopiaApplication ) *iface = this; | ||
90 | else return QS_FALSE; | ||
91 | (*iface)->addRef(); | ||
92 | return QS_OK; | ||
93 | } | ||
94 | |||
95 | /* | ||
96 | * | ||
97 | */ | ||
98 | virtual QWidget *createMainWindow( const QString& appName, QWidget* parent, | ||
99 | const char* name, Qt::WFlags f ) { | ||
100 | if (appName == Product::appName() ) | ||
101 | return new Product(parent, name, f ); | ||
102 | else | ||
103 | return 0l; | ||
104 | } | ||
105 | |||
106 | virtual QStringList applications()const { | ||
107 | QStringList list; | ||
108 | list << Product::appName() ; | ||
109 | |||
110 | return list; | ||
111 | } | ||
112 | Q_REFCOUNT | ||
113 | |||
114 | }; | ||
115 | |||
116 | |||
117 | /* Internal */ | ||
118 | |||
119 | template< class Product > | ||
120 | struct OPrivate { | ||
121 | inline static QWidget *multiFactory( const QString& appName, QWidget* parent, | ||
122 | const char* name, Qt::WFlags fl ) { | ||
123 | if ( appName == Product::appName() ) | ||
124 | return new Product( parent, name, fl ); | ||
125 | else | ||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | inline static QStringList multiString( const QStringList& _list ) { | ||
130 | QStringList list = _list; | ||
131 | list << Product::appName(); | ||
132 | return list; | ||
133 | } | ||
134 | }; | ||
135 | |||
136 | template <> | ||
137 | struct OPrivate<Opie::NullType > { | ||
138 | inline static QWidget* multiFactory ( const QString& , QWidget* , | ||
139 | const char* , Qt::WFlags ) { | ||
140 | return 0l; | ||
141 | } | ||
142 | inline static QStringList multiString( const QStringList& _list ) { | ||
143 | return _list; | ||
144 | } | ||
145 | }; | ||
146 | |||
147 | /* | ||
148 | template <> | ||
149 | struct OPrivate <Opie::NullType, Opie::NullType > { | ||
150 | inline static QWidget* multiFactory( const QString& , QWidget* , | ||
151 | const char* , Qt::WFlags ) { | ||
152 | return 0l; | ||
153 | } | ||
154 | |||
155 | inline static QStringList multiString( const QStringList& _list ) { | ||
156 | return _list; | ||
157 | } | ||
158 | }; | ||
159 | */ | ||
160 | |||
161 | template <class Product, class ProductListTail> | ||
162 | struct OPrivate< Opie::Typelist<Product, ProductListTail> > { | ||
163 | inline static QWidget* multiFactory( const QString& appName, QWidget* parent, | ||
164 | const char* name, Qt::WFlags fl) { | ||
165 | QWidget* wid = OPrivate<Product>::multiFactory( appName, parent, name, fl ); | ||
166 | |||
167 | if (!wid ) | ||
168 | wid = OPrivate<ProductListTail>::multiFactory( appName, parent, name, fl ); | ||
169 | |||
170 | return wid; | ||
171 | } | ||
172 | |||
173 | inline static QStringList multiString( const QStringList& _list ) { | ||
174 | QStringList list = _list; | ||
175 | |||
176 | list = OPrivate<Product>::multiString( list ); | ||
177 | list = OPrivate<ProductListTail>::multiString( list ); | ||
178 | |||
179 | return list; | ||
180 | } | ||
181 | }; | ||
182 | |||
183 | |||
184 | |||
185 | |||
186 | |||
187 | |||
188 | |||
189 | |||
190 | /* Internal END */ | ||
191 | |||
192 | /* | ||
193 | * If you want to export more than one Widget use that function | ||
194 | * Make sure all your Widgets provide the appName() static method | ||
195 | * otherwise you'll get a compiler error | ||
196 | * | ||
197 | * typedef Opie::MakeTypeList<MyWidget, MyDialog, MyMediaPlayer >::Result MyTypes; | ||
198 | * OPIE_EXPORT_APP( OApplicationFactory<MyTypes> ) | ||
199 | */ | ||
200 | |||
201 | template<class Product, class ProductListTail> | ||
202 | struct OApplicationFactory< Opie::Typelist<Product, ProductListTail > > | ||
203 | : ApplicationInterface { | ||
204 | QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) { | ||
205 | *iface = 0; | ||
206 | if ( uuid == IID_QUnknown ) *iface = this; | ||
207 | else if ( uuid ==IID_QtopiaApplication ) *iface = this; | ||
208 | else return QS_FALSE; | ||
209 | (*iface)->addRef(); | ||
210 | return QS_OK; | ||
211 | } | ||
212 | |||
213 | QWidget* createMainWindow ( const QString& appName, QWidget* parent, | ||
214 | const char* name, Qt::WFlags fl ) { | ||
215 | qWarning("StringList is %s", applications().join(":").latin1() ); | ||
216 | return OPrivate< Opie::Typelist<Product, ProductListTail > >::multiFactory( appName, parent, name, fl ); | ||
217 | } | ||
218 | |||
219 | QStringList applications()const { | ||
220 | QStringList _list; | ||
221 | return OPrivate< Opie::Typelist<Product, ProductListTail> >::multiString( _list ); | ||
222 | } | ||
223 | |||
224 | Q_REFCOUNT | ||
225 | }; | ||
226 | |||
227 | |||
228 | /* If the library version should be build */ | ||
229 | #ifdef OPIE_APP_INTERFACE | ||
230 | #define OPIE_EXPORT_APP( factory ) Q_EXPORT_INTERFACE() { Q_CREATE_INSTANCE( factory ) } | ||
231 | #else | ||
232 | |||
233 | #define OPIE_EXPORT_APP( Factory ) \ | ||
234 | int main( int argc, char **argv ) { \ | ||
235 | QPEApplication a(argc, argv ); \ | ||
236 | QWidget *mw = 0;\ | ||
237 | \ | ||
238 | /* method from TT */ \ | ||
239 | QString executableName = QString::fromLatin1( argv[0] ); \ | ||
240 | executableName = executableName.right(executableName.length() \ | ||
241 | - executableName.findRev('/') - 1); \ | ||
242 | \ | ||
243 | Factory f; \ | ||
244 | QStringList list = f.applications(); \ | ||
245 | if (list.contains(executableName) ) \ | ||
246 | mw = f.createMainWindow(executableName, 0, 0, 0 ); \ | ||
247 | else \ | ||
248 | mw = f.createMainWindow( list[0], 0, 0, 0 ); \ | ||
249 | \ | ||
250 | if( mw ) { \ | ||
251 | if ( mw->metaObject()->slotNames().contains("setDocument(const QString&)" ) ) \ | ||
252 | a.showMainDocumentWidget( mw ); \ | ||
253 | else \ | ||
254 | a.showMainWidget( mw ); \ | ||
255 | \ | ||
256 | int rv = a.exec(); \ | ||
257 | delete mw; \ | ||
258 | return rv; \ | ||
259 | }else \ | ||
260 | return -1; \ | ||
261 | } | ||
262 | #endif | ||