objc_release_properties.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #if defined(__has_feature) && __has_feature(objc_arc)
  5. #error "ARC manages properties, so base::mac::ReleaseProperties isn't needed."
  6. #endif
  7. #ifndef BASE_MAC_OBJC_RELEASE_PROPERTIES_H_
  8. #define BASE_MAC_OBJC_RELEASE_PROPERTIES_H_
  9. #import <Foundation/Foundation.h>
  10. #include "base/base_export.h"
  11. // base::mac::ReleaseProperties(self) can be used in a class's -dealloc method
  12. // to release all properties marked "retain" or "copy" and backed by instance
  13. // variables. It only affects properties defined by the calling class, not
  14. // sub/superclass properties.
  15. //
  16. // Example usage:
  17. //
  18. // @interface AllaysIBF : NSObject
  19. //
  20. // @property(retain, nonatomic) NSString* string;
  21. // @property(copy, nonatomic) NSMutableDictionary* dictionary;
  22. // @property(assign, nonatomic) IBFDelegate* delegate;
  23. //
  24. // @end // @interface AllaysIBF
  25. //
  26. // @implementation AllaysIBF
  27. //
  28. // - (void)dealloc {
  29. // base::mac::ReleaseProperties(self);
  30. // [super dealloc];
  31. // }
  32. //
  33. // @end // @implementation AllaysIBF
  34. //
  35. // self.string and self.dictionary will each be released, but self.delegate
  36. // will not because it is marked "assign", not "retain" or "copy".
  37. //
  38. // Another approach would be to provide a base class to inherit from whose
  39. // -dealloc walks the property lists of all subclasses to release their
  40. // properties. Distant subclasses might not expect it and over-release their
  41. // properties, so don't do that.
  42. namespace base {
  43. namespace mac {
  44. namespace details {
  45. BASE_EXPORT void ReleaseProperties(id, Class);
  46. } // namespace details
  47. template <typename Self>
  48. void ReleaseProperties(Self* self) {
  49. details::ReleaseProperties(self, [Self class]);
  50. }
  51. } // namespace mac
  52. } // namespace base
  53. #endif // BASE_MAC_OBJC_RELEASE_PROPERTIES_H_