OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MAC_SCOPED_LAUNCH_DATA_H_ | 5 #ifndef BASE_MAC_SCOPED_CFFILEDESCRIPTORREF_H_ |
6 #define BASE_MAC_SCOPED_LAUNCH_DATA_H_ | 6 #define BASE_MAC_SCOPED_CFFILEDESCRIPTORREF_H_ |
7 | 7 |
8 #include <launch.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
9 | |
10 #include <algorithm> | |
11 | 9 |
12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
14 | 12 |
15 namespace base { | 13 namespace base { |
16 namespace mac { | 14 namespace mac { |
17 | 15 |
18 // Just like scoped_ptr<> but for launch_data_t. | 16 // ScopedCFFileDescriptorRef is designed after ScopedCFTypeRef<>. On |
blundell
2012/11/26 16:05:06
Due to the fact that I'm using git, this file beco
| |
19 class ScopedLaunchData { | 17 // destruction, it will invalidate the file descriptor. |
18 // ScopedCFFileDescriptorRef (unlike ScopedCFTypeRef<>) does not support RETAIN | |
19 // semantics, copying, or assignment, as doing so would increase the chances | |
20 // that a file descriptor is invalidated while still in use. | |
21 class ScopedCFFileDescriptorRef { | |
20 public: | 22 public: |
21 typedef launch_data_t element_type; | 23 explicit ScopedCFFileDescriptorRef(CFFileDescriptorRef fdref = NULL) |
22 | 24 : fdref_(fdref) { |
23 explicit ScopedLaunchData(launch_data_t object = NULL) | |
24 : object_(object) { | |
25 } | 25 } |
26 | 26 |
27 ~ScopedLaunchData() { | 27 ~ScopedCFFileDescriptorRef() { |
28 if (object_) | 28 if (fdref_) { |
29 launch_data_free(object_); | 29 CFFileDescriptorInvalidate(fdref_); |
30 } | 30 CFRelease(fdref_); |
31 | |
32 void reset(launch_data_t object = NULL) { | |
33 if (object != object_) { | |
34 if (object_) | |
35 launch_data_free(object_); | |
36 object_ = object; | |
37 } | 31 } |
38 } | 32 } |
39 | 33 |
40 bool operator==(launch_data_t that) const { | 34 void reset(CFFileDescriptorRef fdref = NULL) { |
41 return object_ == that; | 35 if (fdref_ == fdref) |
36 return; | |
37 if (fdref_) { | |
38 CFFileDescriptorInvalidate(fdref_); | |
39 CFRelease(fdref_); | |
40 } | |
41 fdref_ = fdref; | |
42 } | 42 } |
43 | 43 |
44 bool operator!=(launch_data_t that) const { | 44 bool operator==(CFFileDescriptorRef that) const { |
45 return object_ != that; | 45 return fdref_ == that; |
46 } | 46 } |
47 | 47 |
48 operator launch_data_t() const { | 48 bool operator!=(CFFileDescriptorRef that) const { |
49 return object_; | 49 return fdref_ != that; |
50 } | 50 } |
51 | 51 |
52 launch_data_t get() const { | 52 operator CFFileDescriptorRef() const { |
53 return object_; | 53 return fdref_; |
54 } | 54 } |
55 | 55 |
56 void swap(ScopedLaunchData& that) { | 56 CFFileDescriptorRef get() const { |
57 std::swap(object_, that.object_); | 57 return fdref_; |
58 } | 58 } |
59 | 59 |
60 launch_data_t release() WARN_UNUSED_RESULT { | 60 CFFileDescriptorRef release() WARN_UNUSED_RESULT { |
61 launch_data_t temp = object_; | 61 CFFileDescriptorRef temp = fdref_; |
62 object_ = NULL; | 62 fdref_ = NULL; |
63 return temp; | 63 return temp; |
64 } | 64 } |
65 | 65 |
66 private: | 66 private: |
67 launch_data_t object_; | 67 CFFileDescriptorRef fdref_; |
68 | 68 |
69 DISALLOW_COPY_AND_ASSIGN(ScopedLaunchData); | 69 DISALLOW_COPY_AND_ASSIGN(ScopedCFFileDescriptorRef); |
70 }; | 70 }; |
71 | 71 |
72 } // namespace mac | 72 } // namespace mac |
73 } // namespace base | 73 } // namespace base |
74 | 74 |
75 #endif // BASE_MAC_SCOPED_LAUNCH_DATA_H_ | 75 #endif // BASE_MAC_SCOPED_CFFILEDESCRIPTORREF_H_ |
OLD | NEW |