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