Logo ROOT   6.14/05
Reference Guide
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-2000, 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 http://www.troll.no/qt/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 powerfull component //
33 // programming mechanism. //
34 // //
35 // This implementation is provided by //
36 // Valeriy Onuchin (onuchin@sirius.ihep.su). //
37 // //
38 //////////////////////////////////////////////////////////////////////////
39 
40 #include "TError.h"
41 #include "TList.h"
42 #include "TString.h"
43 #include "TVirtualQConnection.h"
44 
45 class TClass;
46 
47 R__EXTERN void *gTQSender; // the latest sender object
48 
49 class TQObject {
50 
51 protected:
52  TList *fListOfSignals; //! list of signals from this object
53  TList *fListOfConnections; //! list of connections to this object
54  Bool_t fSignalsBlocked; //! flag used for suppression of signals
55 
56  static Bool_t fgAllSignalsBlocked; // flag used for suppression of all signals
57 
58  virtual void *GetSender() { return this; }
59  virtual const char *GetSenderClassName() const { return ""; }
60 
61 
62  static Bool_t ConnectToClass(TQObject *sender,
63  const char *signal,
64  TClass *receiver_class,
65  void *receiver,
66  const char *slot);
67 
68  static Bool_t ConnectToClass(const char *sender_class,
69  const char *signal,
70  TClass *receiver_class,
71  void *receiver,
72  const char *slot);
73 
74  static Int_t CheckConnectArgs(TQObject *sender,
75  TClass *sender_class, const char *signal,
76  TClass *receiver_class, const char *slot);
77 
78  static TString CompressName(const char *method_name);
79 
80 private:
81  TQObject(const TQObject& tqo); // not implemented
82  TQObject& operator=(const TQObject& tqo); // not implemented
83 
84 public:
85  TQObject();
86  virtual ~TQObject();
87 
89  TList *GetListOfSignals() const { return fListOfSignals; }
91 
94  { Bool_t ret = fSignalsBlocked; fSignalsBlocked = b; return ret; }
95 
96  void CollectClassSignalLists(TList& list, TClass* cls);
97 
98  ///////////////////////////////////////////////////////////////////////////////
99  /// Emit a signal with a varying number of arguments.
100  ///
101  template <typename... T> void EmitVA(const char *signal_name, Int_t /* nargs */, const T&... params)
102  {
103  // Activate signal with variable argument list.
104  // For internal use and for var arg EmitVA() in RQ_OBJECT.h.
105 
106  if (fSignalsBlocked || AreAllSignalsBlocked())
107  return;
108 
109  TList classSigLists;
110  CollectClassSignalLists(classSigLists, IsA());
111 
112  if (classSigLists.IsEmpty() && !fListOfSignals)
113  return;
114 
115  TString signal = CompressName(signal_name);
116 
117  TVirtualQConnection *connection = 0;
118 
119  // execute class signals
120  TList *sigList;
121  TIter nextSigList(&classSigLists);
122  while ((sigList = (TList*) nextSigList())) {
123  TIter nextcl((TList*) sigList->FindObject(signal));
124  while ((connection = static_cast<TVirtualQConnection*>(nextcl()))) {
125  gTQSender = GetSender();
126  connection->SetArgs(params...);
127  connection->SendSignal();
128  }
129  }
130  if (!fListOfSignals)
131  return;
132 
133  // execute object signals
134  TIter next((TList*) fListOfSignals->FindObject(signal));
135  while (fListOfSignals && (connection = static_cast<TVirtualQConnection*>(next()))) {
136  gTQSender = GetSender();
137  connection->SetArgs(params...);
138  connection->SendSignal();
139  }
140  }
141 
142  ////////////////////////////////////////////////////////////////////////////////
143  /// Activate signal with single parameter.
144  /// Example:
145  /// ~~~ {.cpp}
146  /// theButton->Emit("Progress(Long64_t)",processed)
147  /// ~~~
148  ///
149  /// If we call Emit with an array of the parameters, they should be converted
150  /// to long type.
151  /// Example:
152  /// ~~~ {.cpp}
153  /// TQObject *processor; // data processor
154  /// TH1F *hist; // filled with processor results
155  ///
156  /// processor->Connect("Evaluated(Float_t,Float_t)",
157  /// "TH1F",hist,"Fill12(Axis_t,Axis_t)");
158  ///
159  /// Long_t args[2];
160  /// args[0] = (Long_t)processor->GetValue(1);
161  /// args[1] = (Long_t)processor->GetValue(2);
162  ///
163  /// processor->Emit("Evaluated(Float_t,Float_t)",args);
164  /// ~~~
165  template <typename T> void Emit(const char *signal, const T& arg) {
166  Int_t placeholder = 0;
167  EmitVA(signal, placeholder, arg);
168  }
169 
170  ////////////////////////////////////////////////////////////////////////////////
171  /// Acitvate 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 = 0,
182  void *receiver = 0,
183  const char *slot = 0);
184 
185  virtual void HighPriority(const char *signal_name,
186  const char *slot_name = 0);
187 
188  virtual void LowPriority(const char *signal_name,
189  const char *slot_name = 0);
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 = 0,
218  void *receiver = 0,
219  const char *slot = 0);
220 
221  static Bool_t Disconnect(const char *class_name,
222  const char *signal,
223  void *receiver = 0,
224  const char *slot = 0);
225 
226  static Bool_t AreAllSignalsBlocked();
227  static Bool_t BlockAllSignals(Bool_t b);
228 
229  ClassDef(TQObject,1) //Base class for object communication mechanism
230 };
231 
232 
233 class TQObjSender : public TQObject {
234 
235 protected:
236  void *fSender; //delegation object
237  TString fSenderClass; //class name of delegation object
238 
239  virtual void *GetSender() { return fSender; }
240  virtual const char *GetSenderClassName() const { return fSenderClass; }
241 
242 private:
243  TQObjSender(const TQObjSender&); // not implemented
244  TQObjSender& operator=(const TQObjSender&); // not implemented
245 
246 public:
247  TQObjSender() : TQObject(), fSender(0), fSenderClass() { }
248  virtual ~TQObjSender() { Disconnect(); }
249 
250  virtual void SetSender(void *sender) { fSender = sender; }
251  void SetSenderClassName(const char *sclass = "") { fSenderClass = sclass; }
252 
253  ClassDef(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 
264 extern 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
Bool_t ConnectCINT(TQObject *sender, const char *signal, const char *slot)
Global function which simplifies making connection in interpreted ROOT session.
Definition: TQObject.cxx:1067
Bool_t AreSignalsBlocked() const
Definition: TQObject.h:92
static Bool_t BlockAllSignals(Bool_t b)
Block or unblock all signals. Returns the previous block status.
Definition: TQObject.cxx:1055
TQObject()
TQObject Constructor.
Definition: TQObject.cxx:393
virtual void * GetSender()
Definition: TQObject.h:239
static Int_t CheckConnectArgs(TQObject *sender, TClass *sender_class, const char *signal, TClass *receiver_class, const char *slot)
Checking of consitency of sender/receiver methods/arguments.
Definition: TQObject.cxx:181
virtual const char * GetSenderClassName() const
Definition: TQObject.h:59
virtual void * GetSender()
Definition: TQObject.h:58
R__EXTERN void * gTQSender
Definition: TQObject.h:45
double T(double x)
Definition: ChebyshevPol.h:34
TList * fListOfSignals
Definition: TQObject.h:52
Bool_t BlockSignals(Bool_t b)
Definition: TQObject.h:93
This is the ROOT implementation of the Qt object communication mechanism (see also http://www...
Definition: TQObject.h:49
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void EmitVA(const char *signal_name, Int_t, const T &... params)
Emit a signal with a varying number of arguments.
Definition: TQObject.h:101
virtual TObject * FindObject(const char *name) const
Delete a TObjLink object.
Definition: TList.cxx:574
#define ClassDef(name, id)
Definition: Rtypes.h:320
void SetArgs(const T &... args)
Unpacks the template parameter type and sets arguments of integral and array (scalar) type...
TList * GetListOfClassSignals() const
Returns pointer to list of signals of this class.
Definition: TQObject.cxx:435
void * fSender
Definition: TQObject.h:236
void Emit(const char *signal)
Acitvate signal without args.
Definition: TQObject.h:174
A doubly linked list.
Definition: TList.h:44
TList * fListOfConnections
list of signals from this object
Definition: TQObject.h:53
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:867
void SetSenderClassName(const char *sclass="")
Definition: TQObject.h:251
static Bool_t fgAllSignalsBlocked
flag used for suppression of signals
Definition: TQObject.h:56
virtual const char * GetSenderClassName() const
Definition: TQObject.h:240
TQObject & operator=(const TQObject &tqo)
virtual Int_t NumberOfSignals() const
Return number of signals for this object.
Definition: TQObject.cxx:539
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:561
virtual Int_t NumberOfConnections() const
Return number of connections for this object.
Definition: TQObject.cxx:549
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
virtual ~TQObject()
TQObject Destructor.
Definition: TQObject.cxx:404
Bool_t fSignalsBlocked
list of connections to this object
Definition: TQObject.h:54
virtual void Message(const char *msg)
Definition: TQObject.h:201
virtual void Destroyed()
Definition: TQObject.h:197
virtual Bool_t IsEmpty() const
Definition: TCollection.h:186
static Bool_t AreAllSignalsBlocked()
Returns true if all signals are blocked.
Definition: TQObject.cxx:1047
virtual void Disconnected(const char *)
Definition: TQObject.h:195
TString fSenderClass
Definition: TQObject.h:237
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Definition: TQObject.cxx:1025
virtual Bool_t HasConnection(const char *signal_name) const
Return true if there is any object connected to this signal.
Definition: TQObject.cxx:525
TList * GetListOfSignals() const
Definition: TQObject.h:89
virtual ~TQObjSender()
Definition: TQObject.h:248
#define R__EXTERN
Definition: DllImport.h:27
virtual void SetSender(void *sender)
Definition: TQObject.h:250
virtual void ChangedBy(const char *method)
Definition: TQObject.h:199
virtual void Connected(const char *)
Definition: TQObject.h:194
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:100
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition: TQObject.h:165
Mediates the link between the signal and the slot.
virtual void LowPriority(const char *signal_name, const char *slot_name=0)
Definition: TQObject.cxx:503
void CollectClassSignalLists(TList &list, TClass *cls)
Collect class signal lists from class cls and all its base-classes.
Definition: TQObject.cxx:451
virtual void SendSignal()=0
virtual void HighPriority(const char *signal_name, const char *slot_name=0)
Definition: TQObject.cxx:477
TList * GetListOfConnections() const
Definition: TQObject.h:90