]> err.no Git - sope/blob - sope-core/NGExtensions/NGExtensions/NSException+misc.h
fixed OGo bug 912
[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
53 #if COCOA_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY
54 @interface NSException (NGLibFoundationCompatibility)
55 - (void)setReason:(NSString *)_reason;
56 @end
57 #endif
58
59
60 /*
61   The following macros are for use of locks together with exception handling.
62   A synchronized block is properly 'unlocked' even if an exception occures.
63   It is used this way:
64
65     SYNCHRONIZED(MyObject) {
66       THROW(MyException..);
67     }
68     END_SYNCHRONIZED;
69
70   Where MyObject must be an object that conforms to the NSObject and NSLocking
71   protocol.
72   This is much different to
73
74     [MyObject lock];
75     {
76       THROW(MyException..);
77     }
78     [MyObject unlock];
79
80   which leaves the lock locked when an exception happens.
81 */
82
83 #if defined(DEBUG_SYNCHRONIZED)
84
85 #define SYNCHRONIZED(__lock__) \
86   { \
87     id<NSObject,NSLocking> __syncLock__ = [__lock__ retain]; \
88     [__syncLock__ lock]; \
89     fprintf(stderr, "0x%08X locked in %s.\n", \
90             (unsigned)__syncLock__, __PRETTY_FUNCTION__); \
91     NS_DURING {
92
93 #define END_SYNCHRONIZED \
94     } \
95     NS_HANDLER { \
96       fprintf(stderr, "0x%08X exceptional unlock in %s exception %s.\n", \
97               (unsigned)__syncLock__, __PRETTY_FUNCTION__,\
98               [[localException description] cString]); \
99       [__syncLock__ unlock]; \
100       [__syncLock__ release]; __syncLock__ = nil; \
101       [localException raise]; \
102     } \
103     NS_ENDHANDLER; \
104     fprintf(stderr, "0x%08X unlock in %s.\n", \
105             (unsigned)__syncLock__, __PRETTY_FUNCTION__); \
106     [__syncLock__ unlock]; \
107     [__syncLock__ release];  __syncLock__ = nil; \
108   }
109
110 #else
111
112 #define SYNCHRONIZED(__lock__) \
113   { \
114     id<NSObject,NSLocking> __syncLock__ = [__lock__ retain]; \
115     [__syncLock__ lock]; \
116     NS_DURING {
117
118 #define END_SYNCHRONIZED \
119     } \
120     NS_HANDLER { \
121       [__syncLock__ unlock]; \
122       [__syncLock__ release]; \
123       [localException raise]; \
124     } \
125     NS_ENDHANDLER; \
126     [__syncLock__ unlock]; \
127     [__syncLock__ release]; \
128   }
129
130 #endif
131
132 #endif /* __NGExtensions_NSException_misc_H__ */