/***********************************************************************
* This file contains support for director classes that proxy
* method calls from C++ to Ocaml extensions.
* Modified for Ocaml by : Art Yerkes
* Original Author : Mark Rose (mrose@stm.lbl.gov)
************************************************************************/
/* base class for director exceptions */
class DirectorException {
DirectorException(const char* msg="") {
const char *getMessage() const {
virtual ~DirectorException() {}
/* type mismatch in the return value from a python method call */
class DirectorTypeMismatchException : public Swig::DirectorException {
DirectorTypeMismatchException(const char* msg="") {
/* any python exception that occurs during a director method call */
class DirectorMethodException : public Swig::DirectorException {};
/* attempt 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 ocaml object */
/* flag indicating whether the object is owned by ocaml or c++ */
mutable bool swig_disown_flag;
/* 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_mutex_active = false;
pthread_mutex_unlock(&swig_mutex_up);
void swig_clear_up() const {
/* wrap a ocaml object, optionally taking ownership */
Director(CAML_VALUE self) : swig_self(self), swig_disown_flag(false), swig_up( false ) {
register_global_root(&swig_self);
/* discard our reference at destruction */
remove_global_root(&swig_self);
// Disown is safe here because we're just divorcing a reference that
/* return a pointer to the wrapped ocaml object */
CAML_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 ocaml object
bool swig_get_up( bool clear = true ) const {
if (Swig::Director::swig_mutex_active) {
if (pthread_equal(Swig::Director::swig_mutex_thread, pthread_self())) {
if( clear ) swig_clear_up();
bool swig_get_up( bool clear = true ) const {
if( clear ) swig_up = false;
/* set the swig_up flag if the next method call should be directed to
* the c++ base class rather than the wrapped ocaml 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;
void swig_set_up() const {
/* acquire ownership of the wrapped ocaml object (the sense of "disown"
void swig_disown() const {
callback(*caml_named_value("caml_obj_disown"),swig_self);
MUTEX_INIT(Swig::Director::swig_mutex_up);
pthread_t Swig::Director::swig_mutex_thread;
bool Swig::Director::swig_mutex_active = false;