/
HLSAssert.h
116 lines (99 loc) · 7.1 KB
/
HLSAssert.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
//
// HLSAssert.h
// CoconutKit
//
// Created by Samuel Défago on 7/14/10.
// Copyright 2010 Hortis. All rights reserved.
//
/**
* The following macros are only active if NS_BLOCK_ASSERTIONS is disabled for the project configuration you use
* (usually -DNS_BLOCK_ASSERTIONS=1)
*/
#ifndef NS_BLOCK_ASSERTIONS
/**
* Assertion at compile time
*
* Remark: Using a typedef avoids "unused variable" warnings, and enclosing within a block avoids "type redefinition" errors
*/
#define HLSStaticAssert(expr) {typedef char static_assertion_failure[(expr) ? 1 : -1];}
/**
* Insert this macro in the implementation of a method which is inherited but does not have any meaning for the class
* you are implementing. This can e.g. be helpful to disable the init NSObject inherited method (which sometimes is
* not meaningful)
*/
#define HLSForbiddenInheritedMethod() NSAssert(NO, @"Forbidden inherited method call. This method has " \
"been inherited from a parent class but could not " \
"be meaningfully overridden. It cannot therefore " \
"be called")
/**
* Insert this macro in methods which must be implemented. This can be useful in the following cases:
* - during development, to mark methods you have not implemented yet (but which must be)
* - in class design: to mark methods for which a class cannot provide a meaningful implementation, which must be
* supplied in subclasses (abstract class, e.g. the -draw method of a Shape class)
*/
#define HLSMissingMethodImplementation() NSAssert(NO, @"Missing method implementation")
/**
* The following macros check the type of objects in a collection. Useful for a class to verify that a collection it
* receives from a client through its public interface contains objects of the expected type. HLSAssert... macros can
* be used in methods only. In C-functions use the HLSCAssert... macros instead.
*
* Example: HLSAssertObjectsInEnumerationAreKindOfClass(views, UIScrollView);
*/
#define HLSAssertObjectsInEnumerationAreKindOfClass(enumeration, objectClassName) \
[[NSAssertionHandler currentHandler] handleIncorrectObjectClass:[objectClassName class] \
inEnumeration:enumeration \
strict:NO \
inMethod:_cmd \
object:self \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__]
#define HLSCAssertObjectsInEnumerationAreKindOfClass(enumeration, objectClassName) \
[[NSAssertionHandler currentHandler] handleIncorrectObjectClass:[objectClassName class] \
inEnumeration:enumeration \
strict:NO \
inFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__]
#define HLSAssertObjectsInEnumerationAreMembersOfClass(enumeration, objectClassName) \
[[NSAssertionHandler currentHandler] handleIncorrectObjectClass:[objectClassName class] \
inEnumeration:enumeration \
strict:YES \
inMethod:_cmd \
object:self \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__]
#define HLSCAssertObjectsInEnumerationAreMembersOfClass(enumeration, objectClassName) \
[[NSAssertionHandler currentHandler] handleIncorrectObjectClass:[objectClassName class] \
inEnumeration:enumeration \
strict:YES \
inFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__]
#else
#define HLSStaticAssert(expr)
#define HLSForbiddenInheritedMethod()
#define HLSMissingMethodImplementation()
#define HLSAssertObjectsInEnumerationAreKindOfClass(enumeration, objectClass)
#define HLSCAssertObjectsInEnumerationAreKindOfClass(enumeration, objectClass)
#define HLSAssertObjectsInEnumerationAreMembersOfClass(enumeration, objectClass)
#define HLSCAssertObjectsInEnumerationAreMembersOfClass(enumeration, objectClass)
#endif
@interface NSAssertionHandler (HLSAssert)
/**
* Check the class of objects in a collection and generate an assertion on mismatch. If strict is set to YES, all objects
* must be members of the class which is provided, otherwise they must be connected to it via inheritance
*/
- (void)handleIncorrectObjectClass:(Class)objectClass
inEnumeration:(id<NSFastEnumeration>)enumeration
strict:(BOOL)strict
inMethod:(SEL)selector
object:(id)object
file:(NSString *)fileName
lineNumber:(NSInteger)line;
- (void)handleIncorrectObjectClass:(Class)objectClass
inEnumeration:(id<NSFastEnumeration>)enumeration
strict:(BOOL)strict
inFunction:(NSString *)functionName
file:(NSString *)fileName
lineNumber:(NSInteger)line;
@end