~ubuntu-branches/ubuntu/maverick/swig1.3/maverick

« back to all changes in this revision

Viewing changes to Lib/python/director_h.swg

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-09-01 18:35:55 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050901183555-eq59uwhq8b62e44c
Tags: 1.3.24-1ubuntu4
* Use php5-dev instead of php4-dev, to kick php4 out of main.
* Drop support for generation of pike bindings, as nothing uses it,
  and swig is the only thing keeping pike7.6 in main (Ubuntu #13796)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
16
extern "C" {
 
17
  struct swig_type_info;
 
18
}
 
19
 
 
20
#ifdef SWIG_DIRECTOR_STATIC
 
21
/* Force anonymous (static) namespace */
 
22
#define Swig
 
23
#endif
 
24
 
 
25
namespace Swig {
 
26
  /* base class for director exceptions */
 
27
  class DirectorException {
 
28
    protected:
 
29
      std::string swig_msg;
 
30
    public:
 
31
      DirectorException(const char* msg="") {
 
32
      }
 
33
      const char *getMessage() const { 
 
34
        return swig_msg.c_str(); 
 
35
      }
 
36
    virtual ~DirectorException();
 
37
    
 
38
  };
 
39
 
 
40
  /* type mismatch in the return value from a python method call */
 
41
  class DirectorTypeMismatchException : public Swig::DirectorException {
 
42
    public:
 
43
      DirectorTypeMismatchException(const char* msg="") {
 
44
        swig_msg = "Swig director type mismatch: ";
 
45
        swig_msg += msg;
 
46
        PyErr_SetString(PyExc_TypeError, getMessage());
 
47
      }
 
48
  };
 
49
 
 
50
  /* any python exception that occurs during a director method call */
 
51
  class DirectorMethodException : public Swig::DirectorException {};
 
52
 
 
53
  /* attempt to call a pure virtual method via a director method */
 
54
  class DirectorPureVirtualException : public Swig::DirectorException {};
 
55
 
 
56
 
 
57
  /* simple thread abstraction for pthreads on win32 */
 
58
#ifdef __THREAD__
 
59
#define __PTHREAD__
 
60
#if defined(_WIN32) || defined(__WIN32__)
 
61
#define pthread_mutex_lock EnterCriticalSection
 
62
#define pthread_mutex_unlock LeaveCriticalSection
 
63
#define pthread_mutex_t CRITICAL_SECTION
 
64
#define MUTEX_INIT(var) CRITICAL_SECTION var
 
65
#else
 
66
#include <pthread.h>
 
67
#define MUTEX_INIT(var) pthread_mutex_t var = PTHREAD_MUTEX_INITIALIZER 
 
68
#endif
 
69
#endif
 
70
 
 
71
 
 
72
  /* director base class */
 
73
  class Director {
 
74
    private:
 
75
      /* pointer to the wrapped python object */
 
76
      PyObject* swig_self;
 
77
      /* flag indicating whether the object is owned by python or c++ */
 
78
      mutable bool swig_disown_flag;
 
79
      /* shared flag for breaking recursive director calls */
 
80
      static bool swig_up;
 
81
 
 
82
#ifdef __PTHREAD__
 
83
      /* locks for sharing the swig_up flag in a threaded environment */
 
84
      static pthread_mutex_t swig_mutex_up;
 
85
      static bool swig_mutex_active;
 
86
      static pthread_t swig_mutex_thread;
 
87
#endif
 
88
 
 
89
      /* decrement the reference count of the wrapped python object */
 
90
      void swig_decref() const { 
 
91
        if (swig_disown_flag) {
 
92
          Py_DECREF(swig_self); 
 
93
        }
 
94
      }
 
95
 
 
96
      /* reset the swig_up flag once the routing direction has been determined */
 
97
#ifdef __PTHREAD__
 
98
      void swig_clear_up() const { 
 
99
        Swig::Director::swig_up = false; 
 
100
        Swig::Director::swig_mutex_active = false;
 
101
        pthread_mutex_unlock(&swig_mutex_up);
 
102
      }
 
103
#else
 
104
      void swig_clear_up() const { 
 
105
        Swig::Director::swig_up = false; 
 
106
      }
 
107
#endif
 
108
 
 
109
    public:
 
110
      /* wrap a python object, optionally taking ownership */
 
111
      Director(PyObject* self) : swig_self(self), swig_disown_flag(false) {
 
112
        swig_incref();
 
113
      }
 
114
 
 
115
      /* discard our reference at destruction */
 
116
      virtual ~Director();
 
117
 
 
118
      /* return a pointer to the wrapped python object */
 
119
      PyObject *swig_get_self() const { 
 
120
        return swig_self; 
 
121
      }
 
122
 
 
123
      /* get the swig_up flag to determine if the method call should be routed
 
124
       * to the c++ base class or through the wrapped python object
 
125
       */
 
126
#ifdef __PTHREAD__
 
127
      bool swig_get_up() const { 
 
128
        if (Swig::Director::swig_mutex_active) {
 
129
          if (pthread_equal(Swig::Director::swig_mutex_thread, pthread_self())) {
 
130
            bool up = swig_up;
 
131
            swig_clear_up();
 
132
            return up;
 
133
          }
 
134
        }
 
135
        return 0;
 
136
      }
 
137
#else 
 
138
      bool swig_get_up() const { 
 
139
        bool up = swig_up;
 
140
        swig_up = false;
 
141
        return up;
 
142
      }
 
143
#endif
 
144
 
 
145
      /* set the swig_up flag if the next method call should be directed to
 
146
       * the c++ base class rather than the wrapped python object
 
147
       */
 
148
#ifdef __PTHREAD__
 
149
      void swig_set_up() const { 
 
150
        pthread_mutex_lock(&Swig::Director::swig_mutex_up);
 
151
        Swig::Director::swig_mutex_thread = pthread_self();
 
152
        Swig::Director::swig_mutex_active = true;
 
153
        Swig::Director::swig_up = true; 
 
154
      }
 
155
#else 
 
156
      void swig_set_up() const { 
 
157
        Swig::Director::swig_up = true; 
 
158
      }
 
159
#endif
 
160
 
 
161
      /* acquire ownership of the wrapped python object (the sense of "disown"
 
162
       * is from python) */
 
163
      void swig_disown() const { 
 
164
        if (!swig_disown_flag) { 
 
165
          swig_disown_flag=true;
 
166
          swig_incref(); 
 
167
        } 
 
168
      }
 
169
 
 
170
      /* increase the reference count of the wrapped python object */
 
171
      void swig_incref() const { 
 
172
        if (swig_disown_flag) {
 
173
          Py_INCREF(swig_self); 
 
174
        }
 
175
      }
 
176
 
 
177
      /* methods to implement pseudo protected director members */
 
178
      virtual bool swig_get_inner(const char* name) const {
 
179
        return true;
 
180
      }
 
181
 
 
182
      virtual void swig_set_inner(const char* name, bool val) const {
 
183
      }
 
184
  };
 
185
 
 
186
}
 
187
 
 
188
#endif /* __cplusplus */
 
189
 
 
190
 
 
191
#endif