-
Notifications
You must be signed in to change notification settings - Fork 3
/
SenUtilities.h
139 lines (119 loc) · 3.74 KB
/
SenUtilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*$Id: SenUtilities.h,v 1.7 2001/07/23 12:01:54 martin Exp $*/
// Copyright (c) 1997-2001, Sen:te Ltd. All rights reserved.
//
// Use of this source code is governed by the license in OpenSourceLicense.html
// found in this distribution and at http://www.sente.ch/software/ , where the
// original version of this source code can also be found.
// This notice may not be removed from this file.
#import <Foundation/Foundation.h>
#import "SenFoundationDefines.h"
#import "SenAssertion.h"
// Defining ASSIGN and RETAIN.
//
// ASSIGN should be used in -set... methods instead of the Apple
// promoted pattern (autorelease / retain). It is faster and
// semantically more correct.
// newVal is to avoid multiple evaluations of val.
// RETAIN is deprecated and should not used.
#if defined (GNUSTEP)
// GNUstep has its own definitions of ASSIGN and RETAIN
#else
#define RETAIN(var,val) \
do { \
id newVal = (val); \
if (var != newVal) { \
if (var) { \
[var release]; \
} \
if (newVal) { \
[newVal retain]; \
} \
var = newVal; \
} \
} while (0)
#if defined(GARBAGE_COLLECTION)
#define ASSIGN(var,val) \
do { \
var = val; \
} while (0)
#else
#define ASSIGN RETAIN
#endif
#endif
// Defining CHANGE_ASSIGN and CHANGE_RETAIN.
//
// Like ASSIGN above, CHANGE_ASSIGN should be used in -set... methods
// instead of the Apple promoted pattern (autorelease / retain).
// CHANGE_ASSIGN sends willChange to self, but only if the variable
// is really changed.
// CHANGE_RETAIN is deprecated and should not used.
@protocol Changes
- (void) willChange;
@end
#define SELF_WILL_CHANGE do { \
[(id <Changes>) self willChange]; \
} while (0)
#define CHANGE_RETAIN(var,val) do { \
id newVal = (val); \
if (var != newVal) { \
SELF_WILL_CHANGE; \
if (var) { \
[var release]; \
} \
if (newVal) { \
[newVal retain]; \
} \
var = newVal; \
} \
} while (0)
#if defined(GARBAGE_COLLECTION)
#define CHANGE_ASSIGN(var,val) \
do { \
SELF_WILL_CHANGE; \
var = val; \
} while (0)
#else
#define CHANGE_ASSIGN CHANGE_RETAIN
#endif
// Defining RELEASE.
//
// The RELEASE macro can be used in any place where a release
// message would be sent. VAR is released and set to nil
#if defined (GNUSTEP)
// GNUstep has its own macro.
#else
#if defined(GARBAGE_COLLECTION)
#define RELEASE(var)
#else
#define RELEASE(var) \
do { \
if (var) { \
[(id) var release]; \
var = nil; \
} \
} while (0)
#endif
#endif
// Protected type casting
#define AsKindOfClass(_class,_object) \
({ \
id _val = (_object); \
senassert((_val == nil) || [_val isKindOfClass:[_class class]]); \
(_class *) _val; \
})
#define AsConformingToProtocol(_protocol,_object) \
({ \
id _val = (_object); \
senassert((_val == nil) || [_val conformsToProtocol:@protocol(_protocol)]); \
(id <_protocol>) _val; \
})
// Miscellaneous constants and predicates
SENFOUNDATION_EXPORT NSRange SenRangeNotFound;
#define isEmptyStringRange(x) ((x).length == 0)
#define isFoundStringRange(x) ((x).length > 0)
#define isValidTextRange(x) ((x).location != NSNotFound)
#define SenDefaultNotificationCenter [NSNotificationCenter defaultCenter]
#define SenDefaultUserDefaults [NSUserDefaults standardUserDefaults]
#define SenDefaultFileManager [NSFileManager defaultManager]
#define SenDefaultNotificationQueue [NSNotificationQueue defaultQueue]
#define SenDefaultTimeZone [NSTimeZone defaultTimeZone]