Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: include/private/SkTemplates.h

Issue 1626873004: Revert of skstd -> std for unique_ptr (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/private/SkOncePtr.h ('k') | include/private/SkUniquePtr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkTemplates_DEFINED 10 #ifndef SkTemplates_DEFINED
11 #define SkTemplates_DEFINED 11 #define SkTemplates_DEFINED
12 12
13 #include "SkMath.h" 13 #include "SkMath.h"
14 #include "SkTLogic.h" 14 #include "SkTLogic.h"
15 #include "SkTypes.h" 15 #include "SkTypes.h"
16 #include "SkUniquePtr.h"
16 #include <limits.h> 17 #include <limits.h>
17 #include <memory>
18 #include <new> 18 #include <new>
19 19
20 /** \file SkTemplates.h 20 /** \file SkTemplates.h
21 21
22 This file contains light-weight template classes for type-safe and exception -safe 22 This file contains light-weight template classes for type-safe and exception -safe
23 resource management. 23 resource management.
24 */ 24 */
25 25
26 /** 26 /**
27 * Marks a local variable as known to be unused (to avoid warnings). 27 * Marks a local variable as known to be unused (to avoid warnings).
(...skipping 22 matching lines...) Expand all
50 }; 50 };
51 51
52 /** \class SkAutoTCallVProc 52 /** \class SkAutoTCallVProc
53 53
54 Call a function when this goes out of scope. The template uses two 54 Call a function when this goes out of scope. The template uses two
55 parameters, the object, and a function that is to be called in the destructo r. 55 parameters, the object, and a function that is to be called in the destructo r.
56 If detach() is called, the object reference is set to null. If the object 56 If detach() is called, the object reference is set to null. If the object
57 reference is null when the destructor is called, we do not call the 57 reference is null when the destructor is called, we do not call the
58 function. 58 function.
59 */ 59 */
60 template <typename T, void (*P)(T*)> class SkAutoTCallVProc { 60 template <typename T, void (*P)(T*)> class SkAutoTCallVProc
61 : public skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>> {
61 public: 62 public:
62 SkAutoTCallVProc(T* obj) : fPtr(obj) {} 63 SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P> >(obj) {}
63 64
64 T* get() const { return fPtr.get(); } 65 operator T*() const { return this->get(); }
65 operator T* () const { return fPtr.get(); } 66 T* detach() { return this->release(); }
66 T* operator->() const { return fPtr.get(); }
67
68 T* detach() { return fPtr.release(); }
69 void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
70 private:
71 std::unique_ptr<T, SkFunctionWrapper<void, T, P>> fPtr;
72 }; 67 };
73 68
74 /** \class SkAutoTCallIProc 69 /** \class SkAutoTCallIProc
75 70
76 Call a function when this goes out of scope. The template uses two 71 Call a function when this goes out of scope. The template uses two
77 parameters, the object, and a function that is to be called in the destructor. 72 parameters, the object, and a function that is to be called in the destructor.
78 If detach() is called, the object reference is set to null. If the object 73 If detach() is called, the object reference is set to null. If the object
79 reference is null when the destructor is called, we do not call the 74 reference is null when the destructor is called, we do not call the
80 function. 75 function.
81 */ 76 */
82 template <typename T, int (*P)(T*)> class SkAutoTCallIProc { 77 template <typename T, int (*P)(T*)> class SkAutoTCallIProc
78 : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> {
83 public: 79 public:
84 SkAutoTCallIProc(T* obj) : fPtr(obj) {} 80 SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> (obj) {}
85 81
86 T* get() const { return fPtr.get(); } 82 operator T*() const { return this->get(); }
87 operator T* () const { return fPtr.get(); } 83 T* detach() { return this->release(); }
88 T* operator->() const { return fPtr.get(); }
89
90 T* detach() { return fPtr.release(); }
91 void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
92 private:
93 std::unique_ptr<T, SkFunctionWrapper<int, T, P>> fPtr;
94 }; 84 };
95 85
96 /** \class SkAutoTDelete 86 /** \class SkAutoTDelete
97 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete< T> 87 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete< T>
98 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T > 88 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T >
99 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold 89 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold
100 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is 90 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is
101 thread-compatible, and once you dereference it, you get the threadsafety 91 thread-compatible, and once you dereference it, you get the threadsafety
102 guarantees of T. 92 guarantees of T.
103 93
104 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) 94 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*)
105 */ 95 */
106 template <typename T> class SkAutoTDelete { 96 template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> {
107 public: 97 public:
108 SkAutoTDelete(T* obj = NULL) : fPtr(obj) {} 98 SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {}
109 99
110 void swap(SkAutoTDelete& other) { fPtr.swap(other.fPtr); } 100 operator T*() const { return this->get(); }
111 101 void free() { this->reset(nullptr); }
112 T* get() const { return fPtr.get(); } 102 T* detach() { return this->release(); }
113 operator T* () const { return fPtr.get(); }
114 T* operator->() const { return fPtr.get(); }
115
116 void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
117 void free() { fPtr.reset(nullptr); }
118 T* detach() { return fPtr.release(); }
119 T* release() { return fPtr.release(); }
120 private:
121 std::unique_ptr<T> fPtr;
122 }; 103 };
123 104
124 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { 105 template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> {
125 public: 106 public:
126 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} 107 SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {}
127 108
128 void free() { this->reset(nullptr); } 109 void free() { this->reset(nullptr); }
129 T* detach() { return this->release(); } 110 T* detach() { return this->release(); }
130 }; 111 };
131 112
132 /** Allocate an array of T elements, and free the array in the destructor 113 /** Allocate an array of T elements, and free the array in the destructor
133 */ 114 */
134 template <typename T> class SkAutoTArray : SkNoncopyable { 115 template <typename T> class SkAutoTArray : SkNoncopyable {
135 public: 116 public:
136 SkAutoTArray() { 117 SkAutoTArray() {
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 * Returns void* because this object does not initialize the 449 * Returns void* because this object does not initialize the
469 * memory. Use placement new for types that require a cons. 450 * memory. Use placement new for types that require a cons.
470 */ 451 */
471 void* get() { return fStorage.get(); } 452 void* get() { return fStorage.get(); }
472 const void* get() const { return fStorage.get(); } 453 const void* get() const { return fStorage.get(); }
473 private: 454 private:
474 SkAlignedSStorage<sizeof(T)*N> fStorage; 455 SkAlignedSStorage<sizeof(T)*N> fStorage;
475 }; 456 };
476 457
477 #endif 458 #endif
OLDNEW
« no previous file with comments | « include/private/SkOncePtr.h ('k') | include/private/SkUniquePtr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698