| 1 | #ifndef SWIG_DIRECTOR_PYTHON_HEADER_ |
| 2 | #define SWIG_DIRECTOR_PYTHON_HEADER_ |
| 3 | /*********************************************************************** |
| 4 | * director_h.swg |
| 5 | * |
| 6 | * This file contains support for director classes that proxy |
| 7 | * method calls from C++ to Python extensions. |
| 8 | * |
| 9 | * Author : Mark Rose (mrose@stm.lbl.gov) |
| 10 | ************************************************************************/ |
| 11 | |
| 12 | #ifdef __cplusplus |
| 13 | |
| 14 | #include <string> |
| 15 | #include <iostream> |
| 16 | #include <exception> |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | Use -DSWIG_DIRECTOR_NOUEH if you prefer to avoid the use of the |
| 21 | Undefined Exception Handler provided by swift |
| 22 | */ |
| 23 | #ifndef SWIG_DIRECTOR_NOUEH |
| 24 | #ifndef SWIG_DIRECTOR_UEH |
| 25 | #define SWIG_DIRECTOR_UEH |
| 26 | #endif |
| 27 | #endif |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | Use -DSWIG_DIRECTOR_STATIC if you prefer to avoid the use of the |
| 32 | 'Swig' namespace. This could be usefull for multi-modules projects. |
| 33 | */ |
| 34 | #ifdef SWIG_DIRECTOR_STATIC |
| 35 | /* Force anonymous (static) namespace */ |
| 36 | #define Swig |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | Use -DSWIG_DIRECTOR_NORTTI if you prefer to avoid the use of the |
| 42 | native C++ RTTI and dynamic_cast<>. But be aware that directors |
| 43 | could stop working when using this option. |
| 44 | */ |
| 45 | #ifdef SWIG_DIRECTOR_NORTTI |
| 46 | /* |
| 47 | When we don't use the native C++ RTTI, we implement a minimal one |
| 48 | only for Directors. |
| 49 | */ |
| 50 | # ifndef SWIG_DIRECTOR_RTDIR |
| 51 | # define SWIG_DIRECTOR_RTDIR |
| 52 | #include <map> |
| 53 | namespace Swig { |
| 54 | class Director; |
| 55 | SWIGINTERN std::map<void*,Director*>& get_rtdir_map() { |
| 56 | static std::map<void*,Director*> rtdir_map; |
| 57 | return rtdir_map; |
| 58 | } |
| 59 | |
| 60 | SWIGINTERNINLINE void set_rtdir(void *vptr, Director *rtdir) { |
| 61 | get_rtdir_map()[vptr] = rtdir; |
| 62 | } |
| 63 | |
| 64 | SWIGINTERNINLINE Director *get_rtdir(void *vptr) { |
| 65 | std::map<void*,Director*>::const_iterator pos = get_rtdir_map().find(vptr); |
| 66 | Director *rtdir = (pos != get_rtdir_map().end()) ? pos->second : 0; |
| 67 | return rtdir; |
| 68 | } |
| 69 | } |
| 70 | # endif /* SWIG_DIRECTOR_RTDIR */ |
| 71 | |
| 72 | # define SWIG_DIRECTOR_CAST(Arg) Swig::get_rtdir(static_cast<void*>(Arg)) |
| 73 | # define SWIG_DIRECTOR_RGTR(Arg1, Arg2) Swig::set_rtdir(static_cast<void*>(Arg1), Arg2) |
| 74 | |
| 75 | #else |
| 76 | |
| 77 | # define SWIG_DIRECTOR_CAST(Arg) dynamic_cast<Swig::Director*>(Arg) |
| 78 | # define SWIG_DIRECTOR_RGTR(Arg1, Arg2) |
| 79 | |
| 80 | #endif /* SWIG_DIRECTOR_NORTTI */ |
| 81 | |
| 82 | extern "C" { |
| 83 | struct swig_type_info; |
| 84 | } |
| 85 | |
| 86 | namespace Swig { |
| 87 | |
| 88 | /* base class for director exceptions */ |
| 89 | class DirectorException { |
| 90 | protected: |
| 91 | std::string swig_msg; |
| 92 | public: |
| 93 | DirectorException(const char* hdr ="", const char* msg ="") |
| 94 | : swig_msg(hdr) { |
| 95 | swig_msg += msg; |
| 96 | if (!PyErr_Occurred()) { |
| 97 | PyErr_SetString(PyExc_TypeError, getMessage()); |
| 98 | } else { |
| 99 | SWIG_Python_AddErrMesg(getMessage(), 1); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | const char *getMessage() const { |
| 104 | return swig_msg.c_str(); |
| 105 | } |
| 106 | |
| 107 | static void raise(const char* msg = "") |
| 108 | { |
| 109 | throw DirectorException(msg); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | class UnknownExceptionHandler |
| 114 | { |
| 115 | static void handler(); |
| 116 | |
| 117 | public: |
| 118 | |
| 119 | #ifdef SWIG_DIRECTOR_UEH |
| 120 | std::unexpected_handler old; |
| 121 | UnknownExceptionHandler(std::unexpected_handler nh = handler) |
| 122 | { |
| 123 | old = std::set_unexpected(nh); |
| 124 | } |
| 125 | |
| 126 | ~UnknownExceptionHandler() |
| 127 | { |
| 128 | std::set_unexpected(old); |
| 129 | } |
| 130 | #endif |
| 131 | }; |
| 132 | |
| 133 | /* type mismatch in the return value from a python method call */ |
| 134 | class DirectorTypeMismatchException : public Swig::DirectorException { |
| 135 | public: |
| 136 | DirectorTypeMismatchException(const char* msg="") |
| 137 | : Swig::DirectorException("Swig director type mismatch: ", msg) { |
| 138 | } |
| 139 | |
| 140 | static void raise(const char* msg = "") |
| 141 | { |
| 142 | throw DirectorTypeMismatchException(msg); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | /* any python exception that occurs during a director method call */ |
| 147 | class DirectorMethodException : public Swig::DirectorException { |
| 148 | public: |
| 149 | DirectorMethodException(const char* msg = "") |
| 150 | : DirectorException("Swig director python method error: ", msg) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | static void raise(const char* msg = "") |
| 155 | { |
| 156 | throw DirectorMethodException(msg); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | /* attempt to call a pure virtual method via a director method */ |
| 161 | class DirectorPureVirtualException : public Swig::DirectorException |
| 162 | { |
| 163 | public: |
| 164 | DirectorPureVirtualException(const char* msg = "") |
| 165 | : DirectorException("Swig director pure virtal method called: ", msg) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | static void raise(const char* msg = "") |
| 170 | { |
| 171 | throw DirectorPureVirtualException(msg); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | /* simple thread abstraction for pthreads on win32 */ |
| 177 | #ifdef __THREAD__ |
| 178 | #define __PTHREAD__ |
| 179 | #if defined(_WIN32) || defined(__WIN32__) |
| 180 | #define pthread_mutex_lock EnterCriticalSection |
| 181 | #define pthread_mutex_unlock LeaveCriticalSection |
| 182 | #define pthread_mutex_t CRITICAL_SECTION |
| 183 | #define MUTEX_INIT(var) CRITICAL_SECTION var |
| 184 | #else |
| 185 | #include <pthread.h> |
| 186 | #define MUTEX_INIT(var) pthread_mutex_t var = PTHREAD_MUTEX_INITIALIZER |
| 187 | #endif |
| 188 | #endif |
| 189 | |
| 190 | |
| 191 | /* director base class */ |
| 192 | class Director { |
| 193 | private: |
| 194 | /* pointer to the wrapped python object */ |
| 195 | PyObject* swig_self; |
| 196 | /* flag indicating whether the object is owned by python or c++ */ |
| 197 | mutable bool swig_disown_flag; |
| 198 | /* shared flag for breaking recursive director calls */ |
| 199 | static bool swig_up; |
| 200 | |
| 201 | #ifdef __PTHREAD__ |
| 202 | /* locks for sharing the swig_up flag in a threaded environment */ |
| 203 | static pthread_mutex_t swig_mutex_up; |
| 204 | static bool swig_mutex_active; |
| 205 | static pthread_t swig_mutex_thread; |
| 206 | #endif |
| 207 | |
| 208 | /* decrement the reference count of the wrapped python object */ |
| 209 | void swig_decref() const { |
| 210 | if (swig_disown_flag) { |
| 211 | Py_DECREF(swig_self); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* reset the swig_up flag once the routing direction has been determined */ |
| 216 | #ifdef __PTHREAD__ |
| 217 | void swig_clear_up() const { |
| 218 | Swig::Director::swig_up = false; |
| 219 | Swig::Director::swig_mutex_active = false; |
| 220 | pthread_mutex_unlock(&swig_mutex_up); |
| 221 | } |
| 222 | #else |
| 223 | void swig_clear_up() const { |
| 224 | Swig::Director::swig_up = false; |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | public: |
| 229 | /* wrap a python object, optionally taking ownership */ |
| 230 | Director(PyObject* self) : swig_self(self), swig_disown_flag(false) { |
| 231 | swig_incref(); |
| 232 | } |
| 233 | |
| 234 | /* discard our reference at destruction */ |
| 235 | virtual ~Director(); |
| 236 | |
| 237 | /* return a pointer to the wrapped python object */ |
| 238 | PyObject *swig_get_self() const { |
| 239 | return swig_self; |
| 240 | } |
| 241 | |
| 242 | /* get the swig_up flag to determine if the method call should be routed |
| 243 | * to the c++ base class or through the wrapped python object |
| 244 | */ |
| 245 | #ifdef __PTHREAD__ |
| 246 | bool swig_get_up() const { |
| 247 | if (Swig::Director::swig_mutex_active) { |
| 248 | if (pthread_equal(Swig::Director::swig_mutex_thread, pthread_self())) { |
| 249 | bool up = swig_up; |
| 250 | swig_clear_up(); |
| 251 | return up; |
| 252 | } |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | #else |
| 257 | bool swig_get_up() const { |
| 258 | bool up = swig_up; |
| 259 | swig_up = false; |
| 260 | return up; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | /* set the swig_up flag if the next method call should be directed to |
| 265 | * the c++ base class rather than the wrapped python object |
| 266 | */ |
| 267 | #ifdef __PTHREAD__ |
| 268 | void swig_set_up() const { |
| 269 | pthread_mutex_lock(&Swig::Director::swig_mutex_up); |
| 270 | Swig::Director::swig_mutex_thread = pthread_self(); |
| 271 | Swig::Director::swig_mutex_active = true; |
| 272 | Swig::Director::swig_up = true; |
| 273 | } |
| 274 | #else |
| 275 | void swig_set_up() const { |
| 276 | Swig::Director::swig_up = true; |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | /* acquire ownership of the wrapped python object (the sense of "disown" |
| 281 | * is from python) */ |
| 282 | void swig_disown() const { |
| 283 | if (!swig_disown_flag) { |
| 284 | swig_disown_flag=true; |
| 285 | swig_incref(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* increase the reference count of the wrapped python object */ |
| 290 | void swig_incref() const { |
| 291 | if (swig_disown_flag) { |
| 292 | Py_INCREF(swig_self); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* methods to implement pseudo protected director members */ |
| 297 | virtual bool swig_get_inner(const char* /* name */) const { |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | virtual void swig_set_inner(const char* /* name */, bool /* val */) const { |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | #endif /* __cplusplus */ |
| 308 | |
| 309 | |
| 310 | #endif |