00001 #ifndef __CALLBACK_H_INCLUDED__
00002 #define __CALLBACK_H_INCLUDED__
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 namespace Monitor {
00042 namespace Utility {
00043
00047 class CallbackBase
00048 {
00049 public:
00053 CallbackBase() {;}
00054
00058 virtual ~CallbackBase() {;}
00059
00060
00066 virtual CallbackBase*
00067 clone() const = 0;
00068
00075 int
00076 operator==(const CallbackBase& other) const { return isEqual(other); }
00077
00084 virtual int
00085 isEqual(const CallbackBase& other) const { return (&other == this); }
00086
00092 virtual void
00093 invoke(const void* invocationData = 0) const = 0;
00094
00100 virtual int
00101 set() const = 0;
00102
00108 virtual int
00109 isTarget(const void* testTarget) const = 0;
00110 };
00111
00112
00113
00117 class FuncCallback : public CallbackBase
00118 {
00119 public:
00123 typedef void (*CallbackType)();
00124
00125
00129 FuncCallback(CallbackType newTargetFunction)
00130 :
00131 CallbackBase(),
00132 targetFunction(newTargetFunction)
00133 {;}
00134
00138 FuncCallback(const FuncCallback& other)
00139 :
00140 CallbackBase(),
00141 targetFunction(other.targetFunction)
00142 {;}
00143
00147 virtual ~FuncCallback() {;}
00148
00154 virtual CallbackBase*
00155 clone() const { return new FuncCallback(*this); }
00156
00162 int
00163 operator==(const FuncCallback& other) const
00164 {
00165 return (targetFunction == other.targetFunction);
00166 }
00167
00173 virtual int
00174 isEqual(const CallbackBase& other) const
00175 {
00176 return (*this == dynamic_cast< const FuncCallback& >(other));
00177 }
00178
00184 virtual int
00185 isTarget(const void*) const
00186 {
00187 return 0;
00188 }
00189
00194 virtual void
00195 invoke(const void*) const { (*targetFunction)(); }
00196
00202 virtual int
00203 set() const { return (0 != targetFunction); }
00204
00205 protected:
00206 CallbackType targetFunction;
00207 };
00208
00209
00210
00214 template <class TargetClass>
00215 class ClassCallback : public CallbackBase
00216 {
00217 public:
00221 ClassCallback(TargetClass& newTargetObject)
00222 :
00223 CallbackBase(),
00224 targetObject(newTargetObject)
00225 {;}
00226
00230 ClassCallback(const ClassCallback& other)
00231 :
00232 CallbackBase(),
00233 targetObject(other.targetObject)
00234 {;}
00235
00239 virtual ~ClassCallback() {;}
00240
00246 int
00247 operator==(const ClassCallback& other) const
00248 {
00249 return (&targetObject == &other.targetObject);
00250 }
00251
00257 virtual int
00258 isTarget(const void* testTarget) const
00259 {
00260 return (testTarget == (const void*)&targetObject);
00261 }
00262
00263 protected:
00267 TargetClass& targetObject;
00268 };
00269
00270
00271
00276 template <class TargetClass>
00277 class Callback : public ClassCallback< TargetClass >
00278 {
00279 public:
00283 typedef void (TargetClass::*CallbackType)();
00284
00285
00289 Callback(TargetClass& newTargetObject,
00290 CallbackType newTargetFunction)
00291 :
00292 ClassCallback< TargetClass >(newTargetObject),
00293 targetFunction(newTargetFunction)
00294 {;}
00295
00299 Callback(const Callback& other)
00300 :
00301 ClassCallback< TargetClass >(other),
00302 targetFunction(other.targetFunction)
00303 {;}
00304
00308 virtual ~Callback() {;}
00309
00315 virtual CallbackBase*
00316 clone() const { return new Callback(*this); }
00317
00323 int
00324 operator==(const Callback& other) const
00325 {
00326 return ((ClassCallback< TargetClass >::operator==(other)) &&
00327 (targetFunction == other.targetFunction));
00328 }
00329
00335 virtual int
00336 isEqual(const CallbackBase& other) const
00337 {
00338 return (*this == dynamic_cast< const Callback& >(other));
00339 }
00340
00345 virtual void
00346 invoke(const void*) const { (targetObject.*targetFunction)(); }
00347
00353 virtual int
00354 set() const { return (0 != targetFunction); }
00355
00356 protected:
00357 CallbackType targetFunction;
00358 };
00359
00360
00361
00365 template <class TargetClass>
00366 class ConstCallback : public Callback< TargetClass >
00367 {
00368 public:
00372 typedef void (TargetClass::*CallbackType)() const;
00373
00374
00378 ConstCallback(
00379 const TargetClass& newTargetObject,
00380 CallbackType newTargetFunction)
00381 :
00382 Callback< TargetClass >(
00383 const_cast< TargetClass& >(newTargetObject),
00384 reinterpret_cast< Callback< TargetClass >::CallbackType >(
00385 newTargetFunction))
00386 {;}
00387
00391 ConstCallback(const ConstCallback& other)
00392 :
00393 Callback< TargetClass >(other)
00394 {;}
00395
00399 virtual ~ConstCallback() {;}
00400
00406 virtual CallbackBase*
00407 clone() const { return new ConstCallback(*this); }
00408
00414 int
00415 operator==(const ConstCallback& other) const
00416 {
00417 return Callback< TargetClass >::operator==(other);
00418 }
00419
00424 virtual void
00425 invoke(const void*) const
00426 {
00427 (targetObject.*reinterpret_cast< CallbackType >(targetFunction))();
00428 }
00429 };
00430
00431
00432
00437 template <class TargetClass, typename TargetData>
00438 class DataCallback : public Callback< TargetClass >
00439 {
00440 public:
00444 typedef void (TargetClass::*CallbackType)(TargetData);
00445
00446
00450 DataCallback(
00451 TargetClass& newTargetObject,
00452 CallbackType newTargetFunction,
00453 const TargetData& newTargetData)
00454 :
00455 Callback< TargetClass >(
00456 newTargetObject,
00457 reinterpret_cast< Callback< TargetClass >::CallbackType >(
00458 newTargetFunction)),
00459 targetData(newTargetData)
00460 {;}
00461
00465 DataCallback(const DataCallback& other)
00466 :
00467 Callback< TargetClass >(other),
00468 targetData(other.targetData)
00469 {;}
00470
00474 virtual ~DataCallback() {;}
00475
00481 virtual CallbackBase*
00482 clone() const { return new DataCallback(*this); }
00483
00489 int
00490 operator==(const DataCallback& other) const
00491 {
00492 return (Callback< TargetClass >::operator==(other) &&
00493 (targetData == other.targetData));
00494 }
00495
00496 virtual int
00497 isEqual(const CallbackBase& other) const
00498 {
00499 return (*this == dynamic_cast< const DataCallback& >(other));
00500 }
00501
00506 virtual void
00507 invoke(const void*) const
00508 {
00509 (targetObject.*reinterpret_cast< CallbackType >(targetFunction))(
00510 targetData);
00511 }
00512
00513 protected:
00514 TargetData targetData;
00515 };
00516
00517
00518
00523 template <class TargetClass, typename TargetData >
00524 class DataConstCallback : public DataCallback< const TargetClass, TargetData >
00525 {
00526 public:
00527 typedef DataCallback< const TargetClass, TargetData > ParentClass;
00531 typedef void (TargetClass::*CallbackType)(TargetData) const;
00532
00533
00537 DataConstCallback(
00538 TargetClass& newTargetObject,
00539 CallbackType newTargetFunction,
00540 const TargetData& newTargetData)
00541 :
00542 ParentClass(
00543 const_cast< const TargetClass& >(newTargetObject),
00544 reinterpret_cast< Callback< TargetClass >::CallbackType >(
00545 newTargetFunction),
00546 newTargetData)
00547 {;}
00548
00552 DataConstCallback(const ParentClass& other)
00553 :
00554 Callback< const TargetClass >(other)
00555 {;}
00556
00560 virtual ~DataConstCallback() {;}
00561
00567 virtual CallbackBase*
00568 clone() const { return new DataConstCallback(*this); }
00569
00575 int
00576 operator==(const DataConstCallback& other) const
00577 {
00578 return ParentClass::operator==(other);
00579 }
00580 };
00581
00582
00583
00588 template <class TargetClass, typename TargetInfo >
00589 class InfoCallback : public Callback< TargetClass >
00590 {
00591 public:
00595 typedef void (TargetClass::*CallbackType)(const TargetInfo*);
00596
00597
00601 InfoCallback(
00602 TargetClass& newTargetObject,
00603 CallbackType newTargetFunction)
00604 :
00605 Callback< TargetClass >(
00606 newTargetObject,
00607 reinterpret_cast< Callback< TargetClass >::CallbackType >(
00608 newTargetFunction))
00609 {;}
00610
00614 InfoCallback(const InfoCallback& other)
00615 :
00616 Callback< TargetClass >(other)
00617 {;}
00618
00622 virtual ~InfoCallback() {;}
00623
00629 virtual CallbackBase*
00630 clone() const { return new InfoCallback(*this); }
00631
00637 int
00638 operator==(const InfoCallback& other) const
00639 {
00640 return Callback< TargetClass >::operator==(other);
00641 }
00642
00649 virtual void
00650 invoke(const void* targetInfo) const
00651 {
00652 (targetObject.*reinterpret_cast< CallbackType >(targetFunction))(
00653 static_cast< const TargetInfo* >(targetInfo));
00654 }
00655 };
00656
00657 };
00658 };
00659
00660 #endif // __CALLBACK_H_INCLUDED__