1/*
2 * Manual wrappers for CFBag
3 */
4#include <Python.h>
5#include "pyobjc-api.h"
6
7#import <CoreFoundation/CoreFoundation.h>
8
9static PyObject*
10mod_CFBagGetValues(
11	PyObject* self __attribute__((__unused__)),
12	PyObject* args)
13{
14	PyObject* py_bag;
15	CFBagRef bag;
16
17
18	if (!PyArg_ParseTuple(args, "O", &py_bag)) {
19		return NULL;
20	}
21
22	if (PyObjC_PythonToObjC(@encode(CFBagRef), py_bag, &bag) < 0) {
23		return NULL;
24	}
25
26	CFIndex count = CFBagGetCount(bag);
27	NSObject** members = malloc(sizeof(NSObject*) * count);
28	if (members == NULL) {
29		PyErr_NoMemory();
30		return NULL;
31	}
32	memset(members, 0, sizeof(NSObject*) * count);
33
34	CFBagGetValues(bag, (const void**)members);
35	PyObject* result = PyObjC_CArrayToPython(@encode(NSObject*), members, (Py_ssize_t)count);
36	free(members);
37	return result;
38}
39
40
41static PyObject*
42mod_CFBagCreate(PyObject* self __attribute__((__unused__)),
43	PyObject* args)
44{
45	PyObject* py_allocator;
46	PyObject* py_members;
47	Py_ssize_t count;
48	CFAllocatorRef allocator;
49	void** members;
50	int r;
51	PyObject* buf = NULL;
52	CFBagRef bag;
53
54
55	if (!PyArg_ParseTuple(args, "OO" Py_ARG_SIZE_T, &py_allocator, &py_members, &count)) {
56		return NULL;
57	}
58
59	if (PyObjC_PythonToObjC(@encode(CFAllocatorRef), py_allocator, &allocator) < 0) {
60		return NULL;
61	}
62
63	r = PyObjC_PythonToCArray(NO, NO, @encode(NSObject*), py_members, (void**)&members, &count, &buf);
64	if (r == -1) {
65		return NULL;
66	}
67
68	bag = CFBagCreate(allocator, (const void**)members, (CFIndex)count, &kCFTypeBagCallBacks);
69
70	PyObjC_FreeCArray(r, members);
71	Py_XDECREF(buf);
72
73	PyObject* result = PyObjC_ObjCToPython(@encode(CFBagRef), &bag);
74	if (bag) {
75		CFRelease(bag);
76	}
77	return result;
78}
79
80static PyObject*
81mod_CFBagCreateMutable(PyObject* self __attribute__((__unused__)),
82	PyObject* args)
83{
84	PyObject* py_allocator;
85	Py_ssize_t count;
86	CFAllocatorRef allocator;
87	CFBagRef bag;
88
89
90	if (!PyArg_ParseTuple(args, "O" Py_ARG_SIZE_T, &py_allocator, &count)) {
91		return NULL;
92	}
93
94	if (PyObjC_PythonToObjC(@encode(CFAllocatorRef), py_allocator, &allocator) < 0) {
95		return NULL;
96	}
97
98	bag = CFBagCreateMutable(allocator, count, &kCFTypeBagCallBacks);
99
100	PyObject* result = PyObjC_ObjCToPython(@encode(CFBagRef), &bag);
101	if (bag) {
102		CFRelease(bag);
103	}
104	return result;
105}
106
107static PyMethodDef mod_methods[] = {
108        {
109		"CFBagCreate",
110		(PyCFunction)mod_CFBagCreate,
111		METH_VARARGS,
112		NULL
113	},
114        {
115		"CFBagCreateMutable",
116		(PyCFunction)mod_CFBagCreateMutable,
117		METH_VARARGS,
118		NULL
119	},
120        {
121		"CFBagGetValues",
122		(PyCFunction)mod_CFBagGetValues,
123		METH_VARARGS,
124		NULL
125	},
126	{ 0, 0, 0, 0 } /* sentinel */
127};
128
129void init_CFBag(void);
130void init_CFBag(void)
131{
132	PyObject* m = Py_InitModule4("_CFBag", mod_methods, "", NULL,
133		PYTHON_API_VERSION);
134
135	PyObjC_ImportAPI(m);
136}
137
138