00001 ///////////////////////////////////////////////////////////////////////////////////////// 00002 /// \file callback.h 00003 /// \brief Declaration of the classes and typedefs used to implement the callback 00004 /// functions in the atomic classes. 00005 /// 00006 /// Types for the argument variables passed to the non-static callbacks: 00007 /// \li class SPARK::TArgument 00008 /// \li class SPARK::TArguments 00009 /// \li typedef SPARK::ArgList 00010 /// 00011 /// Types for the target variables passed to the modifier callbacks: 00012 /// \li class SPARK::TTarget 00013 /// \li class SPARK::TTargets 00014 /// \li typedef SPARK::TargetList 00015 /// 00016 /// Callback wrapper classes: 00017 /// \li template<typename FreeFuncPtrType> class SPARK::TFreeFunctionWrapper 00018 /// \li class SPARK::TModifierCallback 00019 /// \li class SPARK::TNonModifierCallback 00020 /// \li class SPARK::TPredicateCallback 00021 /// \li class SPARK::TStaticNonModifierCallback 00022 /// \li class SPARK::TStaticPredicateCallback 00023 /// 00024 ///////////////////////////////////////////////////////////////////////////////////////// 00025 /// \attention 00026 /// PORTIONS COPYRIGHT (C) 2003 AYRES SOWELL ASSOCIATES, INC. \n 00027 /// PORTIONS COPYRIGHT (C) 2003 THE REGENTS OF THE UNIVERSITY OF CALIFORNIA . 00028 /// PENDING APPROVAL BY THE US DEPARTMENT OF ENERGY. ALL RIGHTS RESERVED. 00029 /// 00030 ///////////////////////////////////////////////////////////////////////////////////////// 00031 /// \author Dimitri Curtil (LBNL/SRG) 00032 /// \date December 20, 2002 00033 ///////////////////////////////////////////////////////////////////////////////////////// 00034 00035 00036 00037 // The debugger can't handle symbols more than 255 characters long. 00038 // STL often creates symbols longer than that. 00039 // When symbols are longer than 255 characters, the warning is disabled. 00040 #pragma warning(disable:4786) 00041 00042 #if !defined(__CALLBACK_H__) 00043 #define __CALLBACK_H__ 00044 00045 #include <iostream> 00046 #include <string> 00047 00048 #include "container.h" 00049 00050 #include "variable.h" // for class SPARK::Variable::TInterface, class TVariable 00051 #include "types.h" // for ReturnTypes, VariableTypes, CallbackTypes 00052 #include "compat.h" // for CLASS_DECLSPEC 00053 00054 00055 ///////////////////////////////////////////////////////////////////////////////////////////// 00056 namespace SPARK { 00057 00058 00059 ///////////////////////////////////////////////////////////////////////////////////////// 00060 // Forward declaration 00061 ///////////////////////////////////////////////////////////////////////////////////////// 00062 class TInverse; 00063 class TObject; 00064 00065 00066 ///////////////////////////////////////////////////////////////////////////////////////// 00067 /// \class TArgument 00068 /// \brief This class acts as a read-only interface to a TVariable object. 00069 /// It is used only in the callbacks to describe the argument variables. 00070 /// This class is not used internally in the solver. 00071 /// It provides proper value access behavior depending on the type of the 00072 /// variable and the calling context. 00073 /// Also, it implements some of the attribute access methods from TVariable. 00074 class CLASS_DECLSPEC TArgument : public SPARK::Variable::TInterface { 00075 public: 00076 ///////////////////////////////////////////////////////////////////////////////////// 00077 /// \name Structors 00078 //@{ 00079 /// Default constructor 00080 TArgument() 00081 : base_type() 00082 {} 00083 /// Normal constructor using a pointer to a TVariable object 00084 explicit 00085 TArgument(SPARK::TVariable* variable) 00086 : base_type(variable) 00087 {} 00088 /// Copy constructor 00089 TArgument(const TArgument& argument) 00090 : base_type(argument) 00091 {} 00092 //@} 00093 ///////////////////////////////////////////////////////////////////////////////////// 00094 00095 00096 ///////////////////////////////////////////////////////////////////////////////////// 00097 /// \name Numerical access methods 00098 //@{ 00099 /// Returns the inner value for innerStep 00100 double operator()(unsigned innerStep) const { return GetInnerValue(innerStep); } 00101 /// Allows implicit conversion to double by returning the current value 00102 operator double() const { return GetArgument(); } 00103 //@} 00104 ///////////////////////////////////////////////////////////////////////////////////// 00105 00106 }; 00107 ///////////////////////////////////////////////////////////////////////////////////////// 00108 00109 00110 00111 ///////////////////////////////////////////////////////////////////////////////////////// 00112 /// \class TTarget 00113 /// \brief This class acts as a write-only interface to a TVariable object. 00114 /// It is used only in the callbacks to describe the target variables. 00115 /// This class is not used internally in the solver. 00116 /// It provides proper value access behavior depending on the type of the 00117 /// variable and the calling context. 00118 /// Also, it implements some of the attribute access methods from TVariable. 00119 class CLASS_DECLSPEC TTarget : public SPARK::Variable::TInterface { 00120 public: 00121 ///////////////////////////////////////////////////////////////////////////////////// 00122 /// \name Structors 00123 //@{ 00124 /// Default constructor 00125 TTarget() 00126 : base_type() 00127 {} 00128 /// Normal constructor using a pointer to a TVariable object 00129 explicit 00130 TTarget(SPARK::TVariable* variable) 00131 : base_type(variable) 00132 {} 00133 /// Copy constructor 00134 TTarget(const TTarget& target) 00135 : base_type(target) 00136 {} 00137 //@} 00138 ///////////////////////////////////////////////////////////////////////////////////// 00139 00140 00141 ///////////////////////////////////////////////////////////////////////////////////// 00142 /// \name Numerical access methods 00143 /// 00144 /// The overloaded arithmetic operators operate on double only and are all unary 00145 /// operators. 00146 //@{ 00147 TTarget& operator=(double scalar) { SetTarget(scalar); return *this; } 00148 TTarget& operator+=(double scalar) { SetTarget( GetArgument()+scalar ); return *this; } 00149 TTarget& operator-=(double scalar) { SetTarget( GetArgument()-scalar ); return *this; } 00150 TTarget& operator*=(double scalar) { SetTarget( GetArgument()*scalar ); return *this; } 00151 TTarget& operator/=(double scalar) { SetTarget( GetArgument()/scalar ); return *this; } 00152 //@} 00153 ///////////////////////////////////////////////////////////////////////////////////// 00154 00155 }; 00156 ///////////////////////////////////////////////////////////////////////////////////////// 00157 00158 00159 00160 ///////////////////////////////////////////////////////////////////////////////////////// 00161 // Type definition used in the prototype of the various callbacks 00162 ///////////////////////////////////////////////////////////////////////////////////////// 00163 00164 /// Type for collection of TArgument objects 00165 typedef SPARK::container< SPARK::TArgument > TArguments; 00166 00167 /// Type of argument list passed to the non-static callback functions 00168 typedef const TArguments& ArgList; 00169 00170 /// Type for collection of TTarget objects 00171 typedef SPARK::container< SPARK::TTarget > TTargets; 00172 00173 /// Type of target list passed to the the modifier callback functions 00174 typedef TTargets& TargetList; 00175 00176 00177 00178 ///////////////////////////////////////////////////////////////////////////////////////// 00179 /// \class TFreeFunctionWrapper 00180 /// \brief Wrapper class for any free function that is invoked as an atomic class callback 00181 template<typename FreeFuncPtrType> 00182 class TFreeFunctionWrapper { 00183 public: 00184 //////////////////////////////////////////////////////////////////////////////////// 00185 // Type definition 00186 //////////////////////////////////////////////////////////////////////////////////// 00187 typedef FreeFuncPtrType function_type; 00188 typedef TFreeFunctionWrapper<function_type> base_type; 00189 00190 00191 //////////////////////////////////////////////////////////////////////////////////// 00192 // Structors 00193 //////////////////////////////////////////////////////////////////////////////////// 00194 00195 /// Default constructor 00196 TFreeFunctionWrapper() 00197 : Name("NOT DEFINED"), FreeFunction(0) 00198 {} 00199 /// Copy constructor 00200 TFreeFunctionWrapper(const TFreeFunctionWrapper& w) 00201 : Name(w.Name), FreeFunction(w.FreeFunction) 00202 {} 00203 /// Constructor 00204 TFreeFunctionWrapper(const char* name, FreeFuncPtrType t) // FreeFuncPtrType is a pointer to the function! 00205 : Name(name), FreeFunction(t) 00206 {} 00207 /// Destructor 00208 ~TFreeFunctionWrapper() SPARK_NOT_THROWING 00209 {} 00210 00211 /// Assignment operator 00212 TFreeFunctionWrapper& operator=(const TFreeFunctionWrapper& w) 00213 { 00214 Name = w.Name; 00215 FreeFunction = w.FreeFunction; 00216 return *this; 00217 } 00218 00219 00220 //////////////////////////////////////////////////////////////////////////////////// 00221 // Access methods 00222 //////////////////////////////////////////////////////////////////////////////////// 00223 00224 /// Returns name of the callback function as const char* 00225 const char* GetName() const { return Name.c_str(); } 00226 00227 00228 //////////////////////////////////////////////////////////////////////////////////// 00229 // Predicate method 00230 //////////////////////////////////////////////////////////////////////////////////// 00231 00232 /// Returns true if wrapper contains a valid callback function 00233 operator bool() const { return (FreeFunction!=0); } 00234 00235 00236 protected: 00237 std::string Name; 00238 FreeFuncPtrType FreeFunction; 00239 00240 }; 00241 ///////////////////////////////////////////////////////////////////////////////////////// 00242 00243 00244 00245 ///////////////////////////////////////////////////////////////////////////////////////// 00246 // Type declarations for each callback type 00247 ///////////////////////////////////////////////////////////////////////////////////////// 00248 00249 /// Function prototype for modifier callbacks 00250 typedef void (*TModifierFunction) (TObject* , ArgList , TargetList ); 00251 00252 /// Function prototype for non-modifier callbacks 00253 typedef void (*TNonModifierFunction) (TObject* , ArgList ); 00254 00255 /// Function prototype for predicate callbacks 00256 typedef bool (*TPredicateFunction) (TObject* , ArgList ); 00257 00258 /// Function prototype for static callbacks 00259 typedef void (*TStaticNonModifierFunction) (TInverse* ); 00260 00261 /// Function prototype for predicate callbacks 00262 typedef bool (*TStaticPredicateFunction) (TInverse* ); 00263 00264 00265 00266 ///////////////////////////////////////////////////////////////////////////////////////// 00267 /// \class TEnumPolicy 00268 /// \brief Policy class that specifies a enum value of type EnumType 00269 template<typename EnumType> 00270 struct TEnumPolicy { 00271 typedef TEnumPolicy<EnumType> enum_policiy_type; 00272 typedef EnumType enum_type; 00273 00274 TEnumPolicy(EnumType type) 00275 : Type(type) 00276 {} 00277 TEnumPolicy(const TEnumPolicy& p) 00278 : Type(p.Type) 00279 {} 00280 ~TEnumPolicy() SPARK_NOT_THROWING 00281 {} 00282 00283 enum_type Type; 00284 00285 }; 00286 ///////////////////////////////////////////////////////////////////////////////////////// 00287 00288 00289 ///////////////////////////////////////////////////////////////////////////////////////// 00290 /// \class TModifierCallback 00291 /// \brief Function wrapper class for modifier callbacks 00292 class TModifierCallback : 00293 public TFreeFunctionWrapper<TModifierFunction>, 00294 public TEnumPolicy<CallbackTypes> 00295 { 00296 public: 00297 typedef TEnumPolicy<CallbackTypes> enum_policy_type; 00298 00299 00300 //////////////////////////////////////////////////////////////////////////////////// 00301 // Structors 00302 //////////////////////////////////////////////////////////////////////////////////// 00303 00304 /// Constructor for a non-defined callback 00305 TModifierCallback(enum_policiy_type::enum_type code) 00306 : base_type(), enum_policiy_type(code), ReturnType( ReturnType_NONE ) 00307 {} 00308 /// Constructor for a defined callback 00309 TModifierCallback(enum_policiy_type::enum_type code, const char* name, function_type ptr, ReturnTypes returnType) 00310 : base_type(name, ptr), enum_policiy_type(code), ReturnType( returnType ) 00311 {} 00312 /// Copy constructor 00313 TModifierCallback(const TModifierCallback& c) 00314 : base_type(c), enum_policy_type(c), ReturnType(c.ReturnType) 00315 {} 00316 00317 /// Assignment operator 00318 TModifierCallback& operator=(const TModifierCallback& c) 00319 { 00320 base_type::operator=(c); 00321 ReturnType = c.ReturnType; 00322 return *this; 00323 } 00324 00325 00326 //////////////////////////////////////////////////////////////////////////////////// 00327 // Modifiers 00328 //////////////////////////////////////////////////////////////////////////////////// 00329 00330 /// \brief Fires the underlying C function that implements the modifier callback 00331 /// 00332 /// \param object is a pointer to the TObject instance the non-modifier callback belongs to 00333 /// \param args is the list of argument variable as defined in the FUNCTIONS {} statement of the atomic class 00334 /// \param targets is the list of target variable as defined in the FUNCTIONS {} statement of the atomic class 00335 inline void operator()(TObject* object, ArgList args, TargetList targets) const 00336 { 00337 FreeFunction(object, args, targets); 00338 } 00339 00340 00341 //////////////////////////////////////////////////////////////////////////////////// 00342 // Access methods 00343 //////////////////////////////////////////////////////////////////////////////////// 00344 00345 /// Returns the return type for the modifier callback as ReturnTypes 00346 ReturnTypes GetReturnType() const { return ReturnType; } 00347 00348 00349 private: 00350 ReturnTypes ReturnType; 00351 }; 00352 ///////////////////////////////////////////////////////////////////////////////////////// 00353 00354 00355 ///////////////////////////////////////////////////////////////////////////////////////// 00356 /// \class TNonModifierCallback 00357 /// \brief Function wrapper class for non-modifier callbacks 00358 class TNonModifierCallback : 00359 public TFreeFunctionWrapper<TNonModifierFunction>, 00360 public TEnumPolicy<CallbackTypes> 00361 { 00362 public: 00363 //////////////////////////////////////////////////////////////////////////////////// 00364 // Structors 00365 //////////////////////////////////////////////////////////////////////////////////// 00366 00367 /// Constructor for a non-defined callback 00368 TNonModifierCallback(enum_policiy_type::enum_type code) 00369 : base_type(), enum_policiy_type(code) 00370 {} 00371 /// Constructor for a defined callback 00372 TNonModifierCallback(enum_policiy_type::enum_type code, const char* name, function_type ptr) 00373 : base_type(name, ptr), enum_policiy_type(code) 00374 {} 00375 00376 00377 //////////////////////////////////////////////////////////////////////////////////// 00378 // Modifiers 00379 //////////////////////////////////////////////////////////////////////////////////// 00380 00381 /// \brief Fires the underlying C function that implements the non-modifier callback 00382 /// 00383 /// \param object is a pointer to the TObject instance the non-modifier callback belongs to 00384 /// \param args is the list of arguments as defined in the FUNCTIONS {} statement of the atomic class 00385 inline void operator()(TObject* object, ArgList args) const 00386 { 00387 FreeFunction(object, args); 00388 } 00389 }; 00390 ///////////////////////////////////////////////////////////////////////////////////////// 00391 00392 00393 ///////////////////////////////////////////////////////////////////////////////////////// 00394 /// \class TPredicateCallback 00395 /// \brief Function wrapper class for predicate callbacks 00396 class TPredicateCallback : 00397 public TFreeFunctionWrapper<TPredicateFunction>, 00398 public TEnumPolicy<CallbackTypes> 00399 { 00400 public: 00401 //////////////////////////////////////////////////////////////////////////////////// 00402 // Structors 00403 //////////////////////////////////////////////////////////////////////////////////// 00404 00405 /// Constructor for a non-defined callback 00406 TPredicateCallback(enum_policiy_type::enum_type code) 00407 : base_type(), enum_policiy_type(code) 00408 {} 00409 /// Constructor for a defined callback 00410 TPredicateCallback(enum_policiy_type::enum_type code, const char* name, function_type ptr) 00411 : base_type(name, ptr), enum_policiy_type(code) 00412 {} 00413 00414 00415 //////////////////////////////////////////////////////////////////////////////////// 00416 // Modifiers 00417 //////////////////////////////////////////////////////////////////////////////////// 00418 00419 /// \brief Fires the underlying C function that implements the predicate callback 00420 /// 00421 /// \param object is a pointer to the TObject instance the non-modifier callback belongs to 00422 /// \param args is the list of arguments as defined in the <code>FUNCTIONS {} </code> 00423 /// statement of the atomic class 00424 inline bool operator()(TObject* object, ArgList args) const 00425 { 00426 return FreeFunction(object, args); 00427 } 00428 }; 00429 ///////////////////////////////////////////////////////////////////////////////////////// 00430 00431 00432 ///////////////////////////////////////////////////////////////////////////////////////// 00433 /// \class TStaticNonModifierCallback 00434 /// \brief Function wrapper class for static non-modifier callbacks 00435 class TStaticNonModifierCallback : 00436 public TFreeFunctionWrapper<TStaticNonModifierFunction>, 00437 public TEnumPolicy<CallbackTypes> 00438 { 00439 public: 00440 //////////////////////////////////////////////////////////////////////////////////// 00441 // Structors 00442 //////////////////////////////////////////////////////////////////////////////////// 00443 00444 /// Constructor for a non-defined callback 00445 TStaticNonModifierCallback(enum_policiy_type::enum_type code) 00446 : base_type(), enum_policiy_type(code) 00447 {} 00448 /// Constructor for a defined callback 00449 TStaticNonModifierCallback(enum_policiy_type::enum_type code, const char* name, function_type ptr) 00450 : base_type(name, ptr), enum_policiy_type(code) 00451 {} 00452 00453 00454 //////////////////////////////////////////////////////////////////////////////////// 00455 // Modifiers 00456 //////////////////////////////////////////////////////////////////////////////////// 00457 00458 /// \brief Fires the underlying C function that implements the static callback 00459 /// 00460 /// \param inverse is a pointer to the TInverse instance the static callback belongs to 00461 inline void operator()(TInverse* inverse) const 00462 { 00463 FreeFunction( inverse ); 00464 } 00465 }; 00466 ///////////////////////////////////////////////////////////////////////////////////////// 00467 00468 00469 ///////////////////////////////////////////////////////////////////////////////////////// 00470 /// \class TStaticPredicateCallback 00471 /// \brief Function wrapper class for static predicate callbacks 00472 class TStaticPredicateCallback : 00473 public TFreeFunctionWrapper<TStaticPredicateFunction>, 00474 public TEnumPolicy<CallbackTypes> 00475 { 00476 public: 00477 //////////////////////////////////////////////////////////////////////////////////// 00478 // Structors 00479 //////////////////////////////////////////////////////////////////////////////////// 00480 00481 /// Constructor for a non-defined callback 00482 TStaticPredicateCallback(enum_policiy_type::enum_type code) 00483 : base_type(), enum_policiy_type(code) 00484 {} 00485 /// Constructor for a defined callback 00486 TStaticPredicateCallback(enum_policiy_type::enum_type code, const char* name, function_type ptr) 00487 : base_type(name, ptr), enum_policiy_type(code) 00488 {} 00489 00490 00491 //////////////////////////////////////////////////////////////////////////////////// 00492 // Modifiers 00493 //////////////////////////////////////////////////////////////////////////////////// 00494 00495 /// \brief Fires the underlying C function that implements the static predicate callback 00496 /// 00497 /// \param inverse is a pointer to the TInverse instance the static predicate callback 00498 /// belongs to 00499 inline bool operator()(TInverse* inverse) const 00500 { 00501 return FreeFunction( inverse ); 00502 } 00503 }; 00504 ///////////////////////////////////////////////////////////////////////////////////////// 00505 00506 00507 /// Output operator << overload for the TArgument class 00508 API_DECLSPEC(std::ostream&) operator<<(std::ostream& os, const SPARK::TArgument& argument); 00509 00510 /// Output operator << overload for the TTarget class 00511 API_DECLSPEC(std::ostream&) operator<<(std::ostream& os, const SPARK::TTarget& target); 00512 00513 00514 }; // namespace SPARK 00515 ///////////////////////////////////////////////////////////////////////////////////////////// 00516 00517 00518 00519 #endif //__CALLBACK_H__ 00520 00521