ubit
ucall.hpp
1 /************************************************************************
2  *
3  * ucall.hpp: Callback objects
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _ucall_hpp_
18 #define _ucall_hpp_ 1
19 #include <ubit/unode.hpp>
20 #include <ubit/uevent.hpp>
21 #include <typeinfo>
22 namespace ubit {
23 
144  class UCall: public UNode {
145  public:
146  //UCLASS("#call", UCall, null)
147  UABSTRACT_CLASS(UCall)
148 
149  virtual ~UCall() {destructs();}
150 
151  virtual void operator()(UEvent&) = 0;
153 
154  protected:
155  virtual UCall* toCall() {return this;}
156  virtual const UCall* toCall() const {return this;}
157  virtual void addingTo(UChild&, UElem& parent);
158 
159  template <class E>
160  E* checkType(UEvent& e) {
161  E* actual_e = dynamic_cast<E*>(&e);
162  if (actual_e) return actual_e;
163  else {
164  wrongEventType(typeid(E).name(), typeid(e).name());
165  return null;
166  //return (E*)&e; !!!
167  }
168  }
169 
170  virtual void wrongEventType(const char* event_name, const char* call_name);
171  };
172 
173 
174  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
175 
177  UCall& ucloseWin(int stat = 0);
178 
179 
180  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181  // uopen()
182 
183  template <class CC>
184  class UCall_open : public UCall {
185  CC obj;
186  public:
187  UCall_open(CC _o) : obj(_o) {}
188  void operator()(UEvent& e) {
189  UMouseEvent*_e = checkType<UMouseEvent>(e); if (_e) obj.open(*_e);
190  }
191  };
192 
193 
195  template <class CC>
196  UCall& uopen(CC& obj) {return *new UCall_open<CC&>(obj);}
197 
199  template <class CC>
200  UCall& uopen(CC* obj) {return *new UCall_open<CC&>(*obj);}
201 
202 
203  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204  // ushow()
205 
206  template <class CC, class VAL>
207  class UCall_show : public UCall {
208  CC obj; VAL val;
209  public:
210  UCall_show(CC _o, VAL _v) : obj(_o), val(_v) {}
211  void operator()(UEvent&) {obj.show(val);}
212  };
213 
214 
216  template <class CC>
217  UCall& ushow(CC& obj) {return *new UCall_show<CC&,bool>(obj,true);}
218 
220  template <class CC>
221  UCall& ushow(CC* obj) {return *new UCall_show<CC&,bool>(*obj,true);}
222 
223 
225  // NB: not available with g++2.*.* doit etre en premier avec CC car valeur par defaut
226  template <class CC, class VAL>
227  UCall& ushow(CC& obj, VAL state) {return *new UCall_show<CC&,VAL>(obj,state);}
228 
230  template <class CC, class VAL>
231  UCall& ushow(CC* obj, VAL state) {return *new UCall_show<CC&,VAL>(*obj,state);}
232 
233 
234  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
235  // uset(x, y)
236 
237  template <class CC, class VAL>
238  class UCall_set : public UCall {
239  CC obj; VAL val;
240  public:
241  UCall_set(CC _o, VAL _v) : obj(_o), val(_v) {}
242  void operator()(UEvent&) {obj.set(val);}
243  };
244 
250  template <class CC, class VAL>
251  UCall& uset(CC& _o, VAL& _v) {return *new UCall_set<CC&,VAL&>(_o,_v);}
252 
253 
254  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255  // uassign(x, y)
256 
257  template <class CC, class VAL>
258  class UCall_assign : public UCall {
259  CC obj; VAL val;
260  public:
261  UCall_assign(CC _o, VAL _v) : obj(_o), val(_v) {}
262  void operator()(UEvent&) {obj = val;}
263  };
264 
271  template <class CC, class VAL>
272  UCall& uassign(CC& _o, VAL& _v) {return *new UCall_assign<CC&,VAL&>(_o,_v);}
273 
274  template <class CC, class VAL>
275  UCall& ucopy(CC& _o, VAL _v) {return *new UCall_assign<CC&,VAL>(_o,_v);}
276 
277 
278  // ============================================================== [Elc] ========
279  // Callback objects for firing objects' methods.
280 
281  // ucall(obj, &Obj::method)
282 
283  template <class O, class R, class M>
284  class UCall_M0 : public UCall {
285  O obj;
286  R (M::*fun)();
287  public:
288  UCall_M0(O o, R(M::*m)()) : obj(o), fun(m) {}
289  void operator()(UEvent&) {(obj.*fun)();}
290  };
291 
292  template <class O, class R, class M, class E>
293  class UCall_M0E : public UCall {
294  O obj;
295  R (M::*fun)(E&);
296  public:
297  UCall_M0E(O o, R(M::*m)(E&)) : obj(o), fun(m) {}
298  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (obj.*fun)(*_e);}
299  };
300 
301 
302  template <class O, class R, class M>
303  UCall& ucall(O& o, R(M::*m)()) {return *new UCall_M0<O&, R,M>(o,m);}
305 
306  template <class O, class R, class M, class E>
307  UCall& ucall(O& o, R(M::*m)(E&)) {return *new UCall_M0E<O&, R,M,E>(o,m);}
309 
310  template <class O, class R, class M>
311  UCall& ucall(O* o, R(M::*m)()) {return *new UCall_M0<O&, R,M>(*o,m);}
313 
314  template <class O, class R, class M, class E>
315  UCall& ucall(O* o, R(M::*m)(E&)) {return *new UCall_M0E<O&, R,M,E>(*o,m);}
317 
318 
319  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
320  // ucall(obj, arg1, &Obj::method)
321 
322  template <class O, class A, class R, class M>
323  class UCall_M1 : public UCall {
324  O obj;
325  R (M::*fun)(A);
326  A arg;
327  public:
328  UCall_M1(O o, R(M::*m)(A), A a) : obj(o), fun(m), arg(a) {}
329  void operator()(UEvent&) {(obj.*fun)(arg);}
330  };
331 
332  template <class O, class A, class R, class M, class E>
333  class UCall_M1E : public UCall {
334  O obj;
335  R (M::*fun)(E&, A);
336  A arg1;
337  public:
338  UCall_M1E(O o, R(M::*m)(E&,A), A a) : obj(o), fun(m), arg1(a) {}
339  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (obj.*fun)(*_e,arg1);}
340  };
341 
342 
343  template <class O, class A, class R, class M>
344  UCall& ucall(O& o, A a, R(M::*m)(A)) {return *new UCall_M1<O&, A,R,M>(o,m,a);}
346 
347  template <class O, class A, class R, class M>
348  UCall& ucall(O& o, A& a, R(M::*m)(A&)) {return *new UCall_M1<O&, A&,R,M>(o,m,a);}
350 
351  template <class O, class A, class R, class M, class E>
352  UCall& ucall(O& o, A a, R(M::*m)(E&,A)) {return *new UCall_M1E<O&, A,R,M,E>(o,m,a);}
354 
355  template <class O, class A, class R, class M, class E>
356  UCall& ucall(O& o, A& a, R(M::*m)(E&,A&)) {return *new UCall_M1E<O&, A&,R,M,E>(o,m,a);}
358 
359 
360  template <class O, class A, class R, class M>
361  UCall& ucall(O* o, A a, R(M::*m)(A)) {return *new UCall_M1<O&, A,R,M>(*o,m,a);}
363 
364  template <class O, class A, class R, class M>
365  UCall& ucall(O* o, A& a, R(M::*m)(A&)) {return *new UCall_M1<O&, A&,R,M>(*o,m,a);}
367 
368  template <class O, class A, class R, class M, class E>
369  UCall& ucall(O* o, A a, R(M::*m)(E&,A)) {return *new UCall_M1E<O&, A,R,M,E>(*o,m,a);}
371 
372  template <class O, class A, class R, class M, class E>
373  UCall& ucall(O* o, A& a, R(M::*m)(E&,A&)) {return *new UCall_M1E<O&, A&,R,M,E>(*o,m,a);}
375 
376 
377  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
378  // ucall(obj, arg1, args2, &Obj::method)
379 
380  template <class O, class A1, class A2, class R, class M>
381  class UCall_M2 : public UCall {
382  O obj;
383  R (M::*fun)(A1,A2);
384  A1 arg1;
385  A2 arg2;
386  public:
387  UCall_M2(O o, R(M::*m)(A1,A2), A1 a1, A2 a2) : obj(o), fun(m), arg1(a1), arg2(a2) {}
388  void operator()(UEvent&) {(obj.*fun)(arg1, arg2);}
389  };
390 
391  template <class O, class A1, class A2, class R, class M, class E>
392  class UCall_M2E : public UCall {
393  O obj;
394  R (M::*fun)(E&,A1,A2);
395  A1 arg1;
396  A2 arg2;
397  public:
398  UCall_M2E(O o, R(M::*m)(E&,A1,A2), A1 a1, A2 a2): obj(o),fun(m),arg1(a1),arg2(a2) {}
399  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (obj.*fun)(*_e,arg1,arg2);}
400  };
401 
402  template <class O, class A1, class A2, class R, class M>
403  UCall& ucall(O& o, A1 a1, A2 a2, R(M::*m)(A1,A2))
404  {return *new UCall_M2<O&,A1,A2,R,M>(o, m, a1, a2);}
406 
407  template <class O, class A1, class A2, class R, class M, class E>
408  UCall& ucall(O& o, A1 a1, A2 a2, R(M::*m)(E&,A1,A2))
409  {return *new UCall_M2E<O&,A1,A2,R,M,E>(o, m, a1, a2);}
411 
412  template <class O, class A1, class A2, class R, class M>
413  UCall& ucall(O* o, A1 a1, A2 a2, R(M::*m)(A1,A2))
414  {return *new UCall_M2<O&,A1,A2,R,M>(*o, m, a1, a2);}
416 
417  template <class O, class A1, class A2, class R, class M, class E>
418  UCall& ucall(O* o, A1 a1, A2 a2, R(M::*m)(E&,A1,A2))
419  {return *new UCall_M2E<O&,A1,A2,R,M,E>(*o, m, a1, a2);}
421 
422 
423  // ============================================================ [Elc] ========
424  // Callback objects for firing functions.
425 
426  // ucall(func)
427 
428  template <class R>
429  class UCall_F0 : public UCall {
430  R (*fun)();
431  public:
432  UCall_F0( R(*f)()) : fun(f) {}
433  void operator()(UEvent&) {(*fun)();}
434  };
435 
436  template <class R, class E>
437  class UCall_F0E : public UCall {
438  R (*fun)(E&);
439  public:
440  UCall_F0E( R(*f)(E&)) : fun(f) {}
441  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (*fun)(*_e);}
442  };
443 
444 
445  template <class R>
446  UCall& ucall(R(*f)()) {return *new UCall_F0<R>(f);}
448 
449  template <class R, class E>
450  UCall& ucall(R(*f)(E&)) {return *new UCall_F0E<R,E>(f);}
452 
453 
454  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
455  // ucall(arg, function)
456 
457  template <class A, class R>
458  class UCall_F1 : public UCall {
459  R (*fun)(A);
460  A arg;
461  public:
462  UCall_F1( R(*f)(A), A a) : fun(f), arg(a) {}
463  void operator()(UEvent&) {(*fun)(arg);}
464  };
465 
466  template <class A, class R, class E>
467  class UCall_F1E : public UCall {
468  R (*fun)(E&, A);
469  A arg1;
470  public:
471  UCall_F1E( R(*f)(E&,A), A a) : fun(f), arg1(a) {}
472  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (*fun)(*_e,arg1);}
473  };
474 
475 
476  template <class A, class R>
477  UCall& ucall(A a, R(*f)(A)) {return *new UCall_F1<A,R>(f,a);}
479 
480  template <class A, class R>
481  UCall& ucall(A& a, R(*f)(A&)) {return *new UCall_F1<A&,R>(f,a);}
483 
484  template <class A, class R, class E>
485  UCall& ucall(A a, R(*f)(E&,A)) {return *new UCall_F1E<A,R,E>(f,a);}
487 
488  template <class A, class R, class E>
489  UCall& ucall(A& a, R(*f)(E&,A&)) {return *new UCall_F1E<A&,R,E>(f,a);}
491 
492 
493  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
494  // ucall(arg1, arg2, function)
495 
496  template <class A1, class A2, class R>
497  class UCall_F2 : public UCall {
498  R (*fun)(A1,A2);
499  A1 arg1; A2 arg2;
500  public:
501  UCall_F2( R(*f)(A1,A2), A1 a1, A2 a2) : fun(f), arg1(a1), arg2(a2) {}
502  void operator()(UEvent&) {(*fun)(arg1,arg2);}
503  };
504 
505  template <class A1, class A2, class R, class E>
506  class UCall_F2E : public UCall {
507  R (*fun)(E&,A1,A2);
508  A1 arg1;
509  A2 arg2;
510  public:
511  UCall_F2E( R(*f)(E&,A1,A2), A1 a1, A2 a2) : fun(f), arg1(a1), arg2(a2) {}
512  void operator()(UEvent& e) {E*_e = checkType<E>(e); if(_e) (*fun)(*_e,arg1,arg2);}
513  };
514 
515  template <class A1, class A2, class R>
516  UCall& ucall(A1 a1, A2 a2, R(*f)(A1,A2)) {return *new UCall_F2<A1,A2,R>(f,a1,a2);}
518 
519  template <class A1, class A2, class R, class E>
520  UCall& ucall(A1 a1, A2 a2, R(*f)(E&,A1,A2)) {return *new UCall_F2E<A1,A2,R,E>(f,a1,a2);}
522 
523 }
524 #endif
Definition: ucall.hpp:458
Definition: ucall.hpp:381
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:289
Base class of objects that can be added to the UBIT scene graph (SEE DETAILS!).
Definition: unode.hpp:38
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:262
Ubit Event class.
Definition: uevent.hpp:30
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:188
virtual void addingTo(UChild &, UElem &parent)
called when this object is added to a parent.
Definition: ucall.cpp:38
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:242
Definition: ucall.hpp:497
virtual const UCall * toCall() const
dynamic cast: returns this object if it derives from UCall and null otherwise.
Definition: ucall.hpp:156
Definition: ucall.hpp:437
Definition: tstclass.hpp:25
Definition: ucall.hpp:258
Definition: ucall.hpp:293
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:433
Definition: ucall.hpp:506
Definition: ucall.hpp:392
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:463
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:472
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:502
Definition: ucall.hpp:207
Definition: ucall.hpp:284
lightweight general purpose container.
Definition: uelem.hpp:44
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:441
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:388
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:329
base class of callback objects for firing functions or methods.
Definition: ucall.hpp:144
void operator()(UEvent &)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:211
Definition: ucall.hpp:184
Definition: ucall.hpp:429
virtual void destructs()
unlinks the object from its parents and destroys its children.
Definition: unode.cpp:56
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:512
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:399
virtual void operator()(UEvent &)=0
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:467
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:339
Definition: uhardfont.hpp:31
Definition: ucall.hpp:333
Definition: ucall.hpp:323
virtual UCall * toCall()
dynamic cast: returns this object if it derives from UCall and null otherwise.
Definition: ucall.hpp:155
void operator()(UEvent &e)
the method that fires callbacks; must be redefined by subclasses.
Definition: ucall.hpp:298
mouse events
Definition: uevent.hpp:172
[impl] Internal implementation of a child node.
Definition: uchild.hpp:23
Definition: ucall.hpp:238