PageRenderTime 42ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMValidatingContainers.h

http://macfuse.googlecode.com/
C++ Header | 196 lines | 114 code | 16 blank | 66 comment | 0 complexity | 8713862865cd28c6c368eb82887b842c MD5 | raw file
  1//
  2//  GTMValidatingContainers.h
  3//
  4//  Mutable containers that do verification of objects being added to them 
  5//  at runtime. Support for arrays, dictionaries and sets.
  6//
  7//  Copyright 2008 Google Inc.
  8//
  9//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 10//  use this file except in compliance with the License.  You may obtain a copy
 11//  of the License at
 12// 
 13//  http://www.apache.org/licenses/LICENSE-2.0
 14// 
 15//  Unless required by applicable law or agreed to in writing, software
 16//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 17//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 18//  License for the specific language governing permissions and limitations under
 19//  the License.
 20//
 21
 22// GTMValidatingContainers are a set of mutable container classes that allow
 23// you to have a selector on a target that is called to verify that the objects 
 24// being put into the container are valid. This can be controlled at compile 
 25// time so that you don't take the performance hit in a release build using the 
 26// GTM_CONTAINERS_VALIDATE macro.
 27// We have supplied validators for simple cases such as kindOfClass or 
 28// conformsToProtocol. See GTMKindOfClassValidator et al. for details.
 29//
 30// Example of usage:
 31// id target = [GTMKindOfClassValidator validateAgainstClass:[NSString class]];
 32// SEL selector = @selector(validateObject:forContainer:);
 33// GTMValidatingArray *array = [GTMValidatingArray validatingArrayWithTarget:target
 34//                                                                  selector:selector];
 35// [array addObject:@"foo"]; // Will be good
 36// [array addObject:[NSNumber numberWithInt:2]]; // Will fail
 37//
 38// By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and 
 39// GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
 40// when a validation fails. If you implement your own validators, you may want
 41// to control their internals using the same macros for consistency.
 42//
 43// Note that the validating collection types retain their targets.
 44
 45#import <Foundation/Foundation.h>
 46#import "GTMDefines.h"
 47
 48// By default we only validate containers in debug. If you want to validate
 49// in release as well, #define GTM_CONTAINERS_VALIDATE in a prefix or build
 50// settings.
 51#ifndef GTM_CONTAINERS_VALIDATE
 52#if DEBUG
 53#define GTM_CONTAINERS_VALIDATE 1
 54#else  // DEBUG
 55#define GTM_CONTAINERS_VALIDATE 0
 56#endif  // DEBUG
 57#endif  // GTM_CONTAINERS_VALIDATE
 58
 59// If GTM_CONTAINERS_VALIDATE is on, and log and assert are both turned off
 60// (see below), the object that failed validation will just not be added
 61// to the container.
 62
 63// If you don't want log to occur on validation failure define
 64// GTM_CONTAINERS_VALIDATION_FAILED_LOG to 0 in a prefix or build settings.
 65#ifndef GTM_CONTAINERS_VALIDATION_FAILED_LOG
 66#define GTM_CONTAINERS_VALIDATION_FAILED_LOG GTM_CONTAINERS_VALIDATE
 67#endif  // GTM_CONTAINERS_VALIDATION_FAILED_LOG
 68
 69// If you don't want an assert to occur on validation failure define
 70// GTM_CONTAINERS_VALIDATION_FAILED_ASSERT to 0 in a prefix or build settings.
 71#ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
 72#define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT GTM_CONTAINERS_VALIDATE
 73#endif  // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
 74
 75// Sometimes you get a container back from somebody else and want to validate
 76// that it contains what you think it contains. _GTMValidateContainer
 77// allows you to do exactly that. _GTMValidateContainer... give you specialty
 78// functions for doing common types of validations. These all inline to nothing
 79// if GTM_CONTAINERS_VALIDATE is not defined.
 80#if GTM_CONTAINERS_VALIDATE
 81void _GTMValidateContainer(id container, id target, SEL selector);
 82void _GTMValidateContainerContainsKindOfClass(id container, Class cls);
 83void _GTMValidateContainerContainsMemberOfClass(id container, Class cls);
 84void _GTMValidateContainerConformsToProtocol(id container, Protocol *prot);
 85void _GTMValidateContainerItemsRespondToSelector(id container, SEL sel);
 86#else
 87GTM_INLINE void _GTMValidateContainer(id container, id target, SEL selector) {
 88}
 89GTM_INLINE void _GTMValidateContainerContainsKindOfClass(id container,
 90                                                         Class cls) {
 91}
 92GTM_INLINE void _GTMValidateContainerContainsMemberOfClass(id container, 
 93                                                           Class cls) {
 94}
 95GTM_INLINE void _GTMValidateContainerConformsToProtocol(id container, 
 96                                                        Protocol *prot) {
 97}
 98GTM_INLINE void _GTMValidateContainerItemsRespondToSelector(id container, 
 99                                                            SEL sel) {
100}
101#endif
102
103
104// See comments near top of file for class description.
105@interface GTMValidatingArray : NSMutableArray {
106#if GTM_CONTAINERS_VALIDATE
107  NSMutableArray *embeddedContainer_;
108  id target_;
109  SEL selector_;
110#endif  // #if GTM_CONTAINERS_VALIDATE
111}
112+ (id)validatingArrayWithTarget:(id)target selector:(SEL)sel;
113+ (id)validatingArrayWithCapacity:(NSUInteger)capacity 
114                           target:(id)target 
115                         selector:(SEL)sel;
116- (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
117- (id)initValidatingWithCapacity:(NSUInteger)capacity
118                          target:(id)target 
119                        selector:(SEL)sel;
120@end
121
122// See comments near top of file for class description.
123@interface GTMValidatingDictionary : NSMutableDictionary {
124#if GTM_CONTAINERS_VALIDATE
125  NSMutableDictionary *embeddedContainer_;
126  id target_;
127  SEL selector_;
128#endif  // #if GTM_CONTAINERS_VALIDATE
129}
130+ (id)validatingDictionaryWithTarget:(id)target selector:(SEL)sel;
131+ (id)validatingDictionaryWithCapacity:(NSUInteger)capacity 
132                                target:(id)target 
133                              selector:(SEL)sel;
134- (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
135- (id)initValidatingWithCapacity:(NSUInteger)capacity
136                          target:(id)target 
137                        selector:(SEL)sel;
138@end
139
140// See comments near top of file for class description.
141@interface GTMValidatingSet : NSMutableSet {
142#if GTM_CONTAINERS_VALIDATE
143  NSMutableSet *embeddedContainer_;
144  id target_;
145  SEL selector_;
146#endif  // #if GTM_CONTAINERS_VALIDATE
147}
148+ (id)validatingSetWithTarget:(id)target selector:(SEL)sel;
149+ (id)validatingSetWithCapacity:(NSUInteger)capacity 
150                         target:(id)target 
151                       selector:(SEL)sel;
152- (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
153- (id)initValidatingWithCapacity:(NSUInteger)capacity
154                          target:(id)target 
155                        selector:(SEL)sel;
156@end
157
158#pragma mark -
159#pragma mark Simple Common Validators
160// See comments near top of file for examples of how these are used.
161@protocol GTMContainerValidatorProtocol
162- (BOOL)validateObject:(id)object forContainer:(id)container;
163@end
164
165// Validates that a given object is a kind of class (instance of class or an
166// instance of any class that inherits from that class)
167@interface GTMKindOfClassValidator : NSObject <GTMContainerValidatorProtocol> {
168  Class cls_;
169}
170+ (id)validateAgainstClass:(Class)cls;
171- (id)initWithClass:(Class)cls;
172@end
173
174// Validates that a given object is a member of class (exact instance of class)
175@interface GTMMemberOfClassValidator : NSObject <GTMContainerValidatorProtocol> {
176  Class cls_;
177}
178+ (id)validateAgainstClass:(Class)cls;
179- (id)initWithClass:(Class)cls;
180@end
181
182// Validates that a given object conforms to a protocol
183@interface GTMConformsToProtocolValidator : NSObject <GTMContainerValidatorProtocol> {
184  Protocol* prot_;
185}
186+ (id)validateAgainstProtocol:(Protocol*)prot;
187- (id)initWithProtocol:(Protocol*)prot;
188@end
189
190// Validates that a given object responds to a given selector
191@interface GTMRespondsToSelectorValidator : NSObject <GTMContainerValidatorProtocol> {
192  SEL sel_;
193}
194+ (id)validateAgainstSelector:(SEL)sel;
195- (id)initWithSelector:(SEL)sel;
196@end