ucall.hpp

00001 /* ==================================================== ======== ======= *
00002  *
00003  *  ucall.hpp: callback objects
00004  *  Ubit Project
00005  *  Part of the Ubit Toolkit: A Brick Construction Game Model for Creating GUIs
00006  *  (C) 1999-2006 / Eric Lecolinet / ENST Paris / http://www.enst.fr/~elc/ubit
00007  *
00008  * ***********************************************************************
00009  * COPYRIGHT NOTICE : 
00010  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE 
00011  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
00012  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU 
00013  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
00014  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00015  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
00016  * ***********************************************************************
00017  * ==================================================== [(c)Elc] ======= *
00018  * ==================================================== ======== ======= */
00019 
00020 #ifndef _ucall_hpp_
00021 #define _ucall_hpp_
00022 //pragma ident  "@(#)ucall.hpp  ubit:05.05.00"
00023 #include <ubit/ubrick.hpp>
00024 namespace ubit {
00025 
00124 class UCall: public UBrick {
00125 public:
00126   UBIT_ABSTRACT_CLASS(UCall,UBrick)
00127   
00128   virtual void operator()(UEvent&) = 0;
00130 
00131   virtual ~UCall() {destructs();}
00132 
00133   virtual UCall* callCast() {return this;}
00134 
00135   virtual void addingTo(UChild* self, UGroup* parent);
00137 };
00138 
00139 /* ==================================================== ====== ======= */
00140 // uset(x, y)
00141 
00142 template <class CC, class VAL>
00143 class UCall_set : public UCall {
00144   CC obj; VAL val;
00145 public:
00146   UCall_set(CC _o, VAL _v) : obj(_o), val(_v) {}
00147   void operator()(UEvent&) {obj.set(val);}
00148 };
00149 
00155 template <class CC, class VAL> 
00156 UCall& uset(CC& _o, VAL& _v) {return *new UCall_set<CC&,VAL&>(_o,_v);}
00157 
00158 /* ==================================================== ====== ======= */
00159 // uassign(x, y)
00160 
00161 template <class CC, class VAL>
00162 class UCall_assign : public UCall {
00163   CC obj; VAL val;
00164 public:
00165   UCall_assign(CC _o, VAL _v) : obj(_o), val(_v) {}
00166   void operator()(UEvent&) {obj = val;}
00167 };
00168 
00175 template <class CC, class VAL> 
00176 UCall& uassign(CC& _o, VAL& _v) {return *new UCall_assign<CC&,VAL&>(_o,_v);}
00177 
00178 template <class CC, class VAL> 
00179 UCall& ucopy(CC& _o, VAL _v) {return *new UCall_assign<CC&,VAL>(_o,_v);}
00180 
00181 /* ==================================================== ====== ======= */
00182 // ushow(box, bool)
00183 
00184 template <class CC, class VAL>
00185 class UCall_show : public UCall {
00186   CC obj; VAL val;
00187 public:
00188   UCall_show(CC _o, VAL _v) : obj(_o), val(_v) {}
00189   void operator()(UEvent&) {obj.show(val);}
00190 };
00191 
00197 template <class CC, class VAL> 
00198 UCall& ushow(CC& _o, VAL _v = true) {return *new UCall_show<CC&,VAL>(_o,_v);}
00199 
00203 template <class CC, class VAL> 
00204 UCall& ushow(CC* _o, VAL _v) {return *new UCall_show<CC&,VAL>(*_o,_v);}
00205 
00206 /* ==================================================== ===== ======= */
00207 // compatibility 
00208 
00209   UCall& ucloseWin(int stat = 0);
00210 
00211   UCall& ucompactEvents(UCall&);
00212 
00213 // ==================================================== ====== =======
00214 // ucall(obj, &ObjClass::foo) : calls obj->foo() or obj->foo(event)
00215 
00216 template <class O, class R, class M> 
00217 class UCall_M0 : public UCall {
00218   O obj;
00219   R (M::*fun)();
00220 public:
00221   UCall_M0(O o, R(M::*m)()) : obj(o), fun(m) {}
00222   void operator()(UEvent&) {(obj->*fun)();}
00223 };
00224 
00225 template <class O, class R,  class M, class E> 
00226 class UCall_M0E : public UCall {
00227   O obj;
00228   R (M::*fun)(E&);
00229 public:
00230   UCall_M0E(O o, R(M::*m)(E&)) : obj(o), fun(m) {}
00231   void operator()(UEvent& e) {E& _e = (E&)e; (obj->*fun)(_e);}
00232 };
00233 
00234 template <class O, class R, class M> 
00235 UCall& ucall(O* o, R(M::*m)()) {return *new UCall_M0<O*,R,M>(o,m);}
00236 
00237 template <class O, class R, class M, class E> 
00238 UCall& ucall(O* o, R(M::*m)(E&)) {return *new UCall_M0E<O*,R,M,E>(o,m);}
00239 
00240 // ==================================================== ====== =======
00241 // ucall(obj, a, &ObjClass::foo) : calls obj->foo(a) or obj->foo(event,a)
00242 
00243 template <class O, class A, class R, class M> 
00244 class UCall_M1 : public UCall {
00245   O obj;
00246   R (M::*fun)(A);
00247   A arg;
00248 public:
00249   UCall_M1(O o, R(M::*m)(A), A a) : obj(o), fun(m), arg(a) {}
00250   void operator()(UEvent&) {(obj->*fun)(arg);}
00251 };
00252 
00253 template <class O, class A, class R, class M, class E> 
00254 class UCall_M1E : public UCall {
00255   O obj;
00256   R (M::*fun)(E&, A);
00257   A arg;
00258 public:
00259   UCall_M1E(O o, R(M::*m)(E&,A), A a) : obj(o), fun(m), arg(a) {}
00260   void operator()(UEvent& e) {E& _e = (E&)e; (obj->*fun)(_e, arg);}
00261 };
00262 
00263 template <class O, class A, class R, class M>
00264 UCall& ucall(O* o, A a, R(M::*m)(A)) {return *new UCall_M1<O*,A,R,M>(o,m,a);}
00265 
00266 template <class O, class A, class R, class M, class E> 
00267 UCall& ucall(O* o, A a, R(M::*m)(E&,A)) {return *new UCall_M1E<O*,A,R,M,E>(o,m,a);}
00268 
00269 // ==================================================== ====== ======= 
00270 // ucall(obj, a, b, &ObjClass::foo) : calls obj->foo(a,b) or obj->foo(event,a,b)
00271 
00272 template <class O, class A1, class A2, class R, class M> 
00273 class UCall_M2 : public UCall {
00274   O obj;
00275   R (M::*fun)(A1,A2);
00276   A1 arg1; A2 arg2;
00277 public:
00278   UCall_M2(O o, R(M::*m)(A1,A2), A1 a1, A2 a2) : obj(o), fun(m), arg1(a1), arg2(a2) {} 
00279   void operator()(UEvent&) {(obj->*fun)(arg1, arg2);}
00280 };
00281 
00282 template <class O, class A1, class A2, class R, class M, class E> 
00283 class UCall_M2E : public UCall {
00284   O obj;
00285   R (M::*fun)(E&,A1,A2);
00286   A1 arg1; A2 arg2;
00287 public:
00288   UCall_M2E(O o, R(M::*m)(E&,A1,A2), A1 a1, A2 a2) : obj(o), fun(m), arg1(a1), arg2(a2) {} 
00289   void operator()(UEvent& e) {E& _e = (E&)e; (obj->*fun)(_e, arg1, arg2);}
00290 };
00291 
00292 template <class O, class A1, class A2, class R, class M> 
00293 UCall& ucall(O* o, A1 a1, A2 a2, R(M::*m)(A1,A2)) {
00294   return *new UCall_M2<O*,A1,A2,R,M>(o,m,a1,a2);
00295 }
00296 
00297 template <class O, class A1, class A2, class R, class M, class E> 
00298 UCall& ucall(O* o, A1 a1, A2 a2, R(M::*m)(E&,A1,A2)) {
00299   return *new UCall_M2E<O*,A1,A2,R,M,E>(o,m,a1,a2);
00300 }
00301 
00302 /* ==================================================== [Elc:] ======= */
00303 /* ==================================================== ====== ======= */
00304 // Callback Function with ONE argument
00305 
00306 template <class A, class R> 
00307 class UCall_F1 : public UCall {
00308   R (*fun)(A);
00309   A arg;
00310 public:
00311   UCall_F1( R(*f)(A), A a) : fun(f), arg(a) {}
00312   void operator()(UEvent&) {(*fun)(arg);}
00313 };
00314 
00315 template <class A, class R, class E> 
00316 class UCall_F1E : public UCall {
00317   R (*fun)(E&, A);
00318   A arg;
00319 public:
00320   UCall_F1E( R(*f)(E&,A), A a) : fun(f), arg(a) {}
00321   void operator()(UEvent& e) {E& _e = (E&)e; (*fun)(_e, arg);}
00322 };
00323 
00324 template <class A, class R> 
00325 UCall& ucall(A a, R(*f)(A)) {return *new UCall_F1<A,R>(f,a);}
00326 
00327 template <class A, class R, class E> 
00328 UCall& ucall(A a, R(*f)(E&,A)) {return *new UCall_F1E<A,R,E>(f,a);}
00329 
00330 template <class A, class R> 
00331 class Truc_F1 {
00332   const UCond& cond;
00333   R (*fun)(A);
00334 public:
00335   Truc_F1(const UCond& c, R(*f)(A)) : cond(c), fun(f) {}
00336   UChild operator()(A a) {return UChild(new UCall_F1<A,R>(fun,a), cond);}
00337 };
00338 
00339 template <class A, class R, class E> 
00340 class Truc_F1E {
00341   const UCond& cond;
00342   R (*fun)(E&,A);
00343 public:
00344   Truc_F1E(const UCond& c, R(*f)(E&,A)) : cond(c), fun(f) {}
00345   UChild operator()(A a) {return UChild(new UCall_F1E<A,R,E>(fun,a), cond);}
00346 };
00347 
00348 /* ==================================================== [Elc:] ======= */
00349 /* ==================================================== ====== ======= */
00350 // Callback Function with TWO arguments
00351 
00352 template <class A1, class A2, class R> 
00353 class UCall_F2 : public UCall {
00354   R (*fun)(A1,A2);
00355   A1 arg1; A2 arg2;
00356 public:
00357   UCall_F2( R(*f)(A1,A2), A1 a1, A2 a2) : fun(f), arg1(a1), arg2(a2) {}
00358   void operator()(UEvent&) {(*fun)(arg1,arg2);}
00359 };
00360 
00361 template <class A1, class A2, class R, class E> 
00362 class UCall_F2E : public UCall {
00363   R (*fun)(E&,A1,A2);
00364   A1 arg1; A2 arg2;
00365 public:
00366   UCall_F2E( R(*f)(E&,A1,A2), A1 a1, A2 a2) : fun(f), arg1(a1), arg2(a2) {}
00367   void operator()(UEvent& e) {E& _e = (E&)e; (*fun)(_e,arg1,arg2);}
00368 };
00369 
00370 template <class A1, class A2, class R> 
00371 UCall& ucall(A1 a1, A2 a2, R(*f)(A1,A2)) {return *new UCall_F2<A1,A2,R>(f,a1,a2);}
00372 
00373 template <class A1, class A2, class R, class E> 
00374 UCall& ucall(A1 a1, A2 a2, R(*f)(E&,A1,A2)) {return *new UCall_F2E<A1,A2,R,E>(f,a1,a2);}
00375 
00376 template <class A1, class A2, class R> 
00377 class Truc_F2 {
00378   const UCond& cond;
00379   R (*fun)(A1,A2);
00380 public:
00381   Truc_F2(const UCond& c, R(*f)(A1,A2)) : cond(c), fun(f) {}
00382   UChild operator()(A1 a1, A2 a2) 
00383   {return UChild(new UCall_F2<A1,A2,R>(fun,a1,a2), cond);}
00384 };
00385 
00386 template <class A1, class A2, class R, class E> 
00387 class Truc_F2E {
00388   const UCond& cond;
00389   R (*fun)(E&,A1,A2);
00390 public:
00391   Truc_F2E(const UCond& c, R(*f)(E&,A1,A2)) : cond(c), fun(f) {}
00392   UChild operator()(A1 a1, A2 a2) 
00393   {return UChild(new UCall_F2E<A1,A2,R,E>(fun,a1,a2), cond);}
00394 };
00395 
00396 }
00397 #endif
00398 /* ==================================================== [TheEnd] ======= */
00399 /* ==================================================== [(c)Elc] ======= */

Generated on Mon Jan 29 00:20:37 2007 for Ubit by  doxygen 1.4.7