scoped_nsobject.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2012 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. #ifndef BASE_MAC_SCOPED_NSOBJECT_H_
  5. #define BASE_MAC_SCOPED_NSOBJECT_H_
  6. #include <type_traits>
  7. // Include NSObject.h directly because Foundation.h pulls in many dependencies.
  8. // (Approx 100k lines of code versus 1.5k for NSObject.h). scoped_nsobject gets
  9. // singled out because it is most typically included from other header files.
  10. #import <Foundation/NSObject.h>
  11. #include "base/base_export.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/mac/scoped_typeref.h"
  14. #if defined(__has_feature) && __has_feature(objc_arc)
  15. #error "Cannot include base/mac/scoped_nsobject.h in file built with ARC."
  16. #endif
  17. @class NSAutoreleasePool;
  18. namespace base {
  19. // scoped_nsobject<> is patterned after std::unique_ptr<>, but maintains
  20. // ownership of an NSObject subclass object. Style deviations here are solely
  21. // for compatibility with std::unique_ptr<>'s interface, with which everyone is
  22. // already familiar.
  23. //
  24. // scoped_nsobject<> takes ownership of an object (in the constructor or in
  25. // reset()) by taking over the caller's existing ownership claim. The caller
  26. // must own the object it gives to scoped_nsobject<>, and relinquishes an
  27. // ownership claim to that object. scoped_nsobject<> does not call -retain,
  28. // callers have to call this manually if appropriate.
  29. //
  30. // scoped_nsprotocol<> has the same behavior as scoped_nsobject, but can be used
  31. // with protocols.
  32. //
  33. // scoped_nsobject<> is not to be used for NSAutoreleasePools. For C++ code use
  34. // NSAutoreleasePool; for Objective-C(++) code use @autoreleasepool instead. We
  35. // check for bad uses of scoped_nsobject and NSAutoreleasePool at compile time
  36. // with a template specialization (see below).
  37. namespace internal {
  38. template <typename NST>
  39. struct ScopedNSProtocolTraits {
  40. static NST InvalidValue() { return nil; }
  41. static NST Retain(NST nst) { return [nst retain]; }
  42. static void Release(NST nst) { [nst release]; }
  43. };
  44. } // namespace internal
  45. template <typename NST>
  46. class scoped_nsprotocol
  47. : public ScopedTypeRef<NST, internal::ScopedNSProtocolTraits<NST>> {
  48. public:
  49. using ScopedTypeRef<NST,
  50. internal::ScopedNSProtocolTraits<NST>>::ScopedTypeRef;
  51. // Shift reference to the autorelease pool to be released later.
  52. NST autorelease() { return [this->release() autorelease]; }
  53. };
  54. // Free functions
  55. template <class C>
  56. void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) {
  57. p1.swap(p2);
  58. }
  59. template <class C>
  60. bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
  61. return p1 == p2.get();
  62. }
  63. template <class C>
  64. bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
  65. return p1 != p2.get();
  66. }
  67. template <typename NST>
  68. class scoped_nsobject : public scoped_nsprotocol<NST*> {
  69. public:
  70. using scoped_nsprotocol<NST*>::scoped_nsprotocol;
  71. static_assert(std::is_same<NST, NSAutoreleasePool>::value == false,
  72. "Use @autoreleasepool instead");
  73. };
  74. // Specialization to make scoped_nsobject<id> work.
  75. template<>
  76. class scoped_nsobject<id> : public scoped_nsprotocol<id> {
  77. public:
  78. using scoped_nsprotocol<id>::scoped_nsprotocol;
  79. };
  80. } // namespace base
  81. #endif // BASE_MAC_SCOPED_NSOBJECT_H_