00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #ifndef __UT_Functor_h__
00064 #define __UT_Functor_h__
00065
00066 #include "UT_Assert.h"
00067
00068
00069
00070
00071
00072 template <typename R>
00073 class UT_FunctorImpl
00074 {
00075 public:
00076 typedef R ReturnType;
00077 virtual ReturnType operator()() const = 0;
00078
00079
00080 virtual UT_FunctorImpl<ReturnType> *clone() const = 0;
00081
00082
00083
00084 virtual ~UT_FunctorImpl() {}
00085 };
00086
00087
00088
00089 template <typename R, typename P1>
00090 class UT_FunctorImpl1
00091 {
00092 public:
00093 typedef R ReturnType;
00094 typedef P1 Parm1Type;
00095 virtual ReturnType operator()(Parm1Type parm1) const = 0;
00096 virtual UT_FunctorImpl1<ReturnType, Parm1Type> *clone() const = 0;
00097 virtual ~UT_FunctorImpl1() {}
00098 };
00099
00100
00101
00102 template <typename R, typename P1, typename P2>
00103 class UT_FunctorImpl2
00104 {
00105 public:
00106 typedef R ReturnType;
00107 typedef P1 Parm1Type;
00108 typedef P2 Parm2Type;
00109 virtual ReturnType operator()(Parm1Type parm1, Parm2Type parm2) const = 0;
00110 virtual UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *clone() const =0;
00111 virtual ~UT_FunctorImpl2() {}
00112 };
00113
00114
00115
00116
00117
00118
00119 template <typename ParentFunctor, typename FunctorType>
00120 class UT_FunctorHandler
00121 : public UT_FunctorImpl<typename ParentFunctor::ReturnType>
00122 {
00123 public:
00124 typedef typename ParentFunctor::ReturnType ReturnType;
00125
00126 UT_FunctorHandler(const FunctorType &functor)
00127 : myFunctor(functor)
00128 {}
00129
00130
00131
00132
00133 virtual UT_FunctorImpl<ReturnType> *clone() const
00134 { return new UT_FunctorHandler<ParentFunctor, FunctorType>(*this); }
00135
00136 virtual ReturnType operator()() const
00137 { return myFunctor(); }
00138
00139 private:
00140 FunctorType myFunctor;
00141 };
00142
00143
00144
00145 template <typename ParentFunctor, typename FunctorType>
00146 class UT_FunctorHandler1
00147 : public UT_FunctorImpl1<typename ParentFunctor::ReturnType,
00148 typename ParentFunctor::Parm1Type>
00149 {
00150 public:
00151 typedef typename ParentFunctor::ReturnType ReturnType;
00152 typedef typename ParentFunctor::Parm1Type Parm1Type;
00153 UT_FunctorHandler1(const FunctorType &functor)
00154 : myFunctor(functor) {}
00155
00156 virtual UT_FunctorImpl1<ReturnType, Parm1Type> *clone() const
00157 { return new UT_FunctorHandler1<ParentFunctor, FunctorType>(*this); }
00158
00159 virtual ReturnType operator()(Parm1Type parm1) const
00160 { return myFunctor(parm1); }
00161
00162 private:
00163 FunctorType myFunctor;
00164 };
00165
00166
00167
00168 template <typename ParentFunctor, typename FunctorType>
00169 class UT_FunctorHandler2
00170 : public UT_FunctorImpl2<typename ParentFunctor::ReturnType,
00171 typename ParentFunctor::Parm1Type,
00172 typename ParentFunctor::Parm2Type>
00173 {
00174 public:
00175 typedef typename ParentFunctor::ReturnType ReturnType;
00176 typedef typename ParentFunctor::Parm1Type Parm1Type;
00177 typedef typename ParentFunctor::Parm2Type Parm2Type;
00178 UT_FunctorHandler2(const FunctorType &functor)
00179 : myFunctor(functor) {}
00180
00181 virtual UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *clone() const
00182 { return new UT_FunctorHandler2<ParentFunctor, FunctorType>(*this); }
00183
00184 virtual ReturnType operator()(Parm1Type parm1, Parm2Type parm2) const
00185 { return myFunctor(parm1, parm2); }
00186
00187 private:
00188 FunctorType myFunctor;
00189 };
00190
00191
00192
00193
00194 template <typename ParentFunctor, typename PointerToObj,
00195 typename PointerToMemFn>
00196 class UT_MemFunHandler
00197 : public UT_FunctorImpl<typename ParentFunctor::ReturnType>
00198 {
00199 public:
00200 typedef typename ParentFunctor::ReturnType ReturnType;
00201
00202 UT_MemFunHandler(const PointerToObj &pointer_to_obj,
00203 const PointerToMemFn pointer_to_mem_fn)
00204 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00205 {}
00206
00207 virtual UT_FunctorImpl<ReturnType> *clone() const
00208 { return new UT_MemFunHandler<ParentFunctor, PointerToObj,
00209 PointerToMemFn>(*this); }
00210
00211 virtual ReturnType operator()() const
00212 { return ((*myPointerToObj).*myPointerToMemFn)(); }
00213
00214 private:
00215 PointerToObj myPointerToObj;
00216 PointerToMemFn myPointerToMemFn;
00217 };
00218
00219
00220
00221 template <typename ParentFunctor, typename PointerToObj,
00222 typename PointerToMemFn>
00223 class UT_MemFunHandler1
00224 : public UT_FunctorImpl1<typename ParentFunctor::ReturnType,
00225 typename ParentFunctor::Parm1Type>
00226 {
00227 public:
00228 typedef typename ParentFunctor::ReturnType ReturnType;
00229 typedef typename ParentFunctor::Parm1Type Parm1Type;
00230
00231 UT_MemFunHandler1(const PointerToObj &pointer_to_obj,
00232 const PointerToMemFn pointer_to_mem_fn)
00233 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00234 {}
00235
00236 virtual UT_FunctorImpl1<ReturnType, Parm1Type> *clone() const
00237 { return new UT_MemFunHandler1<ParentFunctor, PointerToObj,
00238 PointerToMemFn>(*this); }
00239
00240 virtual ReturnType operator()(Parm1Type parm1) const
00241 { return ((*myPointerToObj).*myPointerToMemFn)(parm1); }
00242
00243 private:
00244 PointerToObj myPointerToObj;
00245 PointerToMemFn myPointerToMemFn;
00246 };
00247
00248
00249
00250 template <typename ParentFunctor, typename PointerToObj,
00251 typename PointerToMemFn>
00252 class UT_MemFunHandler2
00253 : public UT_FunctorImpl2<typename ParentFunctor::ReturnType,
00254 typename ParentFunctor::Parm1Type,
00255 typename ParentFunctor::Parm2Type>
00256 {
00257 public:
00258 typedef typename ParentFunctor::ReturnType ReturnType;
00259 typedef typename ParentFunctor::Parm1Type Parm1Type;
00260 typedef typename ParentFunctor::Parm2Type Parm2Type;
00261
00262 UT_MemFunHandler2(const PointerToObj &pointer_to_obj,
00263 const PointerToMemFn pointer_to_mem_fn)
00264 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00265 {}
00266
00267 virtual UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *clone() const
00268 { return new UT_MemFunHandler2<ParentFunctor, PointerToObj,
00269 PointerToMemFn>(*this); }
00270
00271 virtual ReturnType operator()(Parm1Type parm1, Parm2Type parm2) const
00272 { return ((*myPointerToObj).*myPointerToMemFn)(parm1, parm2); }
00273
00274 private:
00275 PointerToObj myPointerToObj;
00276 PointerToMemFn myPointerToMemFn;
00277 };
00278
00279
00280
00281
00282 template <typename R>
00283 class UT_Functor
00284 {
00285 public:
00286 typedef R ReturnType;
00287
00288 UT_Functor() {}
00289
00290
00291 explicit UT_Functor(UT_FunctorImpl<ReturnType> *functor_implementation)
00292 : myFunctorImpl(functor_implementation)
00293 {}
00294
00295
00296
00297
00298
00299
00300
00301 template <typename FunctorType>
00302 UT_Functor(FunctorType functor)
00303 : myFunctorImpl(new UT_FunctorHandler<UT_Functor, FunctorType>(functor))
00304 {}
00305
00306
00307
00308
00309 template <typename PointerToObj, typename PointerToMemFn>
00310 UT_Functor(const PointerToObj &pointer, PointerToMemFn method)
00311 : myFunctorImpl(new UT_MemFunHandler<UT_Functor, PointerToObj,
00312 PointerToMemFn>(pointer, method))
00313 {}
00314
00315
00316 ~UT_Functor()
00317 {
00318 delete myFunctorImpl.myFunctorImpl;
00319 myFunctorImpl.myFunctorImpl = 0;
00320 }
00321
00322
00323 UT_Functor &operator=(const UT_Functor &functor)
00324 {
00325 if (&functor != this)
00326 {
00327 delete myFunctorImpl.myFunctorImpl;
00328 myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
00329 ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
00330 }
00331 return *this;
00332 }
00333
00334
00335
00336
00337
00338 ReturnType operator()() const
00339 {
00340 UT_ASSERT(myFunctorImpl.myFunctorImpl);
00341 return (*myFunctorImpl.myFunctorImpl)();
00342 }
00343
00344
00345 bool isSet() const
00346 { return myFunctorImpl.myFunctorImpl != 0; }
00347
00348 private:
00349
00350
00351
00352 class Helper
00353 {
00354 public:
00355 Helper() : myFunctorImpl(0) {}
00356
00357
00358 Helper(const Helper &helper)
00359 { myFunctorImpl = helper.myFunctorImpl
00360 ? helper.myFunctorImpl->clone() : 0; }
00361
00362 explicit Helper(UT_FunctorImpl<ReturnType> *functor_implementation)
00363 : myFunctorImpl(functor_implementation) {}
00364
00365 template<typename U>
00366 explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
00367
00368 UT_FunctorImpl<ReturnType> *myFunctorImpl;
00369 };
00370
00371 Helper myFunctorImpl;
00372 };
00373
00374
00375
00376 template <typename R, typename P1>
00377 class UT_Functor1
00378 {
00379 public:
00380 typedef R ReturnType;
00381 typedef P1 Parm1Type;
00382
00383 UT_Functor1() {}
00384
00385 explicit UT_Functor1(
00386 UT_FunctorImpl1<ReturnType, Parm1Type> *functor_implementation)
00387 : myFunctorImpl(functor_implementation)
00388 {}
00389
00390 template <typename FunctorType>
00391 UT_Functor1(FunctorType functor)
00392 : myFunctorImpl(new UT_FunctorHandler1<UT_Functor1, FunctorType>(functor))
00393 {}
00394
00395 template <typename PointerToObj, typename PointerToMemFn>
00396 UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
00397 : myFunctorImpl(new UT_MemFunHandler1<UT_Functor1, PointerToObj,
00398 PointerToMemFn>(pointer, method))
00399 {}
00400
00401 ~UT_Functor1()
00402 {
00403 delete myFunctorImpl.myFunctorImpl;
00404 myFunctorImpl.myFunctorImpl = 0;
00405 }
00406
00407 UT_Functor1 &operator=(const UT_Functor1 &functor)
00408 {
00409 if (&functor != this)
00410 {
00411 delete myFunctorImpl.myFunctorImpl;
00412 myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
00413 ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
00414 }
00415 return *this;
00416 }
00417
00418 ReturnType operator()(Parm1Type parm1) const
00419 {
00420 UT_ASSERT(myFunctorImpl.myFunctorImpl);
00421 return (*myFunctorImpl.myFunctorImpl)(parm1);
00422 }
00423
00424 bool isSet() const
00425 { return myFunctorImpl.myFunctorImpl != 0; }
00426
00427 private:
00428 class Helper
00429 {
00430 public:
00431 Helper() : myFunctorImpl(0) {}
00432
00433
00434 Helper(const Helper &helper)
00435 { myFunctorImpl = helper.myFunctorImpl
00436 ? helper.myFunctorImpl->clone() : 0; }
00437
00438 explicit Helper(
00439 UT_FunctorImpl1<ReturnType, Parm1Type> *functor_implementation)
00440 : myFunctorImpl(functor_implementation) {}
00441
00442 template<typename U>
00443 explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
00444
00445 UT_FunctorImpl1<ReturnType, Parm1Type> *myFunctorImpl;
00446 };
00447
00448 Helper myFunctorImpl;
00449 };
00450
00451
00452
00453 template <typename R, typename P1, typename P2>
00454 class UT_Functor2
00455 {
00456 public:
00457 typedef R ReturnType;
00458 typedef P1 Parm1Type;
00459 typedef P2 Parm2Type;
00460
00461 UT_Functor2() {}
00462
00463 explicit UT_Functor2(
00464 UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *functor_impl)
00465 : myFunctorImpl(functor_impl)
00466 {}
00467
00468 template <typename FunctorType>
00469 UT_Functor2(FunctorType functor)
00470 : myFunctorImpl(new UT_FunctorHandler2<UT_Functor2, FunctorType>(functor))
00471 {}
00472
00473 template <typename PointerToObj, typename PointerToMemFn>
00474 UT_Functor2(const PointerToObj &pointer, PointerToMemFn method)
00475 : myFunctorImpl(new UT_MemFunHandler2<UT_Functor2, PointerToObj,
00476 PointerToMemFn>(pointer, method))
00477 {}
00478
00479 ~UT_Functor2()
00480 {
00481 delete myFunctorImpl.myFunctorImpl;
00482 myFunctorImpl.myFunctorImpl = 0;
00483 }
00484
00485 UT_Functor2 &operator=(const UT_Functor2 &functor)
00486 {
00487 if (&functor != this)
00488 {
00489 delete myFunctorImpl.myFunctorImpl;
00490 myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
00491 ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
00492 }
00493 return *this;
00494 }
00495
00496 ReturnType operator()(Parm1Type parm1, Parm2Type parm2) const
00497 {
00498 UT_ASSERT(myFunctorImpl.myFunctorImpl);
00499 return (*myFunctorImpl.myFunctorImpl)(parm1, parm2);
00500 }
00501
00502 bool isSet() const
00503 { return myFunctorImpl.myFunctorImpl != 0; }
00504
00505 private:
00506 class Helper
00507 {
00508 public:
00509 Helper() : myFunctorImpl(0) {}
00510
00511
00512 Helper(const Helper &helper)
00513 { myFunctorImpl = helper.myFunctorImpl
00514 ? helper.myFunctorImpl->clone() : 0; }
00515
00516 explicit Helper(
00517 UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *functor_impl)
00518 : myFunctorImpl(functor_impl) {}
00519
00520 template<typename U>
00521 explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
00522
00523 UT_FunctorImpl2<ReturnType, Parm1Type, Parm2Type> *myFunctorImpl;
00524 };
00525
00526 Helper myFunctorImpl;
00527 };
00528
00529
00530
00531
00532
00533 template <typename Incoming>
00534 class UT_BindFirstImpl : public UT_FunctorImpl<typename Incoming::ReturnType>
00535 {
00536 public:
00537 typedef typename Incoming::ReturnType ReturnType;
00538 typedef typename Incoming::Parm1Type BoundType;
00539
00540 UT_BindFirstImpl(const Incoming &functor, BoundType bound_argument)
00541 : myFunctor(functor), myBoundArgument(bound_argument)
00542 {}
00543
00544 UT_FunctorImpl<ReturnType> *clone() const
00545 { return new UT_BindFirstImpl(*this); }
00546
00547 ReturnType operator()() const
00548 { return myFunctor(myBoundArgument); }
00549
00550 private:
00551 Incoming myFunctor;
00552 BoundType myBoundArgument;
00553 };
00554
00555
00556
00557 template <typename Functor>
00558 UT_Functor<typename Functor::ReturnType>
00559 UT_BindFirst(const Functor &functor, typename Functor::Parm1Type bound_argument)
00560 {
00561
00562
00563 return UT_Functor<typename Functor::ReturnType>(
00564 (UT_FunctorImpl<typename Functor::ReturnType> *)
00565 new UT_BindFirstImpl<Functor>(functor, bound_argument));
00566 }
00567
00568
00569
00570 template <typename Incoming>
00571 class UT_BindFirstImpl1 : public UT_FunctorImpl1<typename Incoming::ReturnType,
00572 typename Incoming::Parm2Type>
00573 {
00574 public:
00575 typedef typename Incoming::ReturnType ReturnType;
00576 typedef typename Incoming::Parm2Type Parm1Type;
00577 typedef typename Incoming::Parm1Type BoundType;
00578
00579 UT_BindFirstImpl1(const Incoming &functor, BoundType bound_argument)
00580 : myFunctor(functor), myBoundArgument(bound_argument)
00581 {}
00582
00583 UT_FunctorImpl1<ReturnType, Parm1Type> *clone() const
00584 { return new UT_BindFirstImpl1(*this); }
00585
00586 ReturnType operator()(Parm1Type parm1) const
00587 { return myFunctor(myBoundArgument, parm1); }
00588
00589 private:
00590 Incoming myFunctor;
00591 BoundType myBoundArgument;
00592 };
00593
00594 template <typename Functor>
00595 UT_Functor1<typename Functor::ReturnType, typename Functor::Parm2Type>
00596 UT_BindFirst1(const Functor &functor,
00597 typename Functor::Parm1Type bound_argument)
00598 {
00599 typedef typename Functor::ReturnType ReturnType;
00600 typedef typename Functor::Parm2Type Parm1Type;
00601
00602
00603
00604 return UT_Functor1<ReturnType, Parm1Type>(
00605 (UT_FunctorImpl1<ReturnType, Parm1Type> *)
00606 new UT_BindFirstImpl1<Functor>(functor, bound_argument));
00607 }
00608
00609
00610
00611 #if defined(WIN32) || defined(LINUX) || defined(MBSD) \
00612 || defined(IRIX6) || defined(GAMEOS)
00613 #include "UT_Functor.C"
00614 #endif
00615
00616 #endif