1/*
2 *  This file defines support for "toll-free briging" between Python wrappers
3 *  for CoreFoundation objecs and native Objective-C objects.
4 */
5#include "pyobjc.h"
6
7#include <unistd.h>
8
9#include "objc/objc.h"
10
11#import <Foundation/NSURL.h>
12
13#if 0 && !defined(__OBJC2__) && (PY_MAJOR_VERSION == 2)
14#include "pymactoolbox.h"
15#endif
16
17id
18PyObjC_CFTypeToID(PyObject* argument)
19{
20	/* Tollfree bridging of CF (some) objects  */
21	id  val;
22
23	if (PyObjCObject_Check(argument)) {
24		val = PyObjCObject_GetObject(argument);
25		return val;
26
27	}
28
29#if 0
30//#if !defined(__OBJC2__) && (PY_VERSION_HEX < 0x03000000)
31//#endif
32//#if PY_MAJOR_VERSION == 2
33	int r;
34
35	/* Fall back to MacPython CFType support: */
36	r = CFObj_Convert(argument, (CFTypeRef*)&val);
37	if (r) return val;
38	PyErr_Clear();
39#endif
40
41	return NULL;
42}
43
44/*
45 * NOTE: CFObj_New creates a CF wrapper for any CF object, however we have
46 * better information for at least some types: it is impossible to see the
47 * difference between mutable and immutable types using the CF API.
48 *
49 */
50PyObject*
51PyObjC_IDToCFType(id argument __attribute__((__unused__)))
52{
53
54#if 0 && !defined(__OBJC2__) && (PY_MAJOR_VERSION == 2)
55	CFTypeRef typeRef = (CFTypeRef)argument;
56	CFTypeID typeID = CFGetTypeID(argument);
57
58    /*
59     * This function has a net reference count of 0 as the CF wrapper
60     * does not retain, but will do a CFRelease when the Python proxy
61     * goes away.
62     */
63	CFRetain(typeRef);
64
65	/* This could be more efficient, could cache... */
66	if (typeID == CFStringGetTypeID()) {
67		return CFMutableStringRefObj_New((CFMutableStringRef)argument);
68	} else if (typeID == CFArrayGetTypeID()) {
69		return CFMutableArrayRefObj_New((CFMutableArrayRef)argument);
70	} else if (typeID == CFDictionaryGetTypeID()) {
71		return CFMutableDictionaryRefObj_New((CFMutableDictionaryRef)argument);
72	} else if (typeID == CFURLGetTypeID()) {
73		return CFURLRefObj_New((CFURLRef)argument);
74#if PY_VERSION_HEX >= 0x02050000
75	} else if (typeID == CFDataGetTypeID()) {
76		return CFMutableDataRefObj_New((CFMutableDataRef)argument);
77#endif
78	}
79	return CFObj_New((CFTypeRef)argument);
80#endif
81
82	PyErr_SetString(PyExc_NotImplementedError, "jucky macpython");
83	return NULL;
84}
85