scoped_cftyperef.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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_CFTYPEREF_H_
  5. #define BASE_MAC_SCOPED_CFTYPEREF_H_
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include "base/mac/scoped_typeref.h"
  8. namespace base {
  9. // ScopedCFTypeRef<> is patterned after std::unique_ptr<>, but maintains
  10. // ownership of a CoreFoundation object: any object that can be represented
  11. // as a CFTypeRef. Style deviations here are solely for compatibility with
  12. // std::unique_ptr<>'s interface, with which everyone is already familiar.
  13. //
  14. // By default, ScopedCFTypeRef<> takes ownership of an object (in the
  15. // constructor or in reset()) by taking over the caller's existing ownership
  16. // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and
  17. // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not
  18. // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy|
  19. // enum. If the value |RETAIN| is passed (in the constructor or in reset()),
  20. // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial
  21. // ownership is not changed.
  22. namespace internal {
  23. template<typename CFT>
  24. struct ScopedCFTypeRefTraits {
  25. static CFT InvalidValue() { return nullptr; }
  26. static CFT Retain(CFT object) {
  27. CFRetain(object);
  28. return object;
  29. }
  30. static void Release(CFT object) {
  31. CFRelease(object);
  32. }
  33. };
  34. } // namespace internal
  35. template<typename CFT>
  36. using ScopedCFTypeRef =
  37. ScopedTypeRef<CFT, internal::ScopedCFTypeRefTraits<CFT>>;
  38. } // namespace base
  39. #endif // BASE_MAC_SCOPED_CFTYPEREF_H_