comparison lib/swig/swigwin-2.0.11/Lib/cpointer.i @ 1899:b3009adc0e2f

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