123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef BASE_MAC_SCOPED_NSOBJECT_H_
- #define BASE_MAC_SCOPED_NSOBJECT_H_
- #include <type_traits>
- #import <Foundation/NSObject.h>
- #include "base/base_export.h"
- #include "base/compiler_specific.h"
- #include "base/mac/scoped_typeref.h"
- #if defined(__has_feature) && __has_feature(objc_arc)
- #error "Cannot include base/mac/scoped_nsobject.h in file built with ARC."
- #endif
- @class NSAutoreleasePool;
- namespace base {
- namespace internal {
- template <typename NST>
- struct ScopedNSProtocolTraits {
- static NST InvalidValue() { return nil; }
- static NST Retain(NST nst) { return [nst retain]; }
- static void Release(NST nst) { [nst release]; }
- };
- }
- template <typename NST>
- class scoped_nsprotocol
- : public ScopedTypeRef<NST, internal::ScopedNSProtocolTraits<NST>> {
- public:
- using ScopedTypeRef<NST,
- internal::ScopedNSProtocolTraits<NST>>::ScopedTypeRef;
-
- NST autorelease() { return [this->release() autorelease]; }
- };
- template <class C>
- void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) {
- p1.swap(p2);
- }
- template <class C>
- bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
- return p1 == p2.get();
- }
- template <class C>
- bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
- return p1 != p2.get();
- }
- template <typename NST>
- class scoped_nsobject : public scoped_nsprotocol<NST*> {
- public:
- using scoped_nsprotocol<NST*>::scoped_nsprotocol;
- static_assert(std::is_same<NST, NSAutoreleasePool>::value == false,
- "Use @autoreleasepool instead");
- };
- template<>
- class scoped_nsobject<id> : public scoped_nsprotocol<id> {
- public:
- using scoped_nsprotocol<id>::scoped_nsprotocol;
- };
- }
- #endif
|