]> gitweb.factorcode.org Git - factor.git/blob - basis/core-foundation/core-foundation.factor
Create basis vocab root
[factor.git] / basis / core-foundation / core-foundation.factor
1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings alien.syntax kernel
4 math sequences io.encodings.utf16 destructors accessors ;
5 IN: core-foundation
6
7 TYPEDEF: void* CFAllocatorRef
8 TYPEDEF: void* CFArrayRef
9 TYPEDEF: void* CFDataRef
10 TYPEDEF: void* CFDictionaryRef
11 TYPEDEF: void* CFMutableDictionaryRef
12 TYPEDEF: void* CFNumberRef
13 TYPEDEF: void* CFBundleRef
14 TYPEDEF: void* CFSetRef
15 TYPEDEF: void* CFStringRef
16 TYPEDEF: void* CFURLRef
17 TYPEDEF: void* CFUUIDRef
18 TYPEDEF: void* CFTypeRef
19 TYPEDEF: bool Boolean
20 TYPEDEF: int CFIndex
21 TYPEDEF: int SInt32
22 TYPEDEF: uint UInt32
23 TYPEDEF: uint CFTypeID
24 TYPEDEF: double CFTimeInterval
25 TYPEDEF: double CFAbsoluteTime
26
27 TYPEDEF: int CFNumberType
28 : kCFNumberSInt8Type 1 ; inline
29 : kCFNumberSInt16Type 2 ; inline
30 : kCFNumberSInt32Type 3 ; inline
31 : kCFNumberSInt64Type 4 ; inline
32 : kCFNumberFloat32Type 5 ; inline
33 : kCFNumberFloat64Type 6 ; inline
34 : kCFNumberCharType 7 ; inline
35 : kCFNumberShortType 8 ; inline
36 : kCFNumberIntType 9 ; inline
37 : kCFNumberLongType 10 ; inline
38 : kCFNumberLongLongType 11 ; inline
39 : kCFNumberFloatType 12 ; inline
40 : kCFNumberDoubleType 13 ; inline
41 : kCFNumberCFIndexType 14 ; inline
42 : kCFNumberNSIntegerType 15 ; inline
43 : kCFNumberCGFloatType 16 ; inline
44 : kCFNumberMaxType 16 ; inline
45
46 TYPEDEF: int CFPropertyListMutabilityOptions
47 : kCFPropertyListImmutable                  0 ; inline
48 : kCFPropertyListMutableContainers          1 ; inline
49 : kCFPropertyListMutableContainersAndLeaves 2 ; inline
50
51 FUNCTION: CFArrayRef CFArrayCreateMutable ( CFAllocatorRef allocator, CFIndex capacity, void* callbacks ) ;
52
53 FUNCTION: void* CFArrayGetValueAtIndex ( CFArrayRef array, CFIndex idx ) ;
54
55 FUNCTION: void CFArraySetValueAtIndex ( CFArrayRef array, CFIndex index, void* value ) ;
56
57 FUNCTION: CFIndex CFArrayGetCount ( CFArrayRef array ) ;
58
59 : kCFURLPOSIXPathStyle 0 ; inline
60 : kCFAllocatorDefault f ; inline
61
62 FUNCTION: CFURLRef CFURLCreateWithFileSystemPath ( CFAllocatorRef allocator, CFStringRef filePath, int pathStyle, Boolean isDirectory ) ;
63
64 FUNCTION: CFURLRef CFURLCreateWithString ( CFAllocatorRef allocator, CFStringRef string, CFURLRef base ) ;
65
66 FUNCTION: CFURLRef CFURLCopyFileSystemPath ( CFURLRef url, int pathStyle ) ;
67
68 FUNCTION: CFStringRef CFStringCreateWithCharacters ( CFAllocatorRef allocator, wchar_t* cStr, CFIndex numChars ) ;
69
70 FUNCTION: CFIndex CFStringGetLength ( CFStringRef theString ) ;
71
72 FUNCTION: void CFStringGetCharacters ( void* theString, CFIndex start, CFIndex length, void* buffer ) ;
73
74 FUNCTION: CFNumberRef CFNumberCreate ( CFAllocatorRef allocator, CFNumberType theType, void* valuePtr ) ;
75
76 FUNCTION: CFDataRef CFDataCreate ( CFAllocatorRef allocator, uchar* bytes, CFIndex length ) ;
77
78 FUNCTION: CFBundleRef CFBundleCreate ( CFAllocatorRef allocator, CFURLRef bundleURL ) ;
79
80 FUNCTION: Boolean CFBundleLoadExecutable ( CFBundleRef bundle ) ;
81
82 FUNCTION: CFTypeRef CFRetain ( CFTypeRef cf ) ;
83 FUNCTION: void CFRelease ( CFTypeRef cf ) ;
84
85 FUNCTION: CFTypeID CFGetTypeID ( CFTypeRef cf ) ;
86
87 : CF>array ( alien -- array )
88     dup CFArrayGetCount [ CFArrayGetValueAtIndex ] with map ;
89
90 : <CFArray> ( seq -- alien )
91     [ f swap length f CFArrayCreateMutable ] keep
92     [ length ] keep
93     [ >r dupd r> CFArraySetValueAtIndex ] 2each ;
94
95 : <CFString> ( string -- alien )
96     f swap dup length CFStringCreateWithCharacters ;
97
98 : CF>string ( alien -- string )
99     dup CFStringGetLength 1+ "ushort" <c-array> [
100         >r 0 over CFStringGetLength r> CFStringGetCharacters
101     ] keep utf16n alien>string ;
102
103 : CF>string-array ( alien -- seq )
104     CF>array [ CF>string ] map ;
105
106 : <CFStringArray> ( seq -- alien )
107     [ <CFString> ] map dup <CFArray> swap [ CFRelease ] each ;
108
109 : <CFFileSystemURL> ( string dir? -- url )
110     >r <CFString> f over kCFURLPOSIXPathStyle
111     r> CFURLCreateWithFileSystemPath swap CFRelease ;
112
113 : <CFURL> ( string -- url )
114     <CFString>
115     [ f swap f CFURLCreateWithString ] keep
116     CFRelease ;
117
118 : <CFBundle> ( string -- bundle )
119     t <CFFileSystemURL> [
120         f swap CFBundleCreate
121     ] keep CFRelease ;
122
123 GENERIC: <CFNumber> ( number -- alien )
124 M: integer <CFNumber>
125     [ f kCFNumberLongLongType ] dip <longlong> CFNumberCreate ;
126 M: float <CFNumber>
127     [ f kCFNumberDoubleType ] dip <double> CFNumberCreate ;
128 M: t <CFNumber>
129     drop f kCFNumberIntType 1 <int> CFNumberCreate ;
130 M: f <CFNumber>
131     drop f kCFNumberIntType 0 <int> CFNumberCreate ;
132
133 : <CFData> ( byte-array -- alien )
134     [ f ] dip dup length CFDataCreate ;
135
136 : load-framework ( name -- )
137     dup <CFBundle> [
138         CFBundleLoadExecutable drop
139     ] [
140         "Cannot load bundled named " prepend throw
141     ] ?if ;
142
143 TUPLE: CFRelease-destructor alien disposed ;
144 M: CFRelease-destructor dispose* alien>> CFRelease ;
145 : &CFRelease ( alien -- alien )
146     dup f CFRelease-destructor boa &dispose drop ; inline
147 : |CFRelease ( alien -- alien )
148     dup f CFRelease-destructor boa |dispose drop ; inline