]> err.no Git - sope/blob - sope-core/NGExtensions/NGExtensions/NSException+misc.h
fixed gstep-base compile warnings
[sope] / sope-core / NGExtensions / NGExtensions / NSException+misc.h
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 #ifndef __NGExtensions_NSException_misc_H__
23 #define __NGExtensions_NSException_misc_H__
24
25 #import <Foundation/NSException.h>
26 #import <Foundation/NSLock.h>
27
28 /*
29   Miscellaneous method additions to NSException
30
31   the additions make it easier using exceptions in the Java way. In OpenStep
32   exceptions are identified by their name, subclasses are mainly used when
33   additional information needs to be stored. This works well as long as you do
34   not use exceptions very much (as in the Foundation Kit) - if you do you
35   soon wish to have a 'hierachy' of exceptions. This can be modelled - like in
36   Java - using a subclass for each different kind of exception and then using
37   the type of the exception for it's identification. This is supported by
38   libFoundation using the TRY and CATCH macros.
39
40   The methods below always use the class name as the exception name if the name
41   isn't specified explicitly.
42 */
43
44 @interface NSException(NGMiscellaneous)
45
46 - (id)initWithReason:(NSString *)_reason;
47 - (id)initWithReason:(NSString *)_reason userInfo:(id)_userInfo;
48 - (id)initWithFormat:(NSString *)_format,...;
49
50 @end
51
52 @interface NSObject(NSExceptionNGMiscellaneous)
53 - (BOOL)isException;
54 - (BOOL)isExceptionOrNull;
55 @end
56
57
58 #if COCOA_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY
59 @interface NSException (NGLibFoundationCompatibility)
60 - (void)setReason:(NSString *)_reason;
61 @end
62 #endif
63
64
65 /*
66   The following macros are for use of locks together with exception handling.
67   A synchronized block is properly 'unlocked' even if an exception occures.
68   It is used this way:
69
70     SYNCHRONIZED(MyObject) {
71       THROW(MyException..);
72     }
73     END_SYNCHRONIZED;
74
75   Where MyObject must be an object that conforms to the NSObject and NSLocking
76   protocol.
77   This is much different to
78
79     [MyObject lock];
80     {
81       THROW(MyException..);
82     }
83     [MyObject unlock];
84
85   which leaves the lock locked when an exception happens.
86 */
87
88 #if defined(DEBUG_SYNCHRONIZED)
89
90 #define SYNCHRONIZED(__lock__) \
91   { \
92     id<NSObject,NSLocking> __syncLock__ = [__lock__ retain]; \
93     [__syncLock__ lock]; \
94     fprintf(stderr, "0x%08X locked in %s.\n", \
95             (unsigned)__syncLock__, __PRETTY_FUNCTION__); \
96     NS_DURING {
97
98 #define END_SYNCHRONIZED \
99     } \
100     NS_HANDLER { \
101       fprintf(stderr, "0x%08X exceptional unlock in %s exception %s.\n", \
102               (unsigned)__syncLock__, __PRETTY_FUNCTION__,\
103               [[localException description] cString]); \
104       [__syncLock__ unlock]; \
105       [__syncLock__ release]; __syncLock__ = nil; \
106       [localException raise]; \
107     } \
108     NS_ENDHANDLER; \
109     fprintf(stderr, "0x%08X unlock in %s.\n", \
110             (unsigned)__syncLock__, __PRETTY_FUNCTION__); \
111     [__syncLock__ unlock]; \
112     [__syncLock__ release];  __syncLock__ = nil; \
113   }
114
115 #else
116
117 #define SYNCHRONIZED(__lock__) \
118   { \
119     id<NSObject,NSLocking> __syncLock__ = [__lock__ retain]; \
120     [__syncLock__ lock]; \
121     NS_DURING {
122
123 #define END_SYNCHRONIZED \
124     } \
125     NS_HANDLER { \
126       [__syncLock__ unlock]; \
127       [__syncLock__ release]; \
128       [localException raise]; \
129     } \
130     NS_ENDHANDLER; \
131     [__syncLock__ unlock]; \
132     [__syncLock__ release]; \
133   }
134
135 #endif
136
137 #endif /* __NGExtensions_NSException_misc_H__ */