OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ | 5 #ifndef LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ |
6 #define LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ | 6 #define LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include <ppapi/c/pp_resource.h> | 10 #include <ppapi/c/pp_resource.h> |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 FakeResourceTracker(FakeResource* resource, | 48 FakeResourceTracker(FakeResource* resource, |
49 const char* classname, | 49 const char* classname, |
50 const char* file, | 50 const char* file, |
51 int line); | 51 int line); |
52 ~FakeResourceTracker(); | 52 ~FakeResourceTracker(); |
53 | 53 |
54 void AddRef() { sdk_util::AtomicAddFetch(&ref_count_, 1); } | 54 void AddRef() { sdk_util::AtomicAddFetch(&ref_count_, 1); } |
55 void Release() { sdk_util::AtomicAddFetch(&ref_count_, -1); } | 55 void Release() { sdk_util::AtomicAddFetch(&ref_count_, -1); } |
56 int32_t ref_count() const { return ref_count_; } | 56 int32_t ref_count() const { return ref_count_; } |
57 | 57 |
| 58 // Give up ownership of this resource. It is the responsibility of the caller |
| 59 // to delete this FakeResource. |
| 60 FakeResource* Pass() { |
| 61 FakeResource* result = resource_; |
| 62 resource_ = NULL; |
| 63 return result; |
| 64 } |
| 65 |
58 template <typename T> | 66 template <typename T> |
59 T* resource() { | 67 T* resource() { |
60 if (!CheckType(T::classname())) | 68 if (!CheckType(T::classname())) |
61 return NULL; | 69 return NULL; |
62 | 70 |
63 return static_cast<T*>(resource_); | 71 return static_cast<T*>(resource_); |
64 } | 72 } |
65 | 73 |
| 74 FakeResource* resource() { return resource_; } |
| 75 |
66 const char* classname() const { return classname_; } | 76 const char* classname() const { return classname_; } |
67 const char* file() const { return file_; } | 77 const char* file() const { return file_; } |
68 int line() const { return line_; } | 78 int line() const { return line_; } |
69 | 79 |
70 private: | 80 private: |
71 bool CheckType(const char* classname) const; | 81 bool CheckType(const char* classname) const; |
72 | 82 |
73 FakeResource* resource_; // Owned. | 83 FakeResource* resource_; // Owned. |
74 const char* classname_; // Weak reference. | 84 const char* classname_; // Weak reference. |
75 const char* file_; // Weak reference. | 85 const char* file_; // Weak reference. |
76 int line_; | 86 int line_; |
77 int32_t ref_count_; | 87 int32_t ref_count_; |
78 | 88 |
79 DISALLOW_COPY_AND_ASSIGN(FakeResourceTracker); | 89 DISALLOW_COPY_AND_ASSIGN(FakeResourceTracker); |
80 }; | 90 }; |
81 | 91 |
82 class FakeResource { | 92 class FakeResource { |
83 public: | 93 public: |
84 FakeResource() {} | 94 FakeResource() {} |
| 95 // Called when the resource is destroyed. For debugging purposes, this does |
| 96 // not happen until the resource manager is destroyed. |
85 virtual ~FakeResource() {} | 97 virtual ~FakeResource() {} |
| 98 // Called when the last reference to the resource is released. |
| 99 virtual void Destroy() {} |
86 | 100 |
87 private: | 101 private: |
88 DISALLOW_COPY_AND_ASSIGN(FakeResource); | 102 DISALLOW_COPY_AND_ASSIGN(FakeResource); |
89 }; | 103 }; |
90 | 104 |
91 template <typename T> | 105 template <typename T> |
92 inline T* FakeResourceManager::Get(PP_Resource handle) { | 106 inline T* FakeResourceManager::Get(PP_Resource handle) { |
93 return Get(handle)->resource<T>(); | 107 return Get(handle)->resource<T>(); |
94 } | 108 } |
95 | 109 |
96 #define CREATE_RESOURCE(MANAGER, TYPE, RESOURCE) \ | 110 #define CREATE_RESOURCE(MANAGER, TYPE, RESOURCE) \ |
97 (MANAGER)->Create(RESOURCE, #TYPE, __FILE__, __LINE__) | 111 (MANAGER)->Create((RESOURCE), #TYPE, __FILE__, __LINE__) |
98 | 112 |
99 #endif // LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ | 113 #endif // LIBRARIES_NACL_IO_TEST_FAKE_RESOURCE_MANAGER_H_ |
OLD | NEW |