*: update all clients of the `CString::to_s` services
[nit.git] / lib / cocoa / foundation.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # The Foundation Kit provides basic Objective-C classes and structures
18 module foundation is ldflags "-framework Foundation"
19
20 in "ObjC Header" `{
21 #import <Foundation/Foundation.h>
22 `}
23
24 # Base of the Foundation framework class hierarchy
25 extern class NSObject in "ObjC" `{ NSObject * `}
26 end
27
28 # String of the Foundation Kit
29 #
30 # Created using `Text::to_nsstring`.
31 extern class NSString in "ObjC" `{ NSString * `}
32 super NSObject
33
34 # Null pointer
35 new nil in "ObjC" `{ return nil; `}
36
37 # Get an UTF8 encoded `char*` copy of `self`
38 fun utf8_string: CString in "ObjC" `{ return (char *)[self UTF8String]; `}
39
40 redef fun to_s do return utf8_string.to_s
41 end
42
43 redef class CString
44 # Get a `NSString` from `self` with the specified `length`
45 fun to_nsstring(length: Int): NSString in "ObjC" `{
46 return [[NSString alloc] initWithBytes:self
47 length:length
48 encoding:NSUTF8StringEncoding];
49 `}
50 end
51
52 redef class Text
53 # Get a `NSString` from `self`
54 fun to_nsstring: NSString do return to_cstring.to_nsstring(byte_length)
55 end
56
57 # Wrapper of byte buffers
58 extern class NSData in "ObjC" `{ NSData * `}
59
60 # Pointer to contained data
61 fun bytes: CString in "ObjC" `{ return (char*)self.bytes; `}
62
63 # Number of bytes containted in `self`
64 fun length: Int in "ObjC" `{ return self.length; `}
65
66 redef fun to_s do return bytes.to_s_with_length(length)
67 end
68
69 # Error condition
70 extern class NSError in "ObjC" `{ NSError * `}
71
72 # Wraps: `[self initWithDomain:(NSString)domain code:(NSInteger)code userInfo:(NSDictionary)dict]`
73 #new init_with_domain_code_user_info(domain: NSString, code: Int, dict: NSDictionary) in "ObjC" `{
74 # return [[NSError alloc] initWithDomain: domain code: code userInfo: dict];
75 #`}
76
77 # Wraps: `NSError.domain`
78 fun domain: NSString in "ObjC" `{
79 return [self domain];
80 `}
81
82 # Wraps: `NSError.code`
83 fun code: Int in "ObjC" `{
84 return [self code];
85 `}
86
87 # Wraps: `NSError.userInfo`
88 #fun user_info: NSDictionary in "ObjC" `{
89 # return [self userInfo];
90 #`}
91
92 # Wraps: `NSError.localizedDescription`
93 fun localized_description: NSString in "ObjC" `{
94 return [self localizedDescription];
95 `}
96
97 # Wraps: `NSError.localizedFailureReason`
98 fun localized_failure_reason: NSString in "ObjC" `{
99 return [self localizedFailureReason];
100 `}
101
102 # Wraps: `NSError.localizedRecoverySuggestion`
103 fun localized_recovery_suggestion: NSString in "ObjC" `{
104 return [self localizedRecoverySuggestion];
105 `}
106
107 # Wraps: `NSError.localizedRecoveryOptions`
108 #fun localized_recovery_options: NSArray in "ObjC" `{
109 # return [self localizedRecoveryOptions];
110 #`}
111
112 # Wraps: `NSError.recoveryAttempter`
113 fun recovery_attempter: NSObject in "ObjC" `{
114 return [self recoveryAttempter];
115 `}
116
117 # Wraps: `NSError.helpAnchor`
118 fun help_anchor: NSString in "ObjC" `{
119 return [self helpAnchor];
120 `}
121 end
122
123 # Path to a specific node in a tree of nested array collections
124 extern class NSIndexPath in "ObjC" `{ NSIndexPath * `}
125 super NSObject
126
127 # Wraps: `[self initWithIndex:(NSUInteger)index]`
128 new init_with_index(index: Int) in "ObjC" `{
129 return [[NSIndexPath alloc] initWithIndex: index];
130 `}
131
132 # Wraps: `NSIndexPath.length`
133 fun length: Int in "ObjC" `{
134 return [self length];
135 `}
136
137 # Wraps: `[self indexPathByAddingIndex:(NSUInteger)index]`
138 fun index_path_by_adding_index(index: Int): NSIndexPath in "ObjC" `{
139 return [self indexPathByAddingIndex: index];
140 `}
141
142 # Wraps: `[self indexPathByRemovingLastIndex]`
143 fun index_path_by_removing_last_index: NSIndexPath in "ObjC" `{
144 return [self indexPathByRemovingLastIndex];
145 `}
146
147 # Wraps: `[self indexAtPosition:(NSUInteger)position]`
148 fun index_at_position(position: Int): Int in "ObjC" `{
149 return [self indexAtPosition: position];
150 `}
151 end
152
153 # Interface to the defaults system for an app to customize its behavior to match a user's preferences
154 extern class NSUserDefaults in "ObjC" `{ NSUserDefaults * `}
155 super NSObject
156
157 # Wraps: `[NSUserDefaults standardUserDefaults]`
158 new standard_user_defaults in "ObjC" `{
159 return [NSUserDefaults standardUserDefaults];
160 `}
161
162 # Wraps: `[NSIndexPath stringForKey:]`
163 fun string_for_key(key: NSString): NSString in "ObjC" `{
164 return [self stringForKey: key];
165 `}
166
167 # Wraps: `[NSIndexPath setObject: forKey:]`
168 fun set_object(value: NSObject, default_name: NSString)
169 in "ObjC" `{
170 [self setObject:value forKey:default_name];
171 `}
172 end