summaryrefslogtreecommitdiff
path: root/Lib/perl5/director.swg
blob: f0a6614fc5c81889be72d4d38f75e30b97244cbc (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -----------------------------------------------------------------------------
 * director.swg
 *
 * This file contains support for director classes so that Perl proxy
 * methods can be called from C++.
 * ----------------------------------------------------------------------------- */

#ifndef SWIG_DIRECTOR_PERL_HEADER_
#define SWIG_DIRECTOR_PERL_HEADER_

#include <string>
#include <iostream>
#include <exception>
#include <vector>
#include <map>


/*
  Use -DSWIG_DIRECTOR_NORTTI if you prefer to avoid the use of the
  native C++ RTTI and dynamic_cast<>. But be aware that directors
  could stop working when using this option.
*/
#ifdef SWIG_DIRECTOR_NORTTI
/*
   When we don't use the native C++ RTTI, we implement a minimal one
   only for Directors.
*/
# ifndef SWIG_DIRECTOR_RTDIR
# define SWIG_DIRECTOR_RTDIR

namespace Swig {
  class Director;
  SWIGINTERN std::map<void *, Director *>& get_rtdir_map() {
    static std::map<void *, Director *> rtdir_map;
    return rtdir_map;
  }

  SWIGINTERNINLINE void set_rtdir(void *vptr, Director *rtdir) {
    get_rtdir_map()[vptr] = rtdir;
  }

  SWIGINTERNINLINE Director *get_rtdir(void *vptr) {
    std::map<void *, Director *>::const_iterator pos = get_rtdir_map().find(vptr);
    Director *rtdir = (pos != get_rtdir_map().end()) ? pos->second : 0;
    return rtdir;
  }
}
# endif /* SWIG_DIRECTOR_RTDIR */

# define SWIG_DIRECTOR_CAST(ARG) Swig::get_rtdir(static_cast<void *>(ARG))
# define SWIG_DIRECTOR_RGTR(ARG1, ARG2) Swig::set_rtdir(static_cast<void *>(ARG1), ARG2)

#else

# define SWIG_DIRECTOR_CAST(ARG) dynamic_cast<Swig::Director *>(ARG)
# define SWIG_DIRECTOR_RGTR(ARG1, ARG2)

#endif /* SWIG_DIRECTOR_NORTTI */

extern "C" {
  struct swig_type_info;
}

namespace Swig {

  /* memory handler */
  struct GCItem {
    virtual ~GCItem() {}

    virtual int get_own() const {
      return 0;
    }
  };

  struct GCItem_var {
    GCItem_var(GCItem *item = 0) : _item(item) {
    }

    GCItem_var& operator=(GCItem *item) {
      GCItem *tmp = _item;
      _item = item;
      delete tmp;
      return *this;
    }

    ~GCItem_var() {
      delete _item;
    }

    GCItem *operator->() const {
      return _item;
    }

  private:
    GCItem *_item;
  };

  struct GCItem_Object : GCItem {
    GCItem_Object(int own) : _own(own) {
    }

    virtual ~GCItem_Object() {
    }

    int get_own() const {
      return _own;
    }

  private:
    int _own;
  };

  template <typename Type>
  struct GCItem_T : GCItem {
    GCItem_T(Type *ptr) : _ptr(ptr) {
    }

    virtual ~GCItem_T() {
      delete _ptr;
    }

  private:
    Type *_ptr;
  };

  template <typename Type>
  struct GCArray_T : GCItem {
    GCArray_T(Type *ptr) : _ptr(ptr) {
    }

    virtual ~GCArray_T() {
      delete[] _ptr;
    }

  private:
    Type *_ptr;
  };

  /* base class for director exceptions */
  class DirectorException : public std::exception {
  public:
    virtual SV *getNative() const = 0;
  };

  /* exceptions emitted by Perl */
  class DirectorMethodException : public DirectorException {
  protected:
    SV *err;
  public:
    DirectorMethodException(SV *sv = sv_mortalcopy(ERRSV)) : err(sv) {
      SvREFCNT_inc(err);
    }

    const char *what() const throw() {
      return SvPV_nolen(err);
    }

    SV *getNative() const {
      return sv_2mortal(newSVsv(err));
    }

    static void raise(SV *sv) {
      throw DirectorMethodException(sv);
    }
  };

  /* exceptions emitted by wrap code */
  class DirectorWrapException : public DirectorException {
  protected:
    std::string msg;
    DirectorWrapException(const char *str) : msg(str) {
    }

  public:
    virtual ~DirectorWrapException() throw() {
    }

    const char *what() const throw() {
      return msg.c_str();
    }

    virtual SV *getNative() const {
      return sv_2mortal(newSVpvn(msg.data(), msg.size()));
    }
  };

  class DirectorTypeMismatchException : public DirectorWrapException {
  public:
    DirectorTypeMismatchException(const char *str) : DirectorWrapException(str) {
    }

    static void raise(const char *type, const char *msg) {
      std::string err = std::string(type);
      err += ": ";
      err += msg;
      throw DirectorTypeMismatchException(err.c_str());
    }
  };

  class DirectorPureVirtualException : public DirectorWrapException {
  public:
    DirectorPureVirtualException(const char *name)
      : DirectorWrapException("SWIG director pure virtual method called: ") {
      msg += name;
    }

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

  /* director base class */
  class Director {
  private:
    /* pointer to the wrapped perl object */
    SV *swig_self;
    /* class of wrapped perl object */
    std::string swig_class;
    /* flag indicating whether the object is owned by perl or c++ */
    mutable bool swig_disown_flag;

    /* decrement the reference count of the wrapped perl object */
    void swig_decref() const {
      if (swig_disown_flag) {
        SvREFCNT_dec(swig_self);
      }
    }

  public:
    /* wrap a Perl object. */
    Director(SV *pkg) : swig_disown_flag(false) {
      STRLEN len;
      char *str = SvPV(pkg, len);
      swig_class = std::string(str, len);
      swig_self = newRV_inc((SV *)newHV());
    }

    /* discard our reference at destruction */
    virtual ~Director() {
      swig_decref();
    }

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

    const char *swig_get_class() const {
      return swig_class.c_str();
    }

    /* acquire ownership of the wrapped Perl object (the sense of "disown" is from perl) */
    void swig_disown() const {
      if (!swig_disown_flag) {
        swig_disown_flag=true;
        swig_incref();
      }
    }

    /* increase the reference count of the wrapped Perl object */
    void swig_incref() const {
      if (swig_disown_flag) {
        SvREFCNT_inc(swig_self);
      }
    }

    /* methods to implement pseudo protected director members */
    virtual bool swig_get_inner(const char * /* swig_protected_method_name */) const {
      return true;
    }

    virtual void swig_set_inner(const char * /* swig_protected_method_name */, bool /* swig_val */) const {
    }

  /* ownership management */
  private:
    typedef std::map<void *, GCItem_var> swig_ownership_map;
    mutable swig_ownership_map swig_owner;

  public:
    template <typename Type>
    void swig_acquire_ownership_array(Type *vptr) const {
      if (vptr) {
        swig_owner[vptr] = new GCArray_T<Type>(vptr);
      }
    }

    template <typename Type>
    void swig_acquire_ownership(Type *vptr) const {
      if (vptr) {
        swig_owner[vptr] = new GCItem_T<Type>(vptr);
      }
    }

    void swig_acquire_ownership_obj(void *vptr, int own) const {
      if (vptr && own) {
        swig_owner[vptr] = new GCItem_Object(own);
      }
    }

    int swig_release_ownership(void *vptr) const {
      int own = 0;
      if (vptr) {
        swig_ownership_map::iterator iter = swig_owner.find(vptr);
        if (iter != swig_owner.end()) {
          own = iter->second->get_own();
          swig_owner.erase(iter);
        }
      }
      return own;
    }
  };

}

#endif