00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef KRADIO_INTERFACES_H
00019 #define KRADIO_INTERFACES_H
00020
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024
00025 #include <qptrlist.h>
00026 #include <qmap.h>
00027 #include <kdebug.h>
00028 #include <typeinfo>
00029
00030
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00303
00304
00305
00306
00307
00308
00309 class Interface
00310 {
00311 public:
00312 Interface () {}
00313 virtual ~Interface() {}
00314
00315 virtual bool connectI (Interface *) { return false; }
00316 virtual bool disconnectI(Interface *) { return false; }
00317
00318
00319
00320 bool connectI (Interface &i) { return connectI (&i); }
00321 bool disconnectI(Interface &i) { return disconnectI (&i); }
00322 };
00323
00325
00326 template <class thisIF, class cmplIF>
00327 class InterfaceBase : virtual public Interface
00328 {
00329 private:
00330 typedef InterfaceBase<thisIF, cmplIF> thisClass;
00331 typedef InterfaceBase<cmplIF, thisIF> cmplClass;
00332
00333
00334
00335 public:
00336
00337 typedef thisIF thisInterface;
00338 typedef cmplIF cmplInterface;
00339
00340 typedef QPtrList<cmplIF> IFList;
00341 typedef QPtrListIterator<cmplIF> IFIterator;
00342
00343 typedef thisClass BaseClass;
00344
00345 public :
00346 InterfaceBase (int maxIConnections = -1);
00347 virtual ~InterfaceBase ();
00348
00349
00350 virtual bool connectI(Interface *i);
00351 virtual bool disconnectI(Interface *i);
00352
00353 protected:
00354 virtual void disconnectAllI();
00355
00356
00357 public:
00358
00359
00360
00361
00362
00363
00364
00365 virtual void noticeConnectI (cmplInterface *, bool ) {}
00366 virtual void noticeConnectedI (cmplInterface *, bool ) {}
00367 virtual void noticeDisconnectI (cmplInterface *, bool );
00368 virtual void noticeDisconnectedI(cmplInterface *, bool ) {}
00369
00370 virtual bool isIConnectionFree() const;
00371 virtual unsigned connectedI() const { return iConnections.count(); }
00372
00373 thisIF *initThisInterfacePointer();
00374 thisIF *getThisInterfacePointer() const { return me; }
00375 bool isThisInterfacePointerValid() const { return me_valid; }
00376 bool hasConnectionTo(cmplInterface *other) const { return iConnections.containsRef(other); }
00377 void appendConnectionTo(cmplInterface *other) { iConnections.append(other); }
00378 void removeConnectionTo(cmplInterface *other) { iConnections.removeRef(other); }
00379
00380 protected :
00381
00382 IFList iConnections;
00383 int maxIConnections;
00384
00385
00386 protected:
00387 bool addListener (const cmplInterface *i, QPtrList<cmplInterface> &list);
00388 void removeListener(const cmplInterface *i, QPtrList<cmplInterface> &list);
00389 void removeListener(const cmplInterface *i);
00390
00391 QMap<const cmplInterface *, QPtrList<QPtrList<cmplInterface> > > m_FineListeners;
00392
00393 private:
00394 thisInterface *me;
00395 bool me_valid;
00396 };
00397
00398
00399
00400
00401 #define INTERFACE(IF, cmplIF) \
00402 class IF; \
00403 class cmplIF; \
00404 class IF : public InterfaceBase<IF, cmplIF> \
00405
00406 #define IF_CON_DESTRUCTOR(IF, n) \
00407 IF() : BaseClass((n)) {} \
00408 virtual ~IF() { }
00409
00410
00411
00412
00413
00414 #ifdef DEBUG
00415 #include <iostream>
00416 using namespace std;
00417 #define IF_QUERY_DEBUG \
00418 if (iConnections.count() > 1) { \
00419 kdDebug() << "class " << typeid(this).name() << ": using IF_QUERY with #connections > 1\n"; \
00420 }
00421 #else
00422 #define IF_QUERY_DEBUG
00423 #endif
00424
00425
00426
00427
00428
00429 #define SENDERS protected
00430 #define RECEIVERS public
00431
00432 #define IF_SENDER(decl) \
00433 virtual int decl const;
00434
00435 #define IF_SEND_MESSAGE(call) \
00436 int ____n = 0; \
00437 for (IFIterator i(iConnections); i.current(); ++i) { \
00438 if (i.current()->call ) ++____n; \
00439 } \
00440 return ____n;
00441
00442 #define IF_IMPL_SENDER(decl, call) \
00443 int decl const \
00444 { \
00445 IF_SEND_MESSAGE(call) \
00446 }
00447
00448 #define IF_RECEIVER(decl) \
00449 virtual bool decl = 0;
00450
00451 #define IF_RECEIVER_EMPTY(decl) \
00452 virtual bool decl { return false; }
00453
00454
00455
00456 #define ANSWERS public
00457 #define QUERIES protected
00458
00459 #define IF_QUERY(decl) \
00460 virtual decl const;
00461
00462 #define IF_SEND_QUERY(call, default) \
00463 cmplInterface *o = IFIterator(iConnections).current(); \
00464 if (o) { \
00465 IF_QUERY_DEBUG \
00466 return o->call; \
00467 } else { \
00468 return default; \
00469 } \
00470
00471 #define IF_IMPL_QUERY(decl, call, default) \
00472 decl const { \
00473 IF_SEND_QUERY(call, default) \
00474 }
00475
00476 #define IF_ANSWER(decl) \
00477 virtual decl = 0;
00478
00479
00480
00481
00483
00485
00486
00487 #define IF_SENDER_FINE(name, param) \
00488 protected: \
00489 int name param const; \
00490 public: \
00491 bool register4_##name (cmplInterface *); \
00492 void unregister4_##name(cmplInterface *); \
00493 private: \
00494 QPtrList<cmplInterface> m_Listeners_##name;\
00495
00496
00497 #define IF_SEND_MESSAGE_FINE(name, params, call) \
00498 int ____n = 0; \
00499 for (QPtrListIterator<cmplInterface> ____it(m_Listeners_##name); ____it.current(); ++____it) { \
00500 if (____it.current()->call ) ++____n; \
00501 } \
00502 return ____n;
00503
00504 #define IF_IMPL_SENDER_FINE(class, name, param, call) \
00505 int class::name param const { \
00506 IF_SEND_MESSAGE_FINE(name, param, call) \
00507 } \
00508 \
00509 bool class::register4_##name(cmplInterface *i) { \
00510 return addListener(i, m_Listeners_##name); \
00511 } \
00512 void class::unregister4_##name(cmplInterface *i) { \
00513 m_Listeners_##name.remove(i); \
00514 }
00515
00516
00518
00519
00520 template <class thisIF, class cmplIF>
00521 InterfaceBase<thisIF, cmplIF>::InterfaceBase(int _maxIConnections)
00522 : maxIConnections(_maxIConnections),
00523 me(NULL),
00524 me_valid(false)
00525 {
00526 }
00527
00528
00529 template <class thisIF, class cmplIF>
00530 InterfaceBase<thisIF, cmplIF>::~InterfaceBase()
00531 {
00532 me_valid = false;
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 if (iConnections.count() > 0) {
00543 thisClass::disconnectAllI();
00544 }
00545 }
00546
00547
00548 template <class thisIF, class cmplIF>
00549 bool InterfaceBase<thisIF, cmplIF>::isIConnectionFree () const
00550 {
00551 int m = maxIConnections;
00552 return (m < 0) || (iConnections.count() < (unsigned) m);
00553 }
00554
00555 template <class thisIF, class cmplIF>
00556 thisIF *InterfaceBase<thisIF, cmplIF>::initThisInterfacePointer()
00557 {
00558 if (!me) me = dynamic_cast<thisIF*>(this);
00559 me_valid = me != NULL;
00560 return me;
00561 }
00562
00563 template <class thisIF, class cmplIF>
00564 bool InterfaceBase<thisIF, cmplIF>::connectI (Interface *__i)
00565 {
00566
00567
00568 initThisInterfacePointer();
00569
00570
00571 cmplClass *_i = dynamic_cast<cmplClass*>(__i);
00572 if (!_i) {
00573 return false;
00574 }
00575
00576 cmplIF *i = _i->initThisInterfacePointer();
00577
00578 if (i && me) {
00579 bool i_connected = iConnections.containsRef(i);
00580 bool me_connected = i->hasConnectionTo(me);
00581
00582 if (i_connected || me_connected) {
00583 return true;
00584 } else if (isIConnectionFree() && i->isIConnectionFree()) {
00585
00586 noticeConnectI(i, i != NULL);
00587 _i->noticeConnectI(me, me != NULL);
00588
00589 if (!i_connected)
00590 appendConnectionTo(i);
00591 if (!me_connected)
00592 _i->appendConnectionTo(me);
00593
00594 noticeConnectedI(i, i != NULL);
00595 _i->noticeConnectedI(me, me != NULL);
00596
00597 return true;
00598 } else {
00599 return false;
00600 }
00601 }
00602 return false;
00603 }
00604
00605
00606
00607 template <class thisIF, class cmplIF>
00608 bool InterfaceBase<thisIF, cmplIF>::disconnectI (Interface *__i)
00609 {
00610 cmplClass *_i = dynamic_cast<cmplClass*>(__i);
00611
00612
00613 cmplIF *i = _i ? _i->getThisInterfacePointer() : NULL;
00614
00615
00616
00617
00618
00619 if (i && _i) {
00620 if (me_valid)
00621 noticeDisconnectI(i, _i->isThisInterfacePointerValid());
00622 }
00623
00624 if (me && _i) {
00625 if (_i->isThisInterfacePointerValid())
00626 _i->noticeDisconnectI(me, me_valid);
00627 }
00628
00629 if (i && hasConnectionTo(i)) {
00630 removeListener(i);
00631 removeConnectionTo(i);
00632 }
00633
00634 if (me && i && i->hasConnectionTo(me))
00635 i->removeConnectionTo(me);
00636
00637 if (me_valid && i && _i)
00638 noticeDisconnectedI(i, _i->isThisInterfacePointerValid());
00639 if (_i && _i->isThisInterfacePointerValid() && me)
00640 _i->noticeDisconnectedI(me, me_valid);
00641
00642 return true;
00643 }
00644
00645
00646 template <class thisIF, class cmplIF>
00647 void InterfaceBase<thisIF, cmplIF>::noticeDisconnectI(cmplInterface *i, bool )
00648 {
00649 removeListener(i);
00650 }
00651
00652
00653 template <class thisIF, class cmplIF>
00654 void InterfaceBase<thisIF, cmplIF>::disconnectAllI()
00655 {
00656 IFList tmp = iConnections;
00657 for (IFIterator it(tmp); it.current(); ++it) {
00658
00659
00660
00661
00662
00663 if (me_valid)
00664 disconnectI(it.current());
00665 else
00666 thisClass::disconnectI(it.current());
00667 }
00668 }
00669
00670
00671
00672
00673 template <class thisIF, class cmplIF>
00674 bool InterfaceBase<thisIF, cmplIF>::addListener(const cmplInterface *i, QPtrList<cmplInterface> &list)
00675 {
00676 if (iConnections.containsRef(i) && !list.contains(i)) {
00677 list.append(i);
00678 m_FineListeners[i].append(&list);
00679 return true;
00680 } else {
00681 return false;
00682 }
00683 }
00684
00685
00686 template <class thisIF, class cmplIF>
00687 void InterfaceBase<thisIF, cmplIF>::removeListener(const cmplInterface *i, QPtrList<cmplInterface> &list)
00688 {
00689 list.remove(i);
00690 if (m_FineListeners.contains(i))
00691 m_FineListeners[i].remove(&list);
00692 }
00693
00694
00695 template <class thisIF, class cmplIF>
00696 void InterfaceBase<thisIF, cmplIF>::removeListener(const cmplInterface *i)
00697 {
00698 if (m_FineListeners.contains(i)) {
00699 QPtrList<QPtrList<cmplInterface> > &list = m_FineListeners[i];
00700 QPtrListIterator<QPtrList<cmplInterface> > it(list);
00701 for (; it.current(); ++it) {
00702 (*it)->remove(i);
00703 }
00704 }
00705 m_FineListeners.remove(i);
00706 }
00707
00708
00709
00710
00711
00712
00713
00714 #endif