summaryrefslogtreecommitdiff
path: root/Lib/ocaml/director.swg
blob: a21f621028f9a13507a9090cacbcc712acdbc57d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* -----------------------------------------------------------------------------
 * director.swg
 *
 * This file contains support for director classes that proxy
 * method calls from C++ to Ocaml extensions.
 *
 * ----------------------------------------------------------------------------- */

#ifdef __cplusplus

#include <string>

namespace Swig {
  /* base class for director exceptions */
  class DirectorException {
    protected:
      std::string swig_msg;
    public:
      DirectorException(const char* msg="") {
      }
      const char *getMessage() const { 
        return swig_msg.c_str(); 
      }
      virtual ~DirectorException() {}
  };

  /* type mismatch in the return value from a python method call */
  class DirectorTypeMismatchException : public Swig::DirectorException {
    public:
      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 {
    public:
      DirectorPureVirtualException(const char* msg="") {
      }

      static void raise(const char *msg) {
        throw DirectorPureVirtualException(msg);
      }
  };

  /* simple thread abstraction for pthreads on win32 */
#ifdef __THREAD__
#define __PTHREAD__
#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
#else
#include <pthread.h>
#define MUTEX_INIT(var) pthread_mutex_t var = PTHREAD_MUTEX_INITIALIZER 
#endif
#endif

  /* director base class */
  class Director {
    private:
      /* pointer to the wrapped ocaml object */
      CAML_VALUE swig_self;
      /* flag indicating whether the object is owned by ocaml or c++ */
      mutable bool swig_disown_flag;

    public:
      /* wrap a ocaml object, optionally taking ownership */
      Director(CAML_VALUE self) : swig_self(self), swig_disown_flag(false) {
        register_global_root(&swig_self);
      }

      /* discard our reference at destruction */
      virtual ~Director() {
        remove_global_root(&swig_self);
        swig_disown(); 
        // Disown is safe here because we're just divorcing a reference that
        // points to us.  
      }

      /* return a pointer to the wrapped ocaml object */
      CAML_VALUE swig_get_self() const { 
	  return swig_self;
      }

      /* acquire ownership of the wrapped ocaml object (the sense of "disown"
       * is from ocaml) */
      void swig_disown() const { 
        if (!swig_disown_flag) { 
          swig_disown_flag=true;
          callback(*caml_named_value("caml_obj_disown"),swig_self);
        } 
      }
  };
}

#endif /* __cplusplus */