]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSNull+misc.m
fixed bugs in logger, improved performance
[sope] / sope-core / NGExtensions / FdExt.subproj / NSNull+misc.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NSNull+misc.h"
24 #include "common.h"
25
26 #if LIB_FOUNDATION_LIBRARY || GNUSTEP_BASE_LIBRARY
27 #  include <objc/objc-api.h>
28 #  include <objc/objc.h>
29 #  include <objc/encoding.h>
30 #  ifndef GNUSTEP
31 #    import <extensions/objc-runtime.h>
32 #  endif
33 #else
34 #  import <objc/objc-class.h>
35 #endif
36
37 @implementation NSNull(misc)
38
39 static int _doAbort = -1;
40 static inline BOOL doAbort(void) {
41   if (_doAbort == -1) {
42     _doAbort = [[NSUserDefaults standardUserDefaults]
43                                 boolForKey:@"NSNullAbortOnMessage"] ? 1 : 0;
44   }
45   return _doAbort ? YES : NO;
46 }
47
48 - (BOOL)isNotNull {
49   return NO;
50 }
51 - (BOOL)isNull {
52 #if DEBUG
53   NSLog(@"WARNING(%s): called deprecated -isNull on NSNull (use -isNotNull) !",
54         __PRETTY_FUNCTION__);
55   if (doAbort()) abort();
56 #endif
57   return YES;
58 }
59
60 - (NSString *)stringValue {
61 #if DEBUG && 0
62   NSLog(@"WARNING(%s): "
63         @"NSNull -stringValue returns empty string.",
64         __PRETTY_FUNCTION__);
65   if (doAbort()) abort();
66 #endif
67   return @"";
68 }
69 - (double)doubleValue {
70 #if DEBUG && 0
71   NSLog(@"WARNING(%s): "
72         @"NSNull -doubleValue returns 0.0.",
73         __PRETTY_FUNCTION__);
74   if (doAbort()) abort();
75 #endif
76   return 0.0;
77 }
78
79 - (unsigned int)length {
80 #if DEBUG
81   NSLog(@"WARNING(%s): "
82         @"called NSNull -length (returns 0) !!!",
83         __PRETTY_FUNCTION__);
84   if (doAbort()) abort();
85 #endif
86   return 0;
87 }
88 - (unsigned int)count {
89 #if DEBUG
90   NSLog(@"WARNING(%s): "
91         @"called NSNull -count (returns 0) !!!",
92         __PRETTY_FUNCTION__);
93   if (doAbort()) abort();
94 #endif
95   return 0;
96 }
97
98 - (BOOL)isEqualToString:(NSString *)_s {
99 #if DEBUG
100   NSLog(@"WARNING(%s): "
101         @"called NSNull -isEqualToString:!!!",
102         __PRETTY_FUNCTION__);
103   if (doAbort()) abort();
104 #endif
105   return _s == (id)self || _s == nil ? YES : NO;
106 }
107
108 - (unichar)characterAtIndex:(unsigned int)_idx {
109 #if DEBUG
110   NSLog(@"WARNING(%s): "
111         @"called NSNull -characterAtIndex:%d - returning 0!",
112         __PRETTY_FUNCTION__, _idx);
113   if (doAbort()) abort();
114 #endif
115   return 0;
116 }
117
118 /* key-value coding */
119
120 - (void)takeValue:(id)_value forKey:(NSString *)_key {
121   /* do nothing */
122 }
123 - (id)valueForKey:(NSString *)_key {
124   if ([_key isEqualToString:@"isNotNull"]) {
125     static NSNumber *noNum  = nil;
126     
127     if (noNum == nil)
128       noNum = [NSNumber numberWithBool:NO];
129     
130     return noNum;
131   }
132   if ([_key isEqualToString:@"isNull"]) {
133     static NSNumber *yesNum = nil;
134     
135     if (yesNum == nil)
136       yesNum = [NSNumber numberWithBool:YES];
137     return yesNum;
138   }
139   
140   /* do nothing */
141   return nil;
142 }
143
144 /* forwarding ... */
145
146 #if !GNU_RUNTIME
147 - (BOOL)respondsToSelector:(SEL)_sel {
148   /* fake that we have a selector */
149   return YES;
150 }
151
152 - (NSString *)descriptionWithLocale:(id)_locale indent:(int)_indent {
153   return @"<null>";
154 }
155 - (NSString *)descriptionWithLocale:(id)_locale {
156   return @"<null>";
157 }
158
159 #endif
160
161 - (void)forwardInvocation:(NSInvocation *)_invocation {
162   NSMethodSignature *sig;
163   
164   NSLog(@"ERROR(%s): called selector %@ on NSNull !",
165         __PRETTY_FUNCTION__,
166         NSStringFromSelector([_invocation selector]));
167   if (doAbort()) abort();
168   
169   if ((sig = [_invocation methodSignature])) {
170     const unsigned char *ret;
171     
172     if ((ret = [sig methodReturnType])) {
173       switch (*ret) {
174         case _C_INT: {
175           int v = 0;
176           [_invocation setReturnValue:&v];
177           break;
178         }
179         case _C_UINT: {
180           unsigned int v = 0;
181           [_invocation setReturnValue:&v];
182           break;
183         }
184           
185         case _C_ID:
186         case _C_CLASS: {
187           id v = nil;
188           [_invocation setReturnValue:&v];
189           break;
190         }
191         
192         default:
193           NSLog(@"  didn't set return value for type '%s'", ret);
194           break;
195       }
196     }
197     else
198       NSLog(@"  no method return type in signature %@", sig);
199   }
200   else
201     NSLog(@"  no method signature in invocation %@", _invocation);
202 }
203
204 @end /* NSNull(misc) */
205
206 @implementation NSObject(NSNullMisc)
207
208 - (BOOL)isNotNull {
209   return YES;
210 }
211
212 - (BOOL)isNull {
213 #if DEBUG
214   NSLog(@"%s: WARNING, called -isNull on NSObject (use -isNotNull) !",
215         __PRETTY_FUNCTION__);
216 #endif
217   return NO;
218 }
219
220 @end /* NSObject(NSNullMisc) */
221