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

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

Issue 1322933005: Port uses of SkLazyPtr to SkOncePtr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: the rest Created 5 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkOncePtr_DEFINED 8 #ifndef SkOncePtr_DEFINED
9 #define SkOncePtr_DEFINED 9 #define SkOncePtr_DEFINED
10 10
11 #include "SkAtomics.h" 11 #include "SkAtomics.h"
12 #include "SkUniquePtr.h"
12 13
13 template <typename T> class SkStaticOnce; 14 template <typename T> class SkStaticOnce;
14 15
15 // Use this to create a global static pointer that's intialized exactly once whe n you call get(). 16 // Use this to create a global static pointer that's intialized exactly once whe n you call get().
16 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkStaticOnce< type> name 17 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkStaticOnce< type> name
17 18
18 // Use this for a local or member pointer that's initialized exactly once when y ou call get(). 19 // Use this for a local or member pointer that's initialized exactly once when y ou call get().
19 template <typename T> 20 template <typename T, typename Delete = skstd::default_delete<T>>
20 class SkOncePtr : SkNoncopyable { 21 class SkOncePtr : SkNoncopyable {
21 public: 22 public:
22 SkOncePtr() { sk_bzero(this, sizeof(*this)); } 23 SkOncePtr() { sk_bzero(this, sizeof(*this)); }
23 24 ~SkOncePtr() {
24 // SkOncePtr does not have a destructor and does not clean up the pointer. But you may, e.g. 25 if (T* ptr = (T*)*this) {
25 // delete (T*)fOncePtr; 26 Delete()(ptr);
26 // SkSafeUnref((T*)fOncePtr); 27 }
27 // etc. 28 }
28 29
29 template <typename F> 30 template <typename F>
30 T* get(const F& f) const { 31 T* get(const F& f) const {
31 return fOnce.get(f); 32 return fOnce.get(f);
32 } 33 }
33 34
34 operator T*() const { 35 operator T*() const {
35 return (T*)fOnce; 36 return (T*)fOnce;
36 } 37 }
37 38
38 private: 39 private:
39 SkStaticOnce<T> fOnce; 40 SkStaticOnce<T> fOnce;
herb_g 2015/09/08 20:24:34 Ok. So as things evolve, I don't think that name S
mtklein_C 2015/09/08 20:37:17 Done, agreed.
40 }; 41 };
41 42
42 /* TODO(mtklein): in next CL 43 /* TODO(mtklein): in next CL
43 typedef SkStaticOnce<void> SkOnceFlag; 44 typedef SkStaticOnce<void> SkOnceFlag;
44 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name 45 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
45 46
46 template <typename F> 47 template <typename F>
47 inline void SkOnce(SkOnceFlag* once, const F& f) { 48 inline void SkOnce(SkOnceFlag* once, const F& f) {
48 once->get([&]{ f(); return (void*)2; }); 49 once->get([&]{ f(); return (void*)2; });
49 } 50 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 94 }
94 95
95 private: 96 private:
96 // fState == 0 --> we have not created our ptr yet 97 // fState == 0 --> we have not created our ptr yet
97 // fState == 1 --> someone is in the middle of creating our ptr 98 // fState == 1 --> someone is in the middle of creating our ptr
98 // else --> (T*)fState is our ptr 99 // else --> (T*)fState is our ptr
99 mutable SkAtomic<uintptr_t> fState; 100 mutable SkAtomic<uintptr_t> fState;
100 }; 101 };
101 102
102 #endif//SkOncePtr_DEFINED 103 #endif//SkOncePtr_DEFINED
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698