1/* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4 *
5 * cpointer.i
6 *
7 * SWIG library file containing macros that can be used to manipulate simple
8 * pointer objects.
9 * ----------------------------------------------------------------------------- */
10
11/* -----------------------------------------------------------------------------
12 * %pointer_class(type,name)
13 *
14 * Places a simple proxy around a simple type like 'int', 'float', or whatever.
15 * The proxy provides this interface:
16 *
17 *       class type {
18 *       public:
19 *           type();
20 *          ~type();
21 *           type value();
22 *           void assign(type value);
23 *       };
24 *
25 * Example:
26 *
27 *    %pointer_class(int, intp);
28 *
29 *    int add(int *x, int *y) { return *x + *y; }
30 *
31 * In python (with proxies)
32 *
33 *    >>> a = intp()
34 *    >>> a.assign(10)
35 *    >>> a.value()
36 *    10
37 *    >>> b = intp()
38 *    >>> b.assign(20)
39 *    >>> print add(a,b)
40 *    30
41 *
42 * As a general rule, this macro should not be used on class/structures that
43 * are already defined in the interface.
44 * ----------------------------------------------------------------------------- */
45
46
47%define %pointer_class(TYPE, NAME)
48%{
49typedef TYPE NAME;
50%}
51
52typedef struct {
53} NAME;
54
55%extend NAME {
56#ifdef __cplusplus
57NAME() {
58  return new TYPE();
59}
60~NAME() {
61  if (self) delete self;
62}
63#else
64NAME() {
65  return (TYPE *) calloc(1,sizeof(TYPE));
66}
67~NAME() {
68  if (self) free(self);
69}
70#endif
71}
72
73%extend NAME {
74
75void assign(TYPE value) {
76  *self = value;
77}
78TYPE value() {
79  return *self;
80}
81TYPE * cast() {
82  return self;
83}
84static NAME * frompointer(TYPE *t) {
85  return (NAME *) t;
86}
87
88}
89
90%types(NAME = TYPE);
91
92%enddef
93
94/* -----------------------------------------------------------------------------
95 * %pointer_functions(type,name)
96 *
97 * Create functions for allocating/deallocating pointers.   This can be used
98 * if you don't want to create a proxy class or if the pointer is complex.
99 *
100 *    %pointer_functions(int, intp)
101 *
102 *    int add(int *x, int *y) { return *x + *y; }
103 *
104 * In python (with proxies)
105 *
106 *    >>> a = copy_intp(10)
107 *    >>> intp_value(a)
108 *    10
109 *    >>> b = new_intp()
110 *    >>> intp_assign(b,20)
111 *    >>> print add(a,b)
112 *    30
113 *    >>> delete_intp(a)
114 *    >>> delete_intp(b)
115 *
116 * ----------------------------------------------------------------------------- */
117
118%define %pointer_functions(TYPE,NAME)
119%{
120static TYPE *new_##NAME() { %}
121#ifdef __cplusplus
122%{  return new TYPE(); %}
123#else
124%{  return (TYPE *) calloc(1,sizeof(TYPE)); %}
125#endif
126%{}
127
128static TYPE *copy_##NAME(TYPE value) { %}
129#ifdef __cplusplus
130%{  return new TYPE(value); %}
131#else
132%{  TYPE *self = (TYPE *) calloc(1,sizeof(TYPE));
133  *self = value;
134  return self; %}
135#endif
136%{}
137
138static void delete_##NAME(TYPE *self) { %}
139#ifdef __cplusplus
140%{  if (self) delete self; %}
141#else
142%{  if (self) free(self); %}
143#endif
144%{}
145
146static void NAME ##_assign(TYPE *self, TYPE value) {
147  *self = value;
148}
149
150static TYPE NAME ##_value(TYPE *self) {
151  return *self;
152}
153%}
154
155TYPE *new_##NAME();
156TYPE *copy_##NAME(TYPE value);
157void  delete_##NAME(TYPE *self);
158void  NAME##_assign(TYPE *self, TYPE value);
159TYPE  NAME##_value(TYPE *self);
160
161%enddef
162
163/* -----------------------------------------------------------------------------
164 * %pointer_cast(type1,type2,name)
165 *
166 * Generates a pointer casting function.
167 * ----------------------------------------------------------------------------- */
168
169%define %pointer_cast(TYPE1,TYPE2,NAME)
170%inline %{
171TYPE2 NAME(TYPE1 x) {
172   return (TYPE2) x;
173}
174%}
175%enddef
176
177
178
179
180
181
182
183
184