]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSNull+misc.m
fixed gcc 4.0 warnings
[sope] / sope-core / NGExtensions / FdExt.subproj / NSNull+misc.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NSNull+misc.h"
23 #include "common.h"
24
25 #if LIB_FOUNDATION_LIBRARY || GNUSTEP_BASE_LIBRARY
26 #  include <objc/objc-api.h>
27 #  include <objc/objc.h>
28 #  include <objc/encoding.h>
29 #  ifndef GNUSTEP
30 #    import <extensions/objc-runtime.h>
31 #  endif
32 #else
33 #  import <objc/objc-class.h>
34 #endif
35
36 @implementation NSNull(misc)
37
38 static int _doAbort = -1;
39 static inline BOOL doAbort(void) {
40   if (_doAbort == -1) {
41     _doAbort = [[NSUserDefaults standardUserDefaults]
42                                 boolForKey:@"NSNullAbortOnMessage"] ? 1 : 0;
43   }
44   return _doAbort ? YES : NO;
45 }
46
47 - (BOOL)isNotNull {
48   return NO;
49 }
50 - (BOOL)isNull {
51 #if DEBUG
52   NSLog(@"WARNING(%s): called deprecated -isNull on NSNull (use -isNotNull) !",
53         __PRETTY_FUNCTION__);
54   if (doAbort()) abort();
55 #endif
56   return YES;
57 }
58
59 - (NSString *)stringValue {
60 #if DEBUG && 0
61   NSLog(@"WARNING(%s): "
62         @"NSNull -stringValue returns empty string.",
63         __PRETTY_FUNCTION__);
64   if (doAbort()) abort();
65 #endif
66   return @"";
67 }
68 - (double)doubleValue {
69 #if DEBUG && 0
70   NSLog(@"WARNING(%s): "
71         @"NSNull -doubleValue returns 0.0.",
72         __PRETTY_FUNCTION__);
73   if (doAbort()) abort();
74 #endif
75   return 0.0;
76 }
77
78 - (unsigned int)length {
79 #if DEBUG
80   NSLog(@"WARNING(%s): "
81         @"called NSNull -length (returns 0) !!!",
82         __PRETTY_FUNCTION__);
83   if (doAbort()) abort();
84 #endif
85   return 0;
86 }
87 - (unsigned int)count {
88 #if DEBUG
89   NSLog(@"WARNING(%s): "
90         @"called NSNull -count (returns 0) !!!",
91         __PRETTY_FUNCTION__);
92   if (doAbort()) abort();
93 #endif
94   return 0;
95 }
96
97 - (BOOL)isEqualToString:(NSString *)_s {
98   /* Note: I think we can keep this as a regular method */
99   return _s == (id)self || _s == nil ? YES : NO;
100 }
101 - (BOOL)hasPrefix:(NSString *)_s {
102   /* Note: I think we can keep this as a regular method */
103   return _s == (id)self || _s == nil ? YES : NO;
104 }
105 - (BOOL)hasSuffix:(NSString *)_s {
106   /* Note: I think we can keep this as a regular method */
107   return _s == (id)self || _s == nil ? YES : NO;
108 }
109
110 - (unichar)characterAtIndex:(unsigned int)_idx {
111 #if DEBUG
112   NSLog(@"WARNING(%s): "
113         @"called NSNull -characterAtIndex:%d - returning 0!",
114         __PRETTY_FUNCTION__, _idx);
115   if (doAbort()) abort();
116 #endif
117   return 0;
118 }
119
120 /* key-value coding */
121
122 - (void)takeValue:(id)_value forKey:(NSString *)_key {
123   /* do nothing */
124 }
125 - (id)valueForKey:(NSString *)_key {
126   if ([_key isEqualToString:@"isNotNull"]) {
127     static NSNumber *noNum  = nil;
128     
129     if (noNum == nil)
130       noNum = [NSNumber numberWithBool:NO];
131     
132     return noNum;
133   }
134   if ([_key isEqualToString:@"isNull"]) {
135     static NSNumber *yesNum = nil;
136     
137     if (yesNum == nil)
138       yesNum = [NSNumber numberWithBool:YES];
139     return yesNum;
140   }
141   
142   /* do nothing */
143   return nil;
144 }
145
146 /* forwarding ... */
147
148 #if !GNU_RUNTIME
149 - (BOOL)respondsToSelector:(SEL)_sel {
150   /* fake that we have a selector */
151   return YES;
152 }
153
154 - (NSString *)descriptionWithLocale:(id)_locale indent:(int)_indent {
155   return @"<null>";
156 }
157 - (NSString *)descriptionWithLocale:(id)_locale {
158   return @"<null>";
159 }
160
161 #endif
162
163 - (void)forwardInvocation:(NSInvocation *)_invocation {
164   NSMethodSignature *sig;
165   
166   NSLog(@"ERROR(%s): called selector %@ on NSNull !",
167         __PRETTY_FUNCTION__,
168         NSStringFromSelector([_invocation selector]));
169   if (doAbort()) abort();
170   
171   if ((sig = [_invocation methodSignature])) {
172     const char *ret;
173     
174     if ((ret = [sig methodReturnType])) {
175       switch (*ret) {
176         case _C_INT: {
177           int v = 0;
178           [_invocation setReturnValue:&v];
179           break;
180         }
181         case _C_UINT: {
182           unsigned int v = 0;
183           [_invocation setReturnValue:&v];
184           break;
185         }
186           
187         case _C_ID:
188         case _C_CLASS: {
189           id v = nil;
190           [_invocation setReturnValue:&v];
191           break;
192         }
193         
194         default:
195           NSLog(@"  didn't set return value for type '%s'", ret);
196           break;
197       }
198     }
199     else
200       NSLog(@"  no method return type in signature %@", sig);
201   }
202   else
203     NSLog(@"  no method signature in invocation %@", _invocation);
204 }
205
206 @end /* NSNull(misc) */
207
208 @implementation NSObject(NSNullMisc)
209
210 - (BOOL)isNotNull {
211   return YES;
212 }
213
214 - (BOOL)isNull {
215 #if DEBUG
216   NSLog(@"%s: WARNING, called -isNull on NSObject (use -isNotNull) !",
217         __PRETTY_FUNCTION__);
218 #endif
219   return NO;
220 }
221
222 @end /* NSObject(NSNullMisc) */
223