/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. // GTMValidatingContainers are a set of mutable container classes that allow
  22. // you to have a selector on a target that is called to verify that the objects
  23. // being put into the container are valid. This can be controlled at compile
  24. // time so that you don't take the performance hit in a release build using the
  25. // GTM_CONTAINERS_VALIDATE macro.
  26. // We have supplied validators for simple cases such as kindOfClass or
  27. // conformsToProtocol. See GTMKindOfClassValidator et al. for details.
  28. //
  29. // Example of usage:
  30. // id target = [GTMKindOfClassValidator validateAgainstClass:[NSString class]];
  31. // SEL selector = @selector(validateObject:forContainer:);
  32. // GTMValidatingArray *array = [GTMValidatingArray validatingArrayWithTarget:target
  33. // selector:selector];
  34. // [array addObject:@"foo"]; // Will be good
  35. // [array addObject:[NSNumber numberWithInt:2]]; // Will fail
  36. //
  37. // By setting the GTM_CONTAINERS_VALIDATION_FAILED_LOG and
  38. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT macros you can control what happens
  39. // when a validation fails. If you implement your own validators, you may want
  40. // to control their internals using the same macros for consistency.
  41. //
  42. // Note that the validating collection types retain their targets.
  43. #import <Foundation/Foundation.h>
  44. #import "GTMDefines.h"
  45. // By default we only validate containers in debug. If you want to validate
  46. // in release as well, #define GTM_CONTAINERS_VALIDATE in a prefix or build
  47. // settings.
  48. #ifndef GTM_CONTAINERS_VALIDATE
  49. #if DEBUG
  50. #define GTM_CONTAINERS_VALIDATE 1
  51. #else // DEBUG
  52. #define GTM_CONTAINERS_VALIDATE 0
  53. #endif // DEBUG
  54. #endif // GTM_CONTAINERS_VALIDATE
  55. // If GTM_CONTAINERS_VALIDATE is on, and log and assert are both turned off
  56. // (see below), the object that failed validation will just not be added
  57. // to the container.
  58. // If you don't want log to occur on validation failure define
  59. // GTM_CONTAINERS_VALIDATION_FAILED_LOG to 0 in a prefix or build settings.
  60. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_LOG
  61. #define GTM_CONTAINERS_VALIDATION_FAILED_LOG GTM_CONTAINERS_VALIDATE
  62. #endif // GTM_CONTAINERS_VALIDATION_FAILED_LOG
  63. // If you don't want an assert to occur on validation failure define
  64. // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT to 0 in a prefix or build settings.
  65. #ifndef GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  66. #define GTM_CONTAINERS_VALIDATION_FAILED_ASSERT GTM_CONTAINERS_VALIDATE
  67. #endif // GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
  68. // Sometimes you get a container back from somebody else and want to validate
  69. // that it contains what you think it contains. _GTMValidateContainer
  70. // allows you to do exactly that. _GTMValidateContainer... give you specialty
  71. // functions for doing common types of validations. These all inline to nothing
  72. // if GTM_CONTAINERS_VALIDATE is not defined.
  73. #if GTM_CONTAINERS_VALIDATE
  74. void _GTMValidateContainer(id container, id target, SEL selector);
  75. void _GTMValidateContainerContainsKindOfClass(id container, Class cls);
  76. void _GTMValidateContainerContainsMemberOfClass(id container, Class cls);
  77. void _GTMValidateContainerConformsToProtocol(id container, Protocol *prot);
  78. void _GTMValidateContainerItemsRespondToSelector(id container, SEL sel);
  79. #else
  80. GTM_INLINE void _GTMValidateContainer(id container, id target, SEL selector) {
  81. }
  82. GTM_INLINE void _GTMValidateContainerContainsKindOfClass(id container,
  83. Class cls) {
  84. }
  85. GTM_INLINE void _GTMValidateContainerContainsMemberOfClass(id container,
  86. Class cls) {
  87. }
  88. GTM_INLINE void _GTMValidateContainerConformsToProtocol(id container,
  89. Protocol *prot) {
  90. }
  91. GTM_INLINE void _GTMValidateContainerItemsRespondToSelector(id container,
  92. SEL sel) {
  93. }
  94. #endif
  95. // See comments near top of file for class description.
  96. @interface GTMValidatingArray : NSMutableArray {
  97. #if GTM_CONTAINERS_VALIDATE
  98. NSMutableArray *embeddedContainer_;
  99. id target_;
  100. SEL selector_;
  101. #endif // #if GTM_CONTAINERS_VALIDATE
  102. }
  103. + (id)validatingArrayWithTarget:(id)target selector:(SEL)sel;
  104. + (id)validatingArrayWithCapacity:(NSUInteger)capacity
  105. target:(id)target
  106. selector:(SEL)sel;
  107. - (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
  108. - (id)initValidatingWithCapacity:(NSUInteger)capacity
  109. target:(id)target
  110. selector:(SEL)sel;
  111. @end
  112. // See comments near top of file for class description.
  113. @interface GTMValidatingDictionary : NSMutableDictionary {
  114. #if GTM_CONTAINERS_VALIDATE
  115. NSMutableDictionary *embeddedContainer_;
  116. id target_;
  117. SEL selector_;
  118. #endif // #if GTM_CONTAINERS_VALIDATE
  119. }
  120. + (id)validatingDictionaryWithTarget:(id)target selector:(SEL)sel;
  121. + (id)validatingDictionaryWithCapacity:(NSUInteger)capacity
  122. target:(id)target
  123. selector:(SEL)sel;
  124. - (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
  125. - (id)initValidatingWithCapacity:(NSUInteger)capacity
  126. target:(id)target
  127. selector:(SEL)sel;
  128. @end
  129. // See comments near top of file for class description.
  130. @interface GTMValidatingSet : NSMutableSet {
  131. #if GTM_CONTAINERS_VALIDATE
  132. NSMutableSet *embeddedContainer_;
  133. id target_;
  134. SEL selector_;
  135. #endif // #if GTM_CONTAINERS_VALIDATE
  136. }
  137. + (id)validatingSetWithTarget:(id)target selector:(SEL)sel;
  138. + (id)validatingSetWithCapacity:(NSUInteger)capacity
  139. target:(id)target
  140. selector:(SEL)sel;
  141. - (id)initValidatingWithTarget:(id)target selector:(SEL)sel;
  142. - (id)initValidatingWithCapacity:(NSUInteger)capacity
  143. target:(id)target
  144. selector:(SEL)sel;
  145. @end
  146. #pragma mark -
  147. #pragma mark Simple Common Validators
  148. // See comments near top of file for examples of how these are used.
  149. @protocol GTMContainerValidatorProtocol
  150. - (BOOL)validateObject:(id)object forContainer:(id)container;
  151. @end
  152. // Validates that a given object is a kind of class (instance of class or an
  153. // instance of any class that inherits from that class)
  154. @interface GTMKindOfClassValidator : NSObject <GTMContainerValidatorProtocol> {
  155. Class cls_;
  156. }
  157. + (id)validateAgainstClass:(Class)cls;
  158. - (id)initWithClass:(Class)cls;
  159. @end
  160. // Validates that a given object is a member of class (exact instance of class)
  161. @interface GTMMemberOfClassValidator : NSObject <GTMContainerValidatorProtocol> {
  162. Class cls_;
  163. }
  164. + (id)validateAgainstClass:(Class)cls;
  165. - (id)initWithClass:(Class)cls;
  166. @end
  167. // Validates that a given object conforms to a protocol
  168. @interface GTMConformsToProtocolValidator : NSObject <GTMContainerValidatorProtocol> {
  169. Protocol* prot_;
  170. }
  171. + (id)validateAgainstProtocol:(Protocol*)prot;
  172. - (id)initWithProtocol:(Protocol*)prot;
  173. @end
  174. // Validates that a given object responds to a given selector
  175. @interface GTMRespondsToSelectorValidator : NSObject <GTMContainerValidatorProtocol> {
  176. SEL sel_;
  177. }
  178. + (id)validateAgainstSelector:(SEL)sel;
  179. - (id)initWithSelector:(SEL)sel;
  180. @end