-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTQObject.cxx
More file actions
1074 lines (888 loc) · 33.7 KB
/
TQObject.cxx
File metadata and controls
1074 lines (888 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/base:$Id: 5d6810ad46b864564f576f88aa9b154789d91d48 $
// Author: Valeriy Onuchin & Fons Rademakers 15/10/2000
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/** \class TQObject
\ingroup Base
This is the ROOT implementation of the Qt object communication
mechanism (see also http://www.troll.no/qt/metaobjects.html)
Signals and slots are used for communication between objects.
When an object has changed in some way that might be interesting
for the outside world, it emits a signal to tell whoever is
listening. All slots that are connected to this signal will be
activated (called). It is even possible to connect a signal
directly to another signal (this will emit the second signal
immediately whenever the first is emitted.) There is no limitation
on the number of slots that can be connected to a signal.
The slots will be activated in the order they were connected
to the signal. This mechanism allows objects to be easily reused,
because the object that emits a signal does not need to know
to which objects the signals are connected.
Together, signals and slots make up a powerfull component
programming mechanism.
### Signals
~~~ {.cpp}
Destroyed()
~~~
Signal emitted when object is destroyed.
This signal could be connected to some garbage-collector object.
~~~ {.cpp}
ChangedBy(const char *method_name)
~~~
This signal is emitted when some important data members of
the object were changed. method_name parameter can be used
as an identifier of the modifier method.
~~~ {.cpp}
Message(const char *msg)
~~~
General purpose message signal
*/
#include "TQObject.h"
#include "TQConnection.h"
#include "THashList.h"
#include "TPRegexp.h"
#include "TROOT.h"
#include "TBuffer.h"
#include "TClass.h"
#include "TMethod.h"
#include "TBaseClass.h"
#include "TDataType.h"
#include "TInterpreter.h"
#include "TQClass.h"
#include "TError.h"
#include <iostream>
#include "RQ_OBJECT.h"
#include "TVirtualMutex.h"
#include "RConfigure.h"
#include "strlcpy.h"
void *gTQSender; // A pointer to the object that sent the last signal.
// Getting access to the sender might be practical
// when many signals are connected to a single slot.
Bool_t TQObject::fgAllSignalsBlocked = kFALSE;
////////////////////////////////////////////////////////////////////////////////
/// Removes "const" words and blanks from full (with prototype)
/// method name and resolve any typedefs in the method signature.
/// If a null or empty string is passed in, an empty string
/// is returned.
///
/// Example:
/// ~~~ {.cpp}
/// CompressName(" Draw(const char *, const char *,
/// Option_t * , Int_t , Int_t)");
/// ~~~
/// returns the string "Draw(char*,char*,char*,int,int)".
TString TQObject::CompressName(const char *method_name)
{
TString res(method_name);
if (res.IsNull())
return res;
{
static TVirtualMutex * lock = nullptr;
R__LOCKGUARD2(lock);
static TPMERegexp *constRe = nullptr, *wspaceRe = nullptr;
if (constRe == nullptr) {
constRe = new TPMERegexp("(?<=\\(|\\s|,|&|\\*)const(?=\\s|,|\\)|&|\\*)", "go");
wspaceRe = new TPMERegexp("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)", "go");
}
constRe ->Substitute(res, "");
wspaceRe->Substitute(res, "");
}
TStringToken methargs(res, "\\(|\\)", kTRUE);
methargs.NextToken();
res = methargs;
res += "(";
methargs.NextToken();
TStringToken arg(methargs, ",");
while (arg.NextToken())
{
Int_t pri = arg.Length() - 1;
Char_t prc = 0;
if (arg[pri] == '*' || arg[pri] == '&') {
prc = arg[pri];
arg.Remove(pri);
}
TDataType *dt = gROOT->GetType(arg.Data());
if (dt) {
res += dt->GetFullTypeName();
} else {
res += arg;
}
if (prc) res += prc;
if (!arg.AtEnd()) res += ",";
}
res += ")";
return res;
}
namespace {
////////////////////////////////////////////////////////////////////////////////
/// Almost the same as TClass::GetMethodWithPrototype().
TMethod *GetMethodWithPrototype(TClass *cl, const char *method,
const char *proto, Int_t &nargs)
{
nargs = 0;
if (!gInterpreter || cl == nullptr) return nullptr;
TMethod *m = cl->GetMethodWithPrototype(method,proto);
if (m) nargs = m->GetNargs();
return m;
}
////////////////////////////////////////////////////////////////////////////////
/// Almost the same as TClass::GetMethod().
static TMethod *GetMethod(TClass *cl, const char *method, const char *params)
{
if (!gInterpreter || cl == nullptr) return nullptr;
return cl->GetMethod(method,params);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Checking of consistency of sender/receiver methods/arguments.
/// Returns -1 on error, otherwise number or arguments of signal function.
/// Static method.
Int_t TQObject::CheckConnectArgs(TQObject *sender,
TClass *sender_class, const char *signal,
TClass *receiver_class, const char *slot)
{
auto len = strlen(signal)+1;
char *signal_method = new char[len];
if (signal_method) strlcpy(signal_method, signal, len);
char *signal_proto;
char *tmp;
if ((signal_proto = strchr(signal_method,'('))) {
// substitute first '(' symbol with '\0'
*signal_proto++ = '\0';
// substitute last ')' symbol with '\0'
if ((tmp = strrchr(signal_proto,')'))) *tmp = '\0';
}
if (!signal_proto) signal_proto = (char*)""; // avoid zero strings
// if delegation object TQObjSender is used get the real sender class
if (sender && sender_class == TQObjSender::Class()) {
sender_class = TClass::GetClass(sender->GetSenderClassName());
if (!sender_class) {
::Error("TQObject::CheckConnectArgs", "for signal/slot consistency\n"
"checking need to specify class name as argument to "
"RQ_OBJECT macro");
delete [] signal_method;
return -1;
}
}
Int_t nargs;
TMethod *signalMethod = GetMethodWithPrototype(sender_class,
signal_method,
signal_proto,
nargs);
if (!signalMethod) {
::Error("TQObject::CheckConnectArgs", "signal %s::%s(%s) does not exist",
sender_class->GetName(), signal_method, signal_proto);
delete [] signal_method;
return -1;
}
Int_t nsigargs = nargs;
#if defined(CHECK_COMMENT_STRING)
const char *comment = 0;
if (signalMethod != (TMethod *) -1) // -1 in case of interpreted class
comment = signalMethod->GetCommentString();
if (!comment || !comment[0] || strstr(comment,"*SIGNAL")){
::Error("TQObject::CheckConnectArgs",
"signal %s::%s(%s), to declare signal use comment //*SIGNAL*",
sender_class->GetName(), signal_method, signal_proto);
delete [] signal_method;
return -1;
}
#endif
// cleaning
delete [] signal_method;
auto len2 = strlen(slot)+1;
char *slot_method = new char[len2];
if (slot_method) strlcpy(slot_method, slot, len2);
char *slot_proto;
char *slot_params = nullptr;
if ((slot_proto = strchr(slot_method,'('))) {
// substitute first '(' symbol with '\0'
*slot_proto++ = '\0';
// substitute last ')' symbol with '\0'
if ((tmp = strrchr(slot_proto,')'))) *tmp = '\0';
}
if (!slot_proto) slot_proto = (char*)""; // avoid zero strings
if ((slot_params = strchr(slot_proto,'='))) *slot_params = ' ';
TFunction *slotMethod = nullptr;
if (!receiver_class) {
// case of slot_method is compiled/intrepreted function
slotMethod = gROOT->GetGlobalFunction(slot_method,nullptr,kFALSE);
} else {
slotMethod = !slot_params ?
GetMethodWithPrototype(receiver_class,
slot_method,
slot_proto,
nargs) :
GetMethod(receiver_class,
slot_method, slot_params);
}
if (!slotMethod) {
if (!slot_params) {
::Error("TQObject::CheckConnectArgs", "slot %s(%s) does not exist",
receiver_class ? Form("%s::%s", receiver_class->GetName(),
slot_method) : slot_method, slot_proto);
} else {
::Error("TQObject::CheckConnectArgs", "slot %s(%s) does not exist",
receiver_class ? Form("%s::%s", receiver_class->GetName(),
slot_method) : slot_method, slot_params);
}
delete [] slot_method;
return -1;
}
#if defined(CHECK_ARGS_NUMBER)
if (slotMethod != (TMethod *) -1 && slotMethod->GetNargsOpt() >= 0 &&
nsigargs < (slotMethod->GetNargs() - slotMethod->GetNargsOpt())) {
::Error("TQObject::CheckConnectArgs",
"inconsistency in numbers of arguments");
delete [] slot_method;
return -1;
}
#endif
// cleaning
delete [] slot_method;
return nsigargs;
}
/** \class TQConnectionList
TQConnectionList is the named list of connections,
see also TQConnection class.
*/
class TQConnectionList : public TList {
private:
Int_t fSignalArgs; // number of arguments in signal function
public:
TQConnectionList(const char *name, Int_t nsigargs) : TList()
{ fName = name; fSignalArgs = nsigargs; }
~TQConnectionList() override;
Bool_t Disconnect(void *receiver = nullptr, const char *slot_name = nullptr);
Int_t GetNargs() const { return fSignalArgs; }
void ls(Option_t *option = "") const override;
};
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TQConnectionList::~TQConnectionList()
{
TIter next(this);
TQConnection *connection;
while ((connection = (TQConnection*)next())) {
// remove this from feed back reference list
connection->Remove(this);
if (connection->IsEmpty()) delete connection;
}
Clear("nodelete");
}
////////////////////////////////////////////////////////////////////////////////
/// Remove connection from the list. For more info see
/// TQObject::Disconnect()
Bool_t TQConnectionList::Disconnect(void *receiver, const char *slot_name)
{
TQConnection *connection = nullptr;
Bool_t return_value = kFALSE;
TObjLink *lnk = FirstLink();
TObjLink *savlnk; // savlnk is used when link is deleted
while (lnk) {
connection = (TQConnection*)lnk->GetObject();
const char *name = connection->GetName();
void *obj = connection->GetReceiver();
if (!slot_name || !slot_name[0]
|| !strcmp(name,slot_name)) {
if (!receiver || (receiver == obj)) {
return_value = kTRUE;
savlnk = lnk->Next(); // keep next link ..
Remove(lnk);
lnk = savlnk; // current link == saved ...
connection->Remove(this); // remove back reference
if (connection->IsEmpty()) SafeDelete(connection);
continue; // .. continue from saved link
}
}
lnk = lnk->Next();
}
return return_value;
}
////////////////////////////////////////////////////////////////////////////////
/// List signal name and list all connections in this signal list.
void TQConnectionList::ls(Option_t *option) const
{
std::cout << "TQConnectionList:" << "\t" << GetName() << std::endl;
((TQConnectionList*)this)->R__FOR_EACH(TQConnection,Print)(option);
}
////////////////////////////////////////////////////////////////////////////////
/// TQObject Constructor.
/// Comment:
/// - In order to minimize memory allocation fListOfSignals and
/// fListOfConnections are allocated only if it is neccesary
/// - When fListOfSignals/fListOfConnections are empty they will
/// be deleted
TQObject::TQObject()
{
fListOfSignals = nullptr;
fListOfConnections = nullptr;
fSignalsBlocked = kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// TQObject Destructor.
/// - delete all connections and signal list
TQObject::~TQObject()
{
if (!gROOT) return;
Destroyed(); // emit "Destroyed()" signal
if (fListOfSignals) {
fListOfSignals->Delete();
SafeDelete(fListOfSignals); // delete list of signals
}
// loop over all connections and remove references to this object
if (fListOfConnections) {
TIter next_connection(fListOfConnections);
TQConnection *connection;
while ((connection = (TQConnection*)next_connection())) {
TIter next_list(connection);
TQConnectionList *list;
while ((list = (TQConnectionList*)next_list())) {
list->Remove(connection);
if (list->IsEmpty()) SafeDelete(list);
}
}
SafeDelete(fListOfConnections);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Returns pointer to list of signals of this class.
TList *TQObject::GetListOfClassSignals() const
{
TQClass *qcl = nullptr;
qcl = dynamic_cast<TQClass*>(IsA());
return qcl ? qcl->fListOfSignals : nullptr; //!!
}
////////////////////////////////////////////////////////////////////////////////
/// Collect class signal lists from class cls and all its
/// base-classes.
///
/// The recursive traversal is not performed for classes not
/// deriving from TQClass.
void TQObject::CollectClassSignalLists(TList& list, TClass* cls)
{
TQClass *qcl = dynamic_cast<TQClass*>(cls);
if (qcl)
{
if (qcl->fListOfSignals)
list.Add(qcl->fListOfSignals);
// Descend into base-classes.
TIter next_base_class(cls->GetListOfBases());
TBaseClass *base;
while ((base = (TBaseClass*) next_base_class()))
{
CollectClassSignalLists(list, base->GetClassPointer());
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// 1. If slot_name = 0 => makes signal defined by the signal_name
/// to be the first in the fListOfSignals, this decreases
/// the time for lookup.
/// 2. If slot_name != 0 => makes slot defined by the slot_name
/// to be executed first when signal_name is emitted.
/// Signal name is not compressed.
void TQObject::HighPriority(const char *signal_name, const char *slot_name)
{
if (!fListOfSignals) return;
TQConnectionList *clist = (TQConnectionList*)
fListOfSignals->FindObject(signal_name);
if (!clist) return; // not found
if (!slot_name) { // update list of signal lists
fListOfSignals->Remove(clist); // remove and add first
fListOfSignals->AddFirst(clist);
return;
} else { // slot_name != 0 , update signal list
TQConnection *con = (TQConnection*) clist->FindObject(slot_name);
if (!con) return; // not found
clist->Remove(con); // remove and add as first
clist->AddFirst(con);
}
}
////////////////////////////////////////////////////////////////////////////////
/// 1. If slot_name = 0 => makes signal defined by the signal_name
/// to be the last in the fListOfSignals, this increase the time
/// for lookup.
/// 2. If slot_name != 0 => makes slot defined by the slot_name
/// to be executed last when signal_name is emitted.
/// Signal name is not compressed.
void TQObject::LowPriority(const char *signal_name, const char *slot_name)
{
if (!fListOfSignals) return;
TQConnectionList *clist = (TQConnectionList*)
fListOfSignals->FindObject(signal_name);
if (!clist) return;
if (!slot_name) {
fListOfSignals->Remove(clist); // remove and add first
fListOfSignals->AddLast(clist);
return;
} else { // slot_name != 0 , update signal list
TQConnection *con = (TQConnection*) clist->FindObject(slot_name);
if (!con) return;
clist->Remove(con); // remove and add as last
clist->AddLast(con);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Return true if there is any object connected to this signal.
/// Only checks for object signals.
Bool_t TQObject::HasConnection(const char *signal_name) const
{
if (!fListOfSignals)
return kFALSE;
TString signal = CompressName(signal_name);
return (fListOfSignals->FindObject(signal) != nullptr);
}
////////////////////////////////////////////////////////////////////////////////
/// Return number of signals for this object.
/// Only checks for object signals.
Int_t TQObject::NumberOfSignals() const
{
if (fListOfSignals)
return fListOfSignals->GetSize();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Return number of connections for this object.
Int_t TQObject::NumberOfConnections() const
{
if (fListOfConnections)
return fListOfConnections->GetSize();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Create connection between sender and receiver.
/// Receiver class needs to have a dictionary.
Bool_t TQObject::ConnectToClass(TQObject *sender,
const char *signal,
TClass *cl,
void *receiver,
const char *slot)
{
// sender should be TQObject
if (!sender->IsA()->InheritsFrom(TQObject::Class()))
return kFALSE;
// remove "const" and strip blanks
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
// check consistency of signal/slot methods/args
Int_t nsigargs;
if ((nsigargs = CheckConnectArgs(sender, sender->IsA(), signal_name, cl, slot_name)) == -1)
return kFALSE;
if (!sender->fListOfSignals)
sender->fListOfSignals = new THashList();
TQConnectionList *clist = (TQConnectionList*)
sender->fListOfSignals->FindObject(signal_name);
if (!clist) {
clist = new TQConnectionList(signal_name, nsigargs);
sender->fListOfSignals->Add(clist);
}
TIter next(clist);
TQConnection *connection = nullptr;
while ((connection = (TQConnection*)next())) {
if (!strcmp(slot_name,connection->GetName()) &&
(receiver == connection->GetReceiver())) break;
}
if (!connection)
connection = new TQConnection(cl, receiver, slot_name);
// check to prevent multiple entries
if (!clist->FindObject(connection)) {
clist->Add(connection);
if (!connection->FindObject(clist)) connection->Add(clist);
sender->Connected(signal_name);
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// This method allows to make connection from any object
/// of the same class to the receiver object.
/// Receiver class needs to have a dictionary.
Bool_t TQObject::ConnectToClass(const char *class_name,
const char *signal,
TClass *cl,
void *receiver,
const char *slot)
{
TClass *sender = TClass::GetClass(class_name);
// sender class should be TQObject (i.e. TQClass)
if (!sender || !sender->IsA()->InheritsFrom(TQObject::Class()))
return kFALSE;
TList *slist = ((TQClass*)sender)->fListOfSignals;
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
// check consistency of signal/slot methods/args
Int_t nsigargs;
if ((nsigargs = CheckConnectArgs(nullptr, sender, signal_name, cl, slot_name)) == -1)
return kFALSE;
if (!slist)
((TQClass*)sender)->fListOfSignals = slist = new THashList();
TQConnectionList *clist = (TQConnectionList*) slist->FindObject(signal_name);
if (!clist) {
clist = new TQConnectionList(signal_name, nsigargs);
slist->Add(clist);
}
TQConnection *connection = nullptr;
TIter next(clist);
while ((connection = (TQConnection*)next())) {
if (!strcmp(slot_name,connection->GetName()) &&
(receiver == connection->GetReceiver())) break;
}
if (!connection)
connection = new TQConnection(cl, receiver, slot_name);
// check to prevent multiple entries
if (!clist->FindObject(connection)) {
clist->Add(connection);
if (!connection->FindObject(clist)) connection->Add(clist);
((TQClass*)sender)->Connected(signal_name);
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create connection between sender and receiver.
/// Signal and slot string must have a form:
/// "Draw(char*, Option_t* ,Int_t)"
/// All blanks and "const" words will be removed,
///
/// cl != 0 - class name, it can be class with or
/// without dictionary, e.g interpreted class.
/// Example:
/// ~~~ {.cpp}
/// TGButton *myButton;
/// TH2F *myHist;
///
/// TQObject::Connect(myButton,"Clicked()",
/// "TH2F", myHist,"Draw(Option_t*)");
/// ~~~
/// cl == 0 - corresponds to function (interpereted or global)
/// the name of the function is defined by the slot string,
/// parameter receiver should be 0.
/// Example:
/// ~~~ {.cpp}
/// TGButton *myButton;
/// TH2F *myHist;
///
/// TQObject::Connect(myButton,"Clicked()",
/// 0, 0,"hsimple()");
/// ~~~
/// Warning:
/// If receiver is class not derived from TQObject and going to be
/// deleted, disconnect all connections to this receiver.
/// In case of class derived from TQObject it is done automatically.
Bool_t TQObject::Connect(TQObject *sender,
const char *signal,
const char *cl,
void *receiver,
const char *slot)
{
if (cl) {
TClass *rcv_cl = TClass::GetClass(cl);
if (rcv_cl) return ConnectToClass(sender, signal, rcv_cl, receiver, slot);
}
// the following is the case of receiver class without dictionary
// e.g. interpreted class or function.
// sender should be TQObject
if (!sender->IsA()->InheritsFrom(TQObject::Class()))
return kFALSE;
// remove "const" and strip blanks
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
// check consistency of signal/slot methods/args
Int_t nsigargs;
if ((nsigargs = CheckConnectArgs(sender, sender->IsA(), signal_name, nullptr, slot_name)) == -1)
return kFALSE;
if (!sender->fListOfSignals) sender->fListOfSignals = new THashList();
TQConnectionList *clist = (TQConnectionList*)
sender->fListOfSignals->FindObject(signal_name);
if (!clist) {
clist = new TQConnectionList(signal_name, nsigargs);
sender->fListOfSignals->Add(clist);
}
TQConnection *connection = nullptr;
TIter next(clist);
while ((connection = (TQConnection*)next())) {
if (!strcmp(slot_name,connection->GetName()) &&
(receiver == connection->GetReceiver())) break;
}
if (!connection)
connection = new TQConnection(cl, receiver, slot_name);
// check to prevent multiple entries
if (!clist->FindObject(connection)) {
clist->Add(connection);
if (!connection->FindObject(clist)) connection->Add(clist);
sender->Connected(signal_name);
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// This method allows to make a connection from any object
/// of the same class to a single slot.
/// Signal and slot string must have a form:
/// "Draw(char*, Option_t* ,Int_t)"
/// All blanks and "const" words will be removed,
///
/// cl != 0 - class name, it can be class with or
/// without dictionary, e.g interpreted class.
/// Example:
/// ~~~ {.cpp}
/// TGButton *myButton;
/// TH2F *myHist;
///
/// TQObject::Connect("TGButton", "Clicked()",
/// "TH2F", myHist, "Draw(Option_t*)");
/// ~~~
/// cl == 0 - corresponds to function (interpereted or global)
/// the name of the function is defined by the slot string,
/// parameter receiver should be 0.
/// Example:
/// ~~~ {.cpp}
/// TGButton *myButton;
/// TH2F *myHist;
///
/// TQObject::Connect("TGButton", "Clicked()",
/// 0, 0, "hsimple()");
/// ~~~
/// Warning:
///
/// If receiver class not derived from TQObject and going to be
/// deleted, disconnect all connections to this receiver.
/// In case of class derived from TQObject it is done automatically.
Bool_t TQObject::Connect(const char *class_name,
const char *signal,
const char *cl,
void *receiver,
const char *slot)
{
if (cl) {
TClass *rcv_cl = TClass::GetClass(cl);
if (rcv_cl) return ConnectToClass(class_name, signal, rcv_cl, receiver,
slot);
}
// the following is case of receiver class without dictionary
// e.g. interpreted class or function.
TClass *sender = TClass::GetClass(class_name);
// sender class should be TQObject (i.e. TQClass)
if (!sender || !sender->IsA()->InheritsFrom(TQObject::Class()))
return kFALSE;
TList *slist = ((TQClass*)sender)->fListOfSignals;
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
// check consistency of signal/slot methods/args
Int_t nsigargs;
if ((nsigargs = CheckConnectArgs(nullptr, sender, signal_name, nullptr, slot_name)) == -1)
return kFALSE;
if (!slist) {
slist = ((TQClass*)sender)->fListOfSignals = new THashList();
}
TQConnectionList *clist = (TQConnectionList*)
slist->FindObject(signal_name);
if (!clist) {
clist = new TQConnectionList(signal_name, nsigargs);
slist->Add(clist);
}
TQConnection *connection = nullptr;
TIter next(clist);
while ((connection = (TQConnection*)next())) {
if (!strcmp(slot_name,connection->GetName()) &&
(receiver == connection->GetReceiver())) break;
}
if (!connection)
connection = new TQConnection(cl, receiver, slot_name);
// check to prevent multiple entries
if (!clist->FindObject(connection)) {
clist->Add(connection);
if (!connection->FindObject(clist)) connection->Add(clist);
((TQClass*)sender)->Connected(signal_name);
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Non-static method is used to connect from the signal
/// of this object to the receiver slot.
///
/// Warning! No check on consistency of sender/receiver
/// classes/methods.
///
/// This method makes possible to have connection/signals from
/// interpreted class. See also RQ_OBJECT.h.
Bool_t TQObject::Connect(const char *signal,
const char *receiver_class,
void *receiver,
const char *slot)
{
// remove "const" and strip blanks
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
// check consistency of signal/slot methods/args
TClass *cl = nullptr;
if (receiver_class)
cl = TClass::GetClass(receiver_class);
Int_t nsigargs;
if ((nsigargs = CheckConnectArgs(this, IsA(), signal_name, cl, slot_name)) == -1)
return kFALSE;
if (!fListOfSignals) fListOfSignals = new THashList();
TQConnectionList *clist = (TQConnectionList*)
fListOfSignals->FindObject(signal_name);
if (!clist) {
clist = new TQConnectionList(signal_name, nsigargs);
fListOfSignals->Add(clist);
}
TIter next(clist);
TQConnection *connection = nullptr;
while ((connection = (TQConnection*)next())) {
if (!strcmp(slot_name,connection->GetName()) &&
(receiver == connection->GetReceiver())) break;
}
if (!connection)
connection = new TQConnection(receiver_class, receiver, slot_name);
// check to prevent multiple entries
if (!clist->FindObject(connection)) {
clist->Add(connection);
if (!connection->FindObject(clist)) connection->Add(clist);
Connected(signal_name);
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Disconnects signal in object sender from slot_method in
/// object receiver. For objects derived from TQObject signal-slot
/// connection is removed when either of the objects involved
/// are destroyed.
///
/// Disconnect() is typically used in three ways, as the following
/// examples shows:
///
/// - Disconnect everything connected to an object's signals:
/// ~~~ {.cpp}
/// Disconnect(myObject);
/// ~~~
/// - Disconnect everything connected to a signal:
/// ~~~ {.cpp}
/// Disconnect(myObject, "mySignal()");
/// ~~~
/// - Disconnect a specific receiver:
/// ~~~ {.cpp}
/// Disconnect(myObject, 0, myReceiver, 0);
/// ~~~
///
/// 0 may be used as a wildcard in three of the four arguments,
/// meaning "any signal", "any receiving object" or
/// "any slot in the receiving object", respectively.
///
/// The sender has no default and may never be 0
/// (you cannot disconnect signals from more than one object).
///
/// If signal is 0, it disconnects receiver and slot_method
/// from any signal. If not, only the specified signal is
/// disconnected.
///
/// If receiver is 0, it disconnects anything connected to signal.
/// If not, slots in objects other than receiver are not
/// disconnected
///
/// If slot_method is 0, it disconnects anything that is connected
/// to receiver. If not, only slots named slot_method will be
/// disconnected, and all other slots are left alone.
/// The slot_method must be 0 if receiver is left out, so you
/// cannot disconnect a specifically-named slot on all objects.
Bool_t TQObject::Disconnect(TQObject *sender,
const char *signal,
void *receiver,
const char *slot)
{
Bool_t return_value = kFALSE;
Bool_t next_return = kFALSE;
if (!sender->GetListOfSignals()) return kFALSE;
TString signal_name = CompressName(signal);
TString slot_name = CompressName(slot);
TQConnectionList *slist = nullptr;
TIter next_signal(sender->GetListOfSignals());
while ((slist = (TQConnectionList*)next_signal())) {
if (!signal || signal_name.IsNull()) { // disconnect all signals
next_return = slist->Disconnect(receiver,slot_name);
return_value = return_value || next_return;
if (slist->IsEmpty()) {
sender->GetListOfSignals()->Remove(slist);
SafeDelete(slist); // delete empty list
}
} else if (signal && !strcmp(signal_name,slist->GetName())) {
next_return = slist->Disconnect(receiver,slot_name);
return_value = return_value || next_return;
if (slist->IsEmpty()) {
sender->GetListOfSignals()->Remove(slist);
SafeDelete(slist); // delete empty list
break;
}
}
}
if (sender->GetListOfSignals() && sender->GetListOfSignals()->IsEmpty()) {
SafeDelete(sender->fListOfSignals);
}
return return_value;
}
////////////////////////////////////////////////////////////////////////////////