/***********************************************************************
* This file contains support for director classes that proxy
* method calls from C++ to Ruby extensions.
* Author : Lyle Johnson (lyle@users.sourceforge.net)
* Based on the original Python implementation by
* Mark Rose (mrose@stm.lbl.gov).
************************************************************************/
/* Base class for director exceptions */
class DirectorException {
DirectorException(VALUE error=Qnil) : swig_error(error) {}
return CLASS_OF(swig_error);
virtual ~DirectorException() {}
/* Type mismatch in the return value from a Ruby method call */
class DirectorTypeMismatchException : public Swig::DirectorException {
DirectorTypeMismatchException(const char *msg="") {
VALUE str = rb_str_new2("Swig director type mismatch: ");
rb_str_concat(str, rb_str_new2(msg));
swig_error = rb_exc_new3(rb_eTypeError, str);
/* Any Ruby exception that occurs during a director method call */
class DirectorMethodException : public Swig::DirectorException {
DirectorMethodException(VALUE error) : Swig::DirectorException(error) {}
/* Attempted to call a pure virtual method via a director method */
class DirectorPureVirtualException : public Swig::DirectorException {};
/* Simple thread abstraction for pthreads on win32 */
#if defined(_WIN32) || defined(__WIN32__)
#define pthread_mutex_lock EnterCriticalSection
#define pthread_mutex_unlock LeaveCriticalSection
#define pthread_mutex_t CRITICAL_SECTION
#define MUTEX_INIT(var) CRITICAL_SECTION var
#define MUTEX_INIT(var) pthread_mutex_t var = PTHREAD_MUTEX_INITIALIZER
/* director base class */
/* pointer to the wrapped Ruby object */
/* flag indicating whether the object is owned by Ruby or c++ */
mutable bool swig_disown_flag;
/* shared flag for breaking recursive director calls */
/* locks for sharing the swig_up flag in a threaded environment */
static pthread_mutex_t swig_mutex_up;
static bool swig_mutex_active;
static pthread_t swig_mutex_thread;
/* reset the swig_up flag once the routing direction has been determined */
void swig_clear_up() const {
Swig::Director::swig_up = false;
Swig::Director::swig_mutex_active = false;
pthread_mutex_unlock(&swig_mutex_up);
void swig_clear_up() const {
Swig::Director::swig_up = false;
/* wrap a Ruby object, optionally taking ownership */
Director(VALUE self) : swig_self(self), swig_disown_flag(false) {
/* discard our reference at destruction */
/* return a pointer to the wrapped Ruby object */
VALUE swig_get_self() const {
/* get the swig_up flag to determine if the method call should be routed
* to the c++ base class or through the wrapped Ruby object
bool swig_get_up() const {
if (Swig::Director::swig_mutex_active) {
if (pthread_equal(Swig::Director::swig_mutex_thread, pthread_self())) {
bool swig_get_up() const {
/* set the swig_up flag if the next method call should be directed to
* the c++ base class rather than the wrapped Ruby object
void swig_set_up() const {
pthread_mutex_lock(&Swig::Director::swig_mutex_up);
Swig::Director::swig_mutex_thread = pthread_self();
Swig::Director::swig_mutex_active = true;
Swig::Director::swig_up = true;
void swig_set_up() const {
Swig::Director::swig_up = true;
/* acquire ownership of the wrapped Ruby object (the sense of "disown"
void swig_disown() const {
bool Swig::Director::swig_up = false;
MUTEX_INIT(Swig::Director::swig_mutex_up);
pthread_t Swig::Director::swig_mutex_thread;
bool Swig::Director::swig_mutex_active = false;