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