Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TQObject.h
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Valeriy Onuchin & Fons Rademakers 15/10/2000
3
4/*************************************************************************
5 * Copyright (C) 1995-2022, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TQObject
13#define ROOT_TQObject
14
15//////////////////////////////////////////////////////////////////////////
16// //
17// This is the ROOT implementation of the Qt object communication //
18// mechanism (see also https://doc.qt.io/qt-5/metaobjects.html) //
19// //
20// Signals and slots are used for communication between objects. //
21// When an object has changed in some way that might be interesting //
22// for the outside world, it emits a signal to tell whoever is //
23// listening. All slots that are connected to this signal will be //
24// activated (called). It is even possible to connect a signal //
25// directly to another signal (this will emit the second signal //
26// immediately whenever the first is emitted.) There is no limitation //
27// on the number of slots that can be connected to a signal. //
28// The slots will be activated in the order they were connected //
29// to the signal. This mechanism allows objects to be easily reused, //
30// because the object that emits a signal does not need to know //
31// to what the signals are connected to. //
32// Together, signals and slots make up a powerful component //
33// programming mechanism. //
34// //
35// This implementation is provided by //
36// Valeriy Onuchin (onuchin@sirius.ihep.su). //
37// //
38//////////////////////////////////////////////////////////////////////////
39
40#include "TList.h"
41#include "TString.h"
42#include "TVirtualQConnection.h"
43
44class TClass;
45
46R__EXTERN void *gTQSender; // the latest sender object
47
48class TQObject {
49
50protected:
51 TList *fListOfSignals; //! list of signals from this object
52 TList *fListOfConnections; //! list of connections to this object
53 Bool_t fSignalsBlocked; //! flag used for suppression of signals
54
55 static Bool_t fgAllSignalsBlocked; // flag used for suppression of all signals
56
57 virtual void *GetSender() { return this; }
58 virtual const char *GetSenderClassName() const { return ""; }
59
60
61 static Bool_t ConnectToClass(TQObject *sender,
62 const char *signal,
63 TClass *receiver_class,
64 void *receiver,
65 const char *slot);
66
67 static Bool_t ConnectToClass(const char *sender_class,
68 const char *signal,
69 TClass *receiver_class,
70 void *receiver,
71 const char *slot);
72
73 static Int_t CheckConnectArgs(TQObject *sender,
74 TClass *sender_class, const char *signal,
75 TClass *receiver_class, const char *slot);
76
77 static TString CompressName(const char *method_name);
78
79private:
80 TQObject(const TQObject &) = delete;
81 TQObject& operator=(const TQObject &) = delete;
82
83public:
84 TQObject();
85 virtual ~TQObject();
86
90
93 { Bool_t ret = fSignalsBlocked; fSignalsBlocked = b; return ret; }
94
95 void CollectClassSignalLists(TList& list, TClass* cls);
96
97 ///////////////////////////////////////////////////////////////////////////////
98 /// Emit a signal with a varying number of arguments.
99 ///
100 template <typename... T> void EmitVA(const char *signal_name, Int_t /* nargs */, const T&... params)
101 {
102 // Activate signal with variable argument list.
103 // For internal use and for var arg EmitVA() in RQ_OBJECT.h.
104
106 return;
107
108 TList classSigLists;
109 CollectClassSignalLists(classSigLists, IsA());
110
111 if (classSigLists.IsEmpty() && !fListOfSignals)
112 return;
113
114 TString signal = CompressName(signal_name);
115
116 TVirtualQConnection *connection = nullptr;
117
118 // execute class signals
119 TList *sigList;
120 TIter nextSigList(&classSigLists);
121 while ((sigList = (TList*) nextSigList()) != nullptr) {
122 TIter nextcl((TList*) sigList->FindObject(signal));
123 while ((connection = static_cast<TVirtualQConnection*>(nextcl())) != nullptr) {
125 connection->SetArgs(params...);
126 connection->SendSignal();
127 }
128 }
129 if (!fListOfSignals)
130 return;
131
132 // execute object signals
133 TIter next((TList*) fListOfSignals->FindObject(signal));
134 while (fListOfSignals && (connection = static_cast<TVirtualQConnection*>(next())) != nullptr) {
136 connection->SetArgs(params...);
137 connection->SendSignal();
138 }
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Activate signal with single parameter.
143 /// Example:
144 /// ~~~ {.cpp}
145 /// theButton->Emit("Progress(Long64_t)",processed)
146 /// ~~~
147 ///
148 /// If we call Emit with an array of the parameters, they should be converted
149 /// to Longptr_t type.
150 /// Example:
151 /// ~~~ {.cpp}
152 /// TQObject *processor; // data processor
153 /// TH1F *hist; // filled with processor results
154 ///
155 /// processor->Connect("Evaluated(Float_t,Float_t)",
156 /// "TH1F",hist,"Fill12(Axis_t,Axis_t)");
157 ///
158 /// Longptr_t args[2];
159 /// args[0] = (Longptr_t)processor->GetValue(1);
160 /// args[1] = (Longptr_t)processor->GetValue(2);
161 ///
162 /// processor->Emit("Evaluated(Float_t,Float_t)",args);
163 /// ~~~
164 template <typename T> void Emit(const char *signal, const T& arg)
165 {
166 Int_t placeholder = 0;
167 EmitVA(signal, placeholder, arg);
168 }
169
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Activate signal without args.
172 /// Example:
173 /// theButton->Emit("Clicked()");
174 void Emit(const char *signal) { EmitVA(signal, (Int_t) 0); }
175
176 Bool_t Connect(const char *signal,
177 const char *receiver_class,
178 void *receiver,
179 const char *slot);
180
181 Bool_t Disconnect(const char *signal = nullptr,
182 void *receiver = nullptr,
183 const char *slot = nullptr);
184
185 virtual void HighPriority(const char *signal_name,
186 const char *slot_name = nullptr);
187
188 virtual void LowPriority(const char *signal_name,
189 const char *slot_name = nullptr);
190
191 virtual Bool_t HasConnection(const char *signal_name) const;
192 virtual Int_t NumberOfSignals() const;
193 virtual Int_t NumberOfConnections() const;
194 virtual void Connected(const char * /*signal_name*/) { }
195 virtual void Disconnected(const char * /*signal_name*/) { }
196
197 virtual void Destroyed()
198 { Emit("Destroyed()"); } // *SIGNAL*
199 virtual void ChangedBy(const char *method)
200 { Emit("ChangedBy(char*)", method); } // *SIGNAL*
201 virtual void Message(const char *msg)
202 { Emit("Message(char*)", msg); } // *SIGNAL*
203
204 static Bool_t Connect(TQObject *sender,
205 const char *signal,
206 const char *receiver_class,
207 void *receiver,
208 const char *slot);
209
210 static Bool_t Connect(const char *sender_class,
211 const char *signal,
212 const char *receiver_class,
213 void *receiver,
214 const char *slot);
215
216 static Bool_t Disconnect(TQObject *sender,
217 const char *signal = nullptr,
218 void *receiver = nullptr,
219 const char *slot = nullptr);
220
221 static Bool_t Disconnect(const char *class_name,
222 const char *signal,
223 void *receiver = nullptr,
224 const char *slot = nullptr);
225
228
229 ClassDef(TQObject,1) //Base class for object communication mechanism
230};
231
232
233class TQObjSender : public TQObject {
234
235protected:
236 void *fSender; //delegation object
237 TString fSenderClass; //class name of delegation object
238
239 void *GetSender() override { return fSender; }
240 const char *GetSenderClassName() const override { return fSenderClass.Data(); }
241
242private:
243 TQObjSender(const TQObjSender&) = delete;
245
246public:
248 virtual ~TQObjSender() { Disconnect(); }
249
250 virtual void SetSender(void *sender) { fSender = sender; }
251 void SetSenderClassName(const char *sclass = "") { fSenderClass = sclass; }
252
253 ClassDefOverride(TQObjSender,0) //Used to "delegate" TQObject functionality
254 //to interpreted classes, see also RQ_OBJECT.h
255};
256
257
258
259// Global function which simplifies making connections in interpreted
260// ROOT session
261//
262// ConnectCINT - connects to interpreter(CINT) command
263
264extern Bool_t ConnectCINT(TQObject *sender, const char *signal,
265 const char *slot);
266
267#ifdef G__DICTIONARY
268// This include makes it possible to have a single connection
269// from all objects of the same class but is only needed in
270// the dictionary.
271#include "TQClass.h"
272#endif
273
274
275//---- ClassImpQ macro ----------------------------------------------
276//
277// This macro used to correspond to the ClassImp macro and should be used
278// for classes derived from TQObject instead of the ClassImp macro.
279// This macro makes it possible to have a single connection from
280// all objects of the same class.
281// *** It is now obsolete ***
282
283#define ClassImpQ(name) \
284 ClassImp(name)
285
286#endif
#define R__EXTERN
Definition DllImport.h:27
#define b(i)
Definition RSha256.hxx:100
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
#define ClassDef(name, id)
Definition Rtypes.h:337
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
R__EXTERN void * gTQSender
Definition TQObject.h:46
Bool_t ConnectCINT(TQObject *sender, const char *signal, const char *slot)
Global function which simplifies making connection in interpreted ROOT session.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
virtual Bool_t IsEmpty() const
A doubly linked list.
Definition TList.h:38
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:578
virtual void SetSender(void *sender)
Definition TQObject.h:250
TString fSenderClass
Definition TQObject.h:237
TQObjSender(const TQObjSender &)=delete
virtual ~TQObjSender()
Definition TQObject.h:248
TQObjSender & operator=(const TQObjSender &)=delete
void SetSenderClassName(const char *sclass="")
Definition TQObject.h:251
void * GetSender() override
Definition TQObject.h:239
const char * GetSenderClassName() const override
Definition TQObject.h:240
void * fSender
Definition TQObject.h:236
This is the ROOT implementation of the Qt object communication mechanism (see also http://www....
Definition TQObject.h:48
virtual void Disconnected(const char *)
Definition TQObject.h:195
static Int_t CheckConnectArgs(TQObject *sender, TClass *sender_class, const char *signal, TClass *receiver_class, const char *slot)
Checking of consistency of sender/receiver methods/arguments.
Definition TQObject.cxx:179
virtual Int_t NumberOfConnections() const
Return number of connections for this object.
Definition TQObject.cxx:551
TQObject()
TQObject Constructor.
Definition TQObject.cxx:393
void CollectClassSignalLists(TList &list, TClass *cls)
Collect class signal lists from class cls and all its base-classes.
Definition TQObject.cxx:451
virtual const char * GetSenderClassName() const
Definition TQObject.h:58
TQObject(const TQObject &)=delete
static Bool_t BlockAllSignals(Bool_t b)
Block or unblock all signals. Returns the previous block status.
TList * GetListOfSignals() const
Definition TQObject.h:88
Bool_t fSignalsBlocked
list of connections to this object
Definition TQObject.h:53
static TString CompressName(const char *method_name)
Removes "const" words and blanks from full (with prototype) method name and resolve any typedefs in t...
Definition TQObject.cxx:98
TList * GetListOfClassSignals() const
Returns pointer to list of signals of this class.
Definition TQObject.cxx:435
static Bool_t fgAllSignalsBlocked
flag used for suppression of signals
Definition TQObject.h:55
virtual Int_t NumberOfSignals() const
Return number of signals for this object.
Definition TQObject.cxx:541
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition TQObject.h:164
virtual void Destroyed()
Definition TQObject.h:197
virtual ~TQObject()
TQObject Destructor.
Definition TQObject.cxx:404
TList * fListOfSignals
Definition TQObject.h:51
TList * fListOfConnections
list of signals from this object
Definition TQObject.h:52
Bool_t AreSignalsBlocked() const
Definition TQObject.h:91
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:869
virtual void ChangedBy(const char *method)
Definition TQObject.h:199
void Emit(const char *signal)
Activate signal without args.
Definition TQObject.h:174
void EmitVA(const char *signal_name, Int_t, const T &... params)
Emit a signal with a varying number of arguments.
Definition TQObject.h:100
virtual void Message(const char *msg)
Definition TQObject.h:201
virtual Bool_t HasConnection(const char *signal_name) const
Return true if there is any object connected to this signal.
Definition TQObject.cxx:527
static Bool_t ConnectToClass(TQObject *sender, const char *signal, TClass *receiver_class, void *receiver, const char *slot)
Create connection between sender and receiver.
Definition TQObject.cxx:563
virtual TClass * IsA() const
Definition TQObject.h:229
virtual void LowPriority(const char *signal_name, const char *slot_name=nullptr)
Definition TQObject.cxx:504
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
static Bool_t AreAllSignalsBlocked()
Returns true if all signals are blocked.
virtual void HighPriority(const char *signal_name, const char *slot_name=nullptr)
Definition TQObject.cxx:477
TQObject & operator=(const TQObject &)=delete
virtual void * GetSender()
Definition TQObject.h:57
TList * GetListOfConnections() const
Definition TQObject.h:89
Bool_t BlockSignals(Bool_t b)
Definition TQObject.h:92
virtual void Connected(const char *)
Definition TQObject.h:194
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
Mediates the link between the signal and the slot.
void SetArgs(const T &... args)
Unpacks the template parameter type and sets arguments of integral and array (scalar) type.
virtual void SendSignal()=0