OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_SKIA_REFPTR_H_ | |
6 #define CC_SKIA_REFPTR_H_ | |
7 | |
8 #include "third_party/skia/include/core/SkRefCnt.h" | |
9 | |
10 namespace cc { | |
11 | |
12 template<typename T> | |
13 class SkiaRefPtr { | |
jamesr
2012/11/28 22:20:54
This doesn't belong in cc/
| |
14 public: | |
15 SkiaRefPtr() | |
16 : ptr_(NULL) { | |
17 } | |
18 | |
19 SkiaRefPtr(const SkiaRefPtr& other) | |
20 : ptr_(other.ptr_) { | |
21 SkSafeRef(ptr_); | |
22 } | |
23 | |
24 ~SkiaRefPtr() { | |
25 clear(); | |
26 } | |
27 | |
28 SkiaRefPtr& operator=(const SkiaRefPtr& other) { | |
29 SkRefCnt_SafeAssign(ptr_, other.ptr_); | |
30 return *this; | |
31 } | |
32 | |
33 T* get() const { return ptr_; } | |
34 T& operator*() const { return *ptr_; } | |
35 T* operator->() const { return ptr_; } | |
36 | |
37 void clear() { | |
38 T* to_unref = ptr_; | |
39 ptr_ = NULL; | |
40 SkSafeUnref(to_unref); | |
41 } | |
42 | |
43 typedef T* SkiaRefPtr::*unspecified_bool_type; | |
44 operator unspecified_bool_type() const { | |
45 return ptr_ ? &SkiaRefPtr::ptr_ : NULL; | |
46 } | |
47 | |
48 // Skia objects come with a reference of 1, steal that reference here to | |
49 // take ownership. | |
50 static SkiaRefPtr<T> Adopt(T* ptr) { | |
51 return SkiaRefPtr<T>(ptr); | |
52 } | |
53 | |
54 private: | |
55 T* ptr_; | |
56 | |
57 // Only let Adopt() create this class from a raw Skia ref-counted | |
58 // pointer. | |
59 explicit SkiaRefPtr(T* ptr) : ptr_(ptr) {} | |
60 }; | |
61 | |
62 } // namespace cc | |
63 | |
64 #endif // CC_SKIA_REFPTR_H_ | |
OLD | NEW |