1/*
2 * Helper methods opaque-pointer tests (objc.test.test_opaque)
3 */
4#include "Python.h"
5#include "pyobjc-api.h"
6#include <stdarg.h>
7
8#import <Foundation/Foundation.h>
9
10typedef struct _Foo* FooHandle;
11typedef struct _Bar* BarHandle;
12
13@interface OC_OpaqueTest : NSObject
14{
15}
16+(FooHandle)createFoo:(int)value;
17+(FooHandle)nullFoo;
18+(void)deleteFoo:(FooHandle)handle;
19+(int)getValueOf:(FooHandle)foo;
20+(void)setValue:(int)value forFoo:(FooHandle)handle;
21
22
23+(BarHandle)createBarWithFirst:(double)first andSecond:(double)second;
24+(BarHandle)nullBar;
25+(void)getFirst:(double*)first andSecond:(double*)second of:(BarHandle)bar;
26+(void)setFirst:(double)first andSecond:(double)second of:(BarHandle)bar;
27+(void)deleteBar:(BarHandle)handle;
28+(double)getFirst:(BarHandle)handle;
29+(double)getSecond:(BarHandle)handle;
30@end
31
32
33static PyMethodDef mod_methods[] = {
34	        { 0, 0, 0, 0 }
35};
36
37void initopaque(void);
38void initopaque(void)
39{
40	PyObject* m;
41
42	m = Py_InitModule4("opaque", mod_methods, NULL, NULL, PYTHON_API_VERSION);
43
44	PyObjC_ImportAPI(m);
45
46	PyModule_AddObject(m, "OC_OpaqueTest",
47		PyObjCClass_New([OC_OpaqueTest class]));
48	PyModule_AddObject(m, "FooHandle",
49		PyObjCCreateOpaquePointerType("FooHandle",
50			                @encode(FooHandle), "FooHandle doc"));
51	PyModule_AddObject(m, "BarEncoded",  PyString_FromString(@encode(BarHandle)));
52}
53
54/*
55 * Only define the full structs here to ensure that @encode won't include
56 * the field definition into the encoded value.
57 */
58
59struct _Foo {
60	int index;
61};
62
63struct _Bar {
64	double first;
65	double second;
66};
67
68@implementation OC_OpaqueTest
69+(FooHandle)createFoo:(int)value
70{
71	FooHandle result = malloc(sizeof(struct _Foo));
72	if (result == NULL) {
73		return NULL;
74	}
75	result->index = value;
76	return result;
77}
78
79+(FooHandle)nullFoo
80{
81	return NULL;
82}
83
84+(void)deleteFoo:(FooHandle)handle
85{
86	if (handle) {
87		free(handle);
88	}
89}
90
91+(int)getValueOf:(FooHandle)foo
92{
93	return foo->index;
94}
95
96+(void)setValue:(int)value forFoo:(FooHandle)handle
97{
98	handle->index = value;
99}
100
101+(BarHandle)createBarWithFirst:(double)first andSecond:(double)second
102{
103	BarHandle result = malloc(sizeof(struct _Bar));
104	if (result == NULL) return NULL;
105
106	result->first = first;
107	result->second = second;
108	return result;
109}
110
111+(BarHandle)nullBar
112{
113	return NULL;
114}
115
116
117+(void)getFirst:(double*)first andSecond:(double*)second of:(BarHandle)bar
118{
119	*first = bar->first;
120	*second = bar->second;
121}
122
123+(void)setFirst:(double)first andSecond:(double)second of:(BarHandle)bar
124{
125	bar->first = first;
126	bar->second = second;
127}
128
129+(void)deleteBar:(BarHandle)handle
130{
131	if (handle) {
132		free(handle);
133	}
134}
135
136+(double)getFirst:(BarHandle)handle
137{
138	return handle->first;
139}
140
141+(double)getSecond:(BarHandle)handle
142{
143	return handle->second;
144}
145
146@end
147